Sitemap of the Module Media Website

Home
Accessibility
Privacy statement
Sitemap
Terms of use
About Module Media
Our approach
Services we offer
User experience (UX)
Web standards & accessibility
Search engine optimization
Our Work
Select case studies
Complete client list
Training
About our training
Training on DVD
Published books
Current course offerings
Blog
Ideas, tips & tricks
Latest articles
Blog search
Tag cloud
Contact Module Media
Submit a question
Where we're located

Module Media | An innovative web design and application development firm with a passion for web standards

Our thoughts, ideas and rants.

Sending SMTP Email with Authentication using ASP.NET
Posted by Module Media on July 8, 2010

One of the topics I teach in my ASP.NET course is the process of sending email using SMTP. The process is relatively simple, you create a new instance of the MailMessage class, passing in two required parameters into the constructor: To and From email addresses. Next you set the Subject property, optionally set the IsBodyHtml property, and finally set the Body property with the content of your email that your recipients will actually read. To send the email you create a new instance of the SmtpClient class, passing in the path to the SMTP host and optionally set the port number. To send the email you simply call the Send() method, passing in the MailMessage object and you’re good to go. A generic implementation of this script begins to look something like this:

Dim oMail As New MailMessage("zak@modulemedia.com", "zak@modulemedia.com")
oMail.Subject = "CONTACT FORM"
oMail.IsBodyHtml = True
oMail.Body = "Message: " & txtMessage.Text

Dim oSend As New SmtpClient("localhost")
oSend.Send(oMail)


As you can see from this script, the code assumes that you’re using Localhost (127.0.0.1) as the SMTP mail server. This is fine if you’re using Windows 2000 or Windows XP Pro where the SMTP Virtual Mail server was still part of IIS. But what if you’re developing in Windows Vista or Windows 7 where SMTP Virtual Mail was eliminated for many reasons including security? What if you’re building an application that you’ll actually be deploying to a remote Web host and need to configure SMTP to use a mail server that requires authentication? Perhaps you want to use your Internet Service Provider’s (ISP) SMTP mail server for relaying email for the Web application you are building? In these scenarios, you’ll need to configure authentication in an effort to connect to your Exchange server or ISP’s mail server. This can be done easily in ASP.NET thanks to the NetworkCredential class. Accepting your username and password as parameters into the constructor, the NetworkCredential class assures authentication with your mail server. The above code can be modified accordingly as follows:

Dim oMail As New MailMessage("zak@modulemedia.com", "zak@modulemedia.com")
oMail.Subject = "CONTACT FORM"
oMail.IsBodyHtml = True
oMail.Body = "Message: " & txtMessage.Text

Dim oSend As New SmtpClient("mail.yourserver.com")
Dim oUser As New System.Net.NetworkCredential("admin@yourserver.com", "password")
oSend.UseDefaultCredentials = False
oSend.Credentials = oUser

oSend.Send(oMail)


As you can see we modify the parameter passed into the SmtpClient class to use the mail server that you want to authenticate to. Second, we create a new instance of the NetworkCredential class, passing in the username and password as parameters into the constructor. Next we set the important UseDefaultCredentials property to False. By default, this property is set to True which means anonymous credentials are passed to the mail server…this is usually why you’ll receive an error message in regards to authenticating to the mail server. Setting this property to False guarantees that our username and password are passed in instead. Finally we set the Credentials property of our SmtpClient object variable equal to our NetworkCredential object variable, call the Send method, and we’re done. Email should send successfully!

Comments: 0     Tags: ,
Comments are closed