Knowledgebase: ASP .NET / ASP
ASP Send Mail Sample
Posted by on 21 August 2012 10:08 PM

While there are third-party components available to send e-mails from your ASP Web site, the Microsoft® Windows® operating system already includes everything you need to send e-mails from your ASP Web site. The following sample demonstrates how to utilize the built-in components. For further technical details, follow the links provided in the sample.

' ASP Send Mail Sample.

' In order to run this sample,
' replace the user name '[email protected]' with the e-mail of the account used to send the e-mail,
' replace the password 'PASSWORD' with the proper password of the account used to send the e-mail,
' replace the mail FROM address '[email protected]' with the e-mail of the account used to send the e-mail,
' and replace the mail TO address '[email protected]' with the e-mail address to send to.

' Create CDO.Message COM object.
Set myMail=CreateObject("CDO.Message")

' Message Configuration.

' Specifies the method used to send messages.
' Must be set to 2!
myMail.Configuration.Fields.Item ("") = 2

' Specifies the authentication mechanism to use when authentication is required to send messages to an SMTP service using a TCP/IP network socket.
' Must be set to 1!
myMail.Configuration.Fields.Item ("") = 1

' The name (DNS) or IP address of the machine hosting the SMTP service through which messages are to be sent.
' Set to 'localhost' for local SMTP server.
myMail.Configuration.Fields.Item ("") = "localhost"

' The port on which the SMTP service specified by the smtpserver field is listening for connections.
' Set to 25 for local SMTP server.
myMail.Configuration.Fields.Item ("") = 25

' Authentication.

' The username for authenticating to an SMTP server using basic (clear-text) authentication.
myMail.Configuration.Fields.Item ("") = "[email protected]"

' The password used to authenticate to an SMTP server using basic (clear-text) authentication.
myMail.Configuration.Fields.Item ("") = "PASSWORD"

' Update the configuration fields.

' Set mail FROM.
' Must match user name used for SMTP authentication!
myMail.From = "[email protected]"

' Set mail TO.
myMail.To = "[email protected]"

' Set mail SUBJECT.
myMail.Subject = "Sending e-mail with CDO"

' Set mail BODY (HTMLBody).
' Do not uncomment both, HTMLBody and TextBody, at the same time.
myMail.HTMLBody = "<H3>This is an HTML message.</H3>"

' Set mail BODY (TextBody).
' Do not uncomment both, HTMLBody and TextBody, at the same time.
'myMail.TextBody = "This is a plain text message."

' Send the mail.

' Release CDO object.
set myMail = nothing

  E-mail sent on <%= Now() %>.

This is an update to knowledge base article CDOSYS Sample ASP Script

(7 vote(s))
Not helpful

Comments (10)
Ramon Smitherman
13 June 2013 12:15 PM
I followed these steps and got no error and no email. It is difficult to understand what to correct. Below is the code I used. Please advise.
set mailer = createobject("cdo.message")
mailer.Configuration.Fields.Item ("") = 2
mailer.Configuration.Fields.Item ("") = 1
mailer.Configuration.Fields.Item ("") = "localhost"
mailer.Configuration.Fields.Item ("") = 25
mailer.Configuration.Fields.Item ("") = "[email protected]"
mailer.Configuration.Fields.Item ("") = "xxx" ' changed from actual

mailer.From = "[email protected]" ' changed from actual = "[email protected]"
mailer.Subject = "New Registration for Public Training"
mailer.HTMLBody = "<H3>This is an HTML message.</H3>"
set Mailer = Nothing
E-mail sent on <%= Now() %>.
27 September 2013 01:05 AM
//using System.Net;
//using System.Net.Mail;

string senderEmail = "myemailaddress";
NetworkCredential mailAuthentication = new NetworkCredential(senderEmail, "myemailloginpassword");
SmtpClient mailClient = new SmtpClient("mail.myhostingaccount", 25);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
//to user
MailMessage MyMailMessage;
MyMailMessage = new MailMessage(senderEmail, emailadd, subject, msg);
MyMailMessage.IsBodyHtml = false;
Parth Shah
26 November 2014 04:39 AM
What changes are required to be made if I were to do this over the secure connection?
Michael Carr
28 November 2014 10:31 AM
Since the script is connecting locally there is little benefit to using SSL but you could simply change the port and use the servers hostname instead of
07 October 2013 04:46 AM
Is any of this using SSL. If you were sending a reset password would this be a security issue? Does anyone have a example with SSL
Michael Carr
08 October 2013 11:11 AM
Sending passwords via email is a security concern itself. When doing this you should always suggest that your users change their passwords right away. Having the form itself connect over SSL would not provide much benefit, as that is a local to local connection but I would also make sure that the form is loaded over SSL via https:// by the end user.
17 October 2013 10:53 AM
Had the same issue with not getting an error or email sent. The solution that finally fixed it was make sure that your smtpserver literally said "localhost". I had ported my similar code from Godaddy and we used our accounts for smtpserver. So hope this helps some one coming over.
Michael Carr
25 October 2013 11:43 AM
Thanks for the feedback, that is a common solution to mail script troubleshooting.
09 March 2014 08:31 PM
I'm seeing the same behavior even after using "localhost". Was there any other solution to this? No error message, no email received...
Michael Carr
19 March 2014 09:04 AM
Please open a support ticket and we will be happy to take a look for you.
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).