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.
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.
Or a complete example in two lines of code:
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.