Knowledge Essentials - 3Essentials Hosting

SMTP-AUTH in ASP.net's System.Web.Mail

Article ID: 1048

 Back to Search

Sending Email with System.Web.Mail using SMTP-auth

 

This article details usage of ASP.net 1.0's older System.Web.Mail. 

For ASP.NET 2.0 includes much richer Email API support within the System.Net.Mail code namespace, discussed in the following article. 

http://knowledge.3essentials.com/web-hosting/article/1930/SMTP-AUTH-in-ASP.nets-System.Net.Mail.html


Many people think that you cannot authenticate a SMTP server using the System.Web.Mail.MailMessage class and opt for using a 3rd party component (like JMAIL which we have on our shared hosting servers) or .Net's newer System.Net.Mail (which would be the recommended solution).  While System.Net.Mail was added to replace System.Web.Mail and definitely supports SMTP-auth more gracefully, you can actually SMTP-authenticate with System.Web.Mail using the following approach:

Dim Msg As New System.Web.Mail.MailMessage
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpserver"
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "you@yourdomain.com"
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
Msg.To = "recipient@recipientdomain.com"
Msg.From = "sender@senderdomain.com"
Msg.Subject = "Subject"
Msg.Body = "Message"
System.Web.Mail.SmtpMail.SmtpServer = "smtpserver"
System.Web.Mail.SmtpMail.Send(Msg)

If you're sending via a remote, non-3essentials server, update the SMTPSERVER values (both of them) to the mail server's hostname. If you're sending via our servers, update the SMTPSERVER value to simply "localhost".

The sendusername and msg.from fields generally are both the same... specifying the email address you're using to send the mail... but you do have the option to make them different, i.e., the sendusername parameter is the email address you are authenticating to the smtp server with, and you can then actually specify a different FROM/SENDER address by way of the msg.from parameter.

 
Downloads Associated With This Article
No downloads are currently associated with this article.