Learn how you can have your own contact form in your own website with this tutorial on CDOSY.
Learn how you can have your own contact form in your own website with this tutorial on CDOSY.
Ever wonder how you could send email from your ASP page? Need to create your own contact form but have no clue how to do it? After reading this tutorial, you will find it quiet easy!
CDO (Collaboration Data Objects) is a Microsoft technology that is designed to simplify the creation of messaging applications.
CDOSYS is a built-in component in ASP. This component is used to send e-mails with ASP.
We all know that whenever you put your email address onto the internet for you to communicate with your visitors, you instantly get hit with tons of spam. This is because a spammer’s email finder application saw your email address and added it to their nasty hit list.
This is why website owners created a contact form for their visitors to interact with them. With this kind of technology (CDOSYS), you can receive emails from your visitors without letting the general public know your email address, reducing the spammer activity on your site.
This following tutorial will show you how you could send email using ASP built in component to a remote server.
Before we start worrying about how to send the email, let’s make a basic HTML form that will gather the following information from the user. Save this file with .htm extension, in this example I’ll name them as contact.htm.
<html>
<head>
<title>Tutorial: Send Email Using CDOSYS</title>
</head>
<body>
<form name="feedback" method="post" action="cdosys_mail.asp">
Your Name : <input type="text" name="txtName">
Your Email Address: <input type="text" name="txtEmail">
Cc: <input type="text" name="txtCc">
Bcc <input type="text" name="txtBcc">
Subject: <input type="text" name="txtSubject">
Comment/Suggestion : <textarea name="txtComment"></textarea>
<input name="submit" type="submit">
</form>
</body>
</html>
Now that we have our HTML Form ready, we need to create the ASP file containing the CDOSYS code that will retrieve this data and shoot off an email. All the properties that are set in the following code are self-explanatory and write only. Please take note that a Message is an object, it must be Set equal to nothing after we have finished with it to release the memory allocated to it.
Save this file with .asp extension and in the same directory with your previous html form. In our previous html form, we have a form action pointing to cdosys_mail.asp which is the name that we will give to this page.
<%
Dim txtName, txtEmail, txtCc, txtBcc, txtSubject, txtComment, htmlBody
txtName = Request.Form("txtName")
txtEmail = Request.Form("txtEmail")
txtCc = Request.Form("txtCc")
txtBcc = Request.Form("txtBcc")
txtSubject = Request.Form("txtSubject")
txtComment = Request.Form("txtComment")
htmlBody = "<b>Tutorial: Send Email Using CDOSYS</b><br>"
htmlBody = htmlBody & txtComment
Dim objCDOSYSCon
Set objCDOSYSMail = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "YourMailServerAddress"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'Your mail SMTP Port Number
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 'Timeout Setting
objCDOSYSCon.Fields.Update
Set objCDOSYSMail.Configuration = objCDOSYSCon
objCDOSYSMail.From = txtEmail
objCDOSYSMail.To = webmaster@mywebsite.com 'Set your email address here!
objCDOSYSMail.Cc = txtCc
objCDOSYSMail.Bcc = txtBcc
objCDOSYSMail.Subject = txtSubject
objCDOSYSMail.HTMLBody = htmlBody
objCDOSYSMail.Send 'Send the email out
'Clear connection and redirect user
Set objCDOSYSMail = Nothing
Set objCDOSYSCon = Nothing
Set adoCon = Nothing
Response.Redirect "email_successfully_sent_message.asp"
%>
Please note that this is a very basic ASP Email Form and is only for tutorial purposes. If you were to use this code on your web site, you would want to do some form validation to make sure the email addresses are valid, compulsory field are filled in, allow for attachments, etc.
That’s just it. Your site are now ready to received email from your visitors!