Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
Cures for Common Ailments Part 2 – Sending E-Mail from ASP.NET

While for most sending an E-Mail message tends to be fairly trivial to seasoned .NET programmers, a new .NET programmer might find it a little mysterious. .NET has all this functionality wrapped up for us in the System.Net.Mail namespace to maange the sending of a message. There are classes for attachments, notification options, alternate views, linked resources and many, many more things common to sending an e-mail. The core classes you need to be familiar with are MailMessage and SMTPClient. These are required for a minimal message to be sent.

This namespace is new to .NET 2.0 and has a predecessor, System.Web.Mail namespace. If you have a legacy 1.0 or 1.1 .NET application the framework will still support this code, but you will receive a compiler warning letting you know it is obsolete and where to go to update your code.

In order to send a minimal e-mail message, which let’s be honest will be the case about 99% of the time, you need to define a sender, the recipient(s), a subject and of course the message body. Finally we need to send the message through a mail server using SMTP (Simple Mail Transport Protocol), hence the SMTPClient class.

There are many properties of the MailMessage class to give us some great granular control over the body of a message, much of which will not be seen by our intended reader, but placed in the message headers. The code below demonstrates how to set up the MailMessage class to represent a very basic message. Note: the variable names are supplied instead of example strings so you will have an idea of what is being set.

        Dim oMailMsg As New System.Net.Mail.MailMessage()

 

        oMailMsg.To.Add(New MailAddress(ToAddr))

        oMailMsg.From = New MailAddress(From)

        oMailMsg.Subject = Subject

        oMailMsg.Body = Msg

Once we have our message defined we need to send it. To do so we will need to create a new instance of the SMTPClient class. This class has a couple of overloaded constructors, an empty version and one that accepts the address of our SMTP server. Notice I did not say mail server, this is because a mail server may actually be comprised of a POP and SMTP server. A POP server is used by an account holder to download or check their e-mail. The POP protocol requires some sort of user authentication so it can identify what account to retrieve mail.

The SMTP protocol is different because it is used to send e-mail. This is where we have a lot of problems with SPAM, because anyone can send raw e-mail from a command line and the SMTP protocol does not require authentication. Some mail servers (hopefully all) require some sort of authentication, such as only allowing outgoing mail from an authenticated IP address, etc. This can cause some problems for many who do not have a lot of experience sending e-mails from their applications because blocked access will throw an exception. So to avoid this you may need to check with your network administrators (I really hate to pull this one on you) if you have any problems and find out how to send e-mail.

For many in a shared web hosting environment you need to send the e-mail from the SMTP server for your site and it must come from a domain that is pointing to their servers. So you can not send e-mail from your Earthlink account from your web site.

Back to something useful, sending a message. The SMTPClient provides a Send method that has two important overloads. The first takes a MailMessage as its only parameter. The second accepts four strings and builds a basic MailMessage object it then passes to the first overload. The four parameters represent the From Address, Recipient, Subject and message Body. Once you invoke this method your message will be sent through the SMTP server you specified in the SMTPClient constructor or the Host property. Note: oMailMsg is the class we created in the previous snippet of code.

        Dim client As New SmtpClient(SMTPServer)

        client.Send(oMailMsg)

Or a complete example in two lines of code:

        Dim client As New SmtpClient(SMTPServer)

        client.Send(From, ToAddr, Subject, Msg)

That is all there is to sending a basic e-mail. I plan on following up over the next few months with examples of sending some more complex messages and possibly even a simple mail merge too.

Posted: Tuesday, March 27, 2007 10:54 AM

by Chris Love
Filed under: , ,

Comments

mfsx said:

I often turn to www.systemnetmail.com for samples on how to do stuff in the System.Net.Mail namespace.

But it's not explained as well as you just did here, Chris.

Btw... Maybe someone has a workaround for this: http://systemnetmail.com/faq/5.1.aspx

# March 28, 2007 3:54 AM

Chris Love's Official Blog - Professional ASP.NET said:

One of the big improvements in the configuration and maintenance of ASP.NET Web sites for version 2.0

# June 27, 2007 11:14 AM

Chris Love's Official Blog - Professional ASP.NET said:

A few months ago I made an entry reviewing how to send e-mail from an ASP.NET page , today I am going

# September 20, 2007 8:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS