Monday 20 September 2010

Sending HTML Emails and Emailing Tricks with C#


Sending emails is relatively easy with C#, even-so, its not much more difficult to send HTML based emails either. The formatting, and how different email clients interpret the HTML and CSS in the emails, differs slightly. The aims of this post is to present a few typical scenarios of different types of emails which are sent.

Sending a Simple email using an email server - non-HTML
using System.Net.Mail;
 
 
MailMessage mail = new MailMessage("from@email.com", "to@email.com", "Email Subject", "Email Message");
mail.IsBodyHtml = false;
this.SendMail(mail);
 
 
/// <summary>
/// Sends the mail using a mail message.
/// </summary>
/// <param name="Mail">The mail message.</param>
private void SendMail(MailMessage Mail)
{
     SmtpClient smtpClient = new SmtpClient();
     smtpClient.Host = "Email Server Address";
     smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 
     try
     {
          smtpClient.Send(Mail);
     }
     catch (Exception ex)
     {
          throw new EmailException(ErrorCodeType.SendFailed, ex);
     }
}


This is enough to send a simple email to a target destination using an existing email server. To enable HTML is the body, we simply enable the IsBodyHtml property of the MailMessage object by setting it to true.


Simple HTML Email using the function in the previous example

MailMessage mail = new MailMessage("from@email.com", "to@email.com", "Email Subject", "<b>HELLO!!</b><br><br>Test Message...");

mail.IsBodyHtml = true;

this.SendMail(mail);



With OutLook for example, some representations of styling alter slightly. For example, I spent some time trying to understand why the line-height was interpreted differently in outlook. For whatever reason, If I tested the HTML template in IE, FF or Chrome; everything would be the same apart from the line-height. This seems to be a common problem. The solution I came up with is to tell outlook to render it in a certain way and that other browsers should ignore it.

HTML Email with line spacing and styles applied to the body of the email
string emailBody = "<div style=\"font-family: Verdana, Arial, sans-serif; font-size: 9pt; mso-line-height-rule:exactly; line-height: 20px\">";
emailBody += "<b>HELLO!!</b><br><br>Test Message...</div>";
 
MailMessage mail = new MailMessage("from@email.com", "to@email.com", "Email Subject", emailBody);
 
mail.IsBodyHtml = true;
 
this.SendMail(mail);


By setting "mso-line-height-rule:exactly" before the line-height property, we are tell mso (Microsoft Office) to render this differently to other browsers. You'll notice that I set the line-height to 20px... for a 9pt font, this is quite huge normally, but in Outlook; it's not.


One more tip, which may be quite trivial to some... If you want to using alias' in your emails (I.e. Appear that the email is being sent from an Alias) then there is no special code. Simply format the 'From' address in the following Format... "AliasName" (Include the quotes aswell)

In a config file, this might look like... [Remember we need to escape special characters in the XML config file!]
<appSettings>
    <add key="Email.FromAddress" value="&quot;WebAdmin&quot; &lt;online@tmgcrb.co.uk&gt;"/>
</appSettings>