The MailMessage Always Delivers
The MailMessage object that's part of .NET lets you send e-mail notifications to your users and supports all the customizations you'd want in e-mail.
I like e-mail and I don't like phones. Phones, like bombs, go off when they want; with mail, I'm in control. (Incidentally, I think that anyone who sets up their e-mail client to pick up their mail every x minutes is nuts.)
If I have to be notified about something, I want to be notified by e-mail. Since I assume that everyone is like me, I build my applications to send e-mails when users need to be notified of some condition. If you prefer phones, most texting services let you route e-mails through them to your target.
To encourage others to use e-mail more, in this column I'll walk through the options for creating a MailMessage. My next column will show you what you can do when it comes time to send the message.
Preparing the Message
In the .NET world, sending e-mail is easy: You create a MailMessage and then you send it off to a mail server (.NET doesn't come with its own mail server). When creating a MailMessage, you can specify your return address, the address of the person you're sending to, a subject line and your body text all when you create your MailMessage object, as this example does:
Dim mm As New System.Net.Mail.MailMessage( _
"[email protected]", "[email protected]", _
"Hello!", "How are you today?")
Once the MailMessage object is created, you can also put addresses in the CC property so you can send copies to other recipients (or in the BCC property to send sneaky copies). When you set your To address as part of creating the MailMessage, you can pass only a single address. However, all the address properties (except the From address) are actually collections, which allows you to send a single e-mail to multiple people:
In my first example, I just sent a plain string message ("How are you today") but you can include HTML. If you do, the polite thing to do is to set the MailMessage's IsBodyHTML property to True to alert the (eventual) client that the text of the message contains HTML.
I hate when I get e-mail that's flagged as "high-priority"; I figure it's my decision as to how important a message is. But if you feel that you know better than the recipient, you can set the Priority property on the MailMessage object to one of three enumerated values: High, Low and Normal. But let's face it -- if you use this option, it's because you want to set the priority to High:
mm.Priority = System.Net.Mail.MailPriority.High;
You can't receive mail using the objects built into .NET but you can ask for a notification to be sent to the return address you specified in the From address. You indicate what problems you want to be notified about by setting the MailMessage's DeliveryNotificationOptions property to one of a set of enumerated values. This example will ask for a notification message to be sent if there's a delay in sending the e-mail (you can Or multiple options together so that you can be notified about all problems):
mm.DeliveryNotificationOptions = _
By the way, you may notice that the DeliveryNotificationOptions has both a "Never" and a "None" option. They mean the same thing.
Working with Attachments
You can also add an attachment to your e-mail. First, you need to create an Attachment object from some file by passing the Attachment object the full physical path name to the file. Once you've created the Attachment object, you can add it to the MailMessage Attachments collection. In this example, I use Server.MapPath to convert a text file in the same folder as the page into an Attachment.
Once you've created an attachment, you can read it using its ContentStream property. More usefully, you can provide information about what the file contains by setting the Attachment's ContentType property or use the ContentDisposition property's Inline property to specify that you want the attachment to be displayed with the message's body text:
att.ContentDisposition.Inline = True
Finally, the Attachment's Name property will let you provide a display name for the attachment when it's read by a mail client.
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.