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.
' http://msdn.microsoft.com/en-us/library/ms527271(v=exchg.10)
Set myMail=CreateObject("CDO.Message")


' Message Configuration.

' Specifies the method used to send messages.
' http://msdn.microsoft.com/en-us/library/ms526994(v=exchg.10)
' http://msdn.microsoft.com/en-us/library/ms527265(v=exchg.10)
' Must be set to 2!
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' Specifies the authentication mechanism to use when authentication is required to send messages to an SMTP service using a TCP/IP network socket.
' http://msdn.microsoft.com/en-us/library/ms526600(v=exchg.10)
' http://msdn.microsoft.com/en-us/library/ms526961(v=exchg.10)
' Must be set to 1!
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

' The name (DNS) or IP address of the machine hosting the SMTP service through which messages are to be sent.
' http://msdn.microsoft.com/en-us/library/ms527294(v=exchg.10)
' Set to 'localhost' for local SMTP server.
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"

' The port on which the SMTP service specified by the smtpserver field is listening for connections.
' http://msdn.microsoft.com/en-us/library/ms526227(v=exchg.10)
' Set to 25 for local SMTP server.
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25


' Authentication.

' The username for authenticating to an SMTP server using basic (clear-text) authentication.
' http://msdn.microsoft.com/en-us/library/ms527002(v=exchg.10)
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"

' The password used to authenticate to an SMTP server using basic (clear-text) authentication.
' http://msdn.microsoft.com/en-us/library/ms526940(v=exchg.10)
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PASSWORD"


' Update the configuration fields.
' http://msdn.microsoft.com/en-us/library/jj250294(v=office.15).aspx
myMail.Configuration.Fields.Update


' Set mail FROM.
' http://msdn.microsoft.com/en-us/library/ms527318(v=exchg.10)
' Must match user name used for SMTP authentication!
myMail.From = "[email protected]"

' Set mail TO.
' http://msdn.microsoft.com/en-us/library/ms527328(v=exchg.10)
myMail.To = "[email protected]"

' Set mail SUBJECT.
' http://msdn.microsoft.com/en-us/library/ms527248(v=exchg.10)
myMail.Subject = "Sending e-mail with CDO"

' Set mail BODY (HTMLBody).
' http://msdn.microsoft.com/en-us/library/ms527537(v=exchg.10)
' Do not uncomment both, HTMLBody and TextBody, at the same time.
myMail.HTMLBody = "<H3>This is an HTML message.</H3>"

' Set mail BODY (TextBody).
' http://msdn.microsoft.com/en-us/library/ms526935(v=exchg.10)
' Do not uncomment both, HTMLBody and TextBody, at the same time.
'myMail.TextBody = "This is a plain text message."


' Send the mail.
' http://msdn.microsoft.com/en-us/library/ms527781(v=exchg.10)
myMail.Send


' Release CDO object.
set myMail = nothing
%>

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

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

(7 vote(s))
Helpful
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 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxx" ' changed from actual

mailer.Configuration.Fields.Update
mailer.From = "[email protected]" ' changed from actual
mailer.to = "[email protected]"
mailer.Subject = "New Registration for Public Training"
mailer.HTMLBody = "<H3>This is an HTML message.</H3>"
Mailer.Send
set Mailer = Nothing
%>
<HTML>
<BODY>
E-mail sent on <%= Now() %>.
</BODY>
</HTML>
Joy
27 September 2013 01:05 AM
//THIS ONE WORKED in c#
//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;
mailClient.Send(MyMailMessage);
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 mail.myhostingaccount.com.
Maccurt
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.
David
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 mail.domain.com 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.
Evan
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:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).