// <copyright file="EmailHelper.cs" company="GinkoSolutions.com">
// Copyright (c) 2011 All Right Reserved
// </copyright>
// <author>Sean Greasley</author>
// <email>sean@ginkosolutions.com/sean@tutorialgenius.com</email>
// <summary>Email helper class. Allows sending of html and plain text emails to a target email address.</summary>
namespace MVCEmailExample.Helpers
{ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MVCEmailExample.Exceptions;
using MVCEmailExample.Models;
/// <summary>
/// Email helper class.
/// Allows sending of html and plain text emails to a target email address.
/// </summary>
public class EmailHelper
{ /// <summary>
/// Sends an email to a recipient. Provides HTML and plain text views.
/// The recipient will receive which one their client supports.
/// </summary>
/// <param name="templateDir">Directory of where the HTML email template is stored.</param>
/// <param name="recipient">Receipient information</param>
public static void SendEmail(string templateDir, Recipient recipient)
{ try
{ // Build message
MailMessage message = new MailMessage();
message.To.Add(new MailAddress(recipient.Email));
message.Subject = ConfigurationManager.AppSettings["EmailSubject"];
// Create plain text mode for alternative view
AlternateView plainView = AlternateView.CreateAlternateViewFromString(ConfigurationManager.AppSettings["PlainTextEmail"], null, "text/plain");
message.AlternateViews.Add(plainView);
// Create HTML email version
MailDefinition mailDef = new MailDefinition();
mailDef.BodyFileName = string.Format(@"{0}\{1}", templateDir, @"Email.html"); mailDef.IsBodyHtml = true;
mailDef.Subject = ConfigurationManager.AppSettings["EmailSubject"];
// Build replacement collection to replace fields in Email.htm file
// Use fields anywhere in the template file. I.e. <%FRIENDNAME%>
ListDictionary replacements = new ListDictionary();
replacements.Add("<%NAME%>", recipient.Name);
// Use dummy control as owner (I.e. new System.Web.UI.Control()) as were in a class library.
// It's only use to determine where the access templates from as a relative base.
MailMessage msgHtml = mailDef.CreateMailMessage(recipient.Email, replacements, new System.Web.UI.Control());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
// Add linked resources
AddLinkedResources(templateDir, ref htmlView);
// Add HTML view
message.AlternateViews.Add(htmlView);
// Send message
SmtpClient client = new SmtpClient();
client.Send(message);
}
catch (Exception mailEx) { throw new MailerException("Error sending email.", mailEx); } }
/// <summary>
/// Adds linked resources to the email
/// Email template must contain the resource IDs in the following format: <img src="cid:CONTENTID" />
/// </summary>
/// <param name="templateDir">Directory of where the HTML email template images are stored.</param>
/// <param name="htmlView">A reference to the HTML view.</param>
private static void AddLinkedResources(string templateDir, ref AlternateView htmlView)
{ LinkedResource logo1 = new LinkedResource(string.Format(@"{0}\{1}", templateDir, @"Images\email.jpg"), MediaTypeNames.Image.Jpeg); logo1.ContentId = "email";
htmlView.LinkedResources.Add(logo1);
}
}
}