I'm searching for:
   
   

Richard
"No alarms and no surprises please"

My Blog



AlternateView Emails in ASP.NET 2.0
June 04, 2007

I found a nice little addition to the email capabilities inside of .NET 2.0 awhile back.  Microsoft reorganized a lot of the email functionality in their .NET 2.0 release.  In fact they even changed the Namespace that that you have to work with in order to send emails inside of ASP.NET.  It went from System.Web.Mail to System.Net.Mail, which makes sense because emails are really sent over a network as opposed to the web.

One feature you have to consider when setting up an email distribution system is how you can handle sending basic "text" emails versus sending well formatted ("pretty") emails that can contain html formatting.  Old systems use to require that you set your preferences to "html" as an option when signing up for emails that you receive. 

The best approach is to create an email that contains different MIME Content-Types of "text/plain" and "text/html" and let the email reader decide which version to display to the user.  Previously, this required getting down to actually communicating with the TCPClient socket on port 25.  While possible, it certainly adds a layer of complexity that most web developers fear to tread, myself included.  If anyone is interested, I created a decent SMTP Socket class that will let you talk with the socket layer and send html and text emails old school style.  Remember that even though a mail reader can parse the html version you send, the individual user may prefer a basic text view.

First, you'll need to come up with a good method for storing and accessing two different emails templates that you want to send which is beyond the scope of this entry and something I might discuss in a future blog.  To get the general concept, we store versions of our emails in the database, pull them in to a StringBuilder, do some regex on them for customization and send away.  Email templates could easily be stored in XML, Text files or even hardcoded into your code.  The important thing is that you have two seperate versions of your email template, the text version and the html version.  I've experimented with creating one html version and regexing out the html tags to create the text version but prefer to manage them seperately for better control over the formatting.

So, assuming you are familiar with a MailMessage and the SMTP classes, this is the basic code you need to distribute dual versions of the same email.

MailMessage mm = new MailMessage();
mm.To.Add(new MailAddress("ToEmailAddress", "ToDiplayName"));
mm.Subject = "Your Subject";

//load up the templates into the StringBuilders
StringBuilder sbBody = new StringBuilder("text template content");
StringBuilder sbBodyHtml = new StringBuilder("html template content");

//do your regexing and customization here, something like:

sbBody.Replace("##Message##", "hello world");

//add the 2 AlternateViews of the email
AlternateView textView = AlternateView.CreateAlternateViewFromString(sbBody.ToString(), null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(sbBodyHtml.ToString(), null, "text/html");

//add the AlternatViews to the MailMessage
mm.AlternateViews.Add(textView);
mm.AlternateViews.Add(htmlView);

SmtpClient smtp = new SmtpClient("localhost");
smtp.Send(mm);

If you look at the raw code of the email that gets sent, you'll see that it created two seperate Mime Content-Types for you and the email reader now has 2 seperate versions of the same email that the user can choose to view. 

Views: 17

Comments

No comments have been added to this blog. Why not be the first?