Mailing and SMTP

As no doubt you’ll love it, .NET comes with the support of sending e-mail messages by SMTP. Unfortunately, it can’t act as a mail receiver, but we should overcome the pain and examine how to send emails with it.

The key namespace is System.Net.Mail, and there are two important classes: MailMessage, which represents an e-mail message, and SmtpClient, which provides methods to send it.

First let’s build our message by using the MailMessage class!

MailMessage represents an email message eager to be sent. It has the following constructors:

  • MailAddress from, MailAddress to
  • String from, String to
  • String from, String to, String subject, String body

Don’t worry about MailAddress, it just takes a string and verifies that it is a valid e-mail address. It also defines some useful properties such as DisplayName or User. Use it instead of just passing a string.

The most useful thing to do I think is to befriend with the properties MailMessage exposes, so here’s a brief list:

The Properties of MailMessage
AlternateViews A strongly-typed collection of AlternateViews, which are exist to provide alternate views for the message.
Attachments A strongly-typed AttachmentCollection.
Bcc A strongly-typed MailAddressCollection of the people who are about to get a blind carbon copy of your message.
Body A string representing the body of the message.
BodyEncoding An Encoding for the body string.
CC A strongly typed MailAddressCollection of the addresses where carbon copies should be sent.
DeliveryNotificationOptions Choose from Delay, Never, None, OnSuccess, OnFail to get yourself notified.
From A MailAddress instance representing the sender.
Headers A NameValueCollection representing the headers sent with the mail.
IsBodyHtml Is the body of the message in HTML format?
Priority MailPriority enumeration: Low, Normal, High.
ReplyTo A MailAddress to reply to.
Sender A MailAddress representing the sender.
Subject A String as the subject of the mail.
SubjectEncoding The Encoding of the subject.
To Returns a strongly-typed MailAddressCollection.

 

I don’t think there’s any need for further explanation of MailMessage. Let’s focus on things you could attach to a mail: AlternateView, Attachment, LinkedResource and AttachmentBase. AttachmentBase acts as the base class of the other three classes. You won’t create instances of it, since it’s abstract.

Attachment can be a file or a stream. Optionally, you can create attachments from a string, by using the static CreateAttachmentFromString which returns an Attachment instance. You should set the MIME type of your attachment, too.

LinkedResource represents an attachment what is embedded into the body of the message (such as an image). To add one to your MailMessage, define an AlternateView, and add to the LinkedResources collection of it your instance of the LinkedResource, then add the AlternateView to your MailMessage.

An AlternateView represents an alternative view of your message body. You can create it from a file, or a stream, or from a string by calling the static CreateAlternateViewFromString method. You should specify the MIME type of it.

So long for creating a mail, now send it with SmtpClient. To begin work with the client, you should specify the SMTP host, and valid credentials to that host. Optionally, you can include a port number, too. Then build and send an e-mail message either synchronously or asynchronously. It’s worth mentioning that the Credentials property accepts a class that implements the ICredentialsByHost interface (for example System.Net.NetworkCredential). It’s also worth mentioning that you should never hard code your credentials (and think twice hard-coding the host, or anything related to the SmtpClient). Instead, you should specify the following data in your app/web.config file:

<system.net>
  <mailSettings>
        <smtp>
            <network host=”smtp.myhost.com”  username=”username” password=”p4ssrw0rd” defaultCredentials=”true”/>
        </smtp>
    </mailSettings>
</system.net>

Then you can create an instance of SmtpClient, set the UseDefaultCredentials property to true, and you are ready to go, without hard-coding anything.

Further Readings

MailMessage Class
Attachment Class
LinkedResource Class
AlternateView Class
SmtpClient Class

Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: