I recently had a request on how to perform an IIS SMTP service mail merge with aspNetDkim.
This is actually pretty straightforward, with 1 minor, but important exception.
By default, the IIS SMTP service adds a date/time stamp to the emails. This will actually break DKIM strict signatures. To get around this, aspNetEmail has the option to automatically add this date/time header to the email. IIS SMTP service will recognize the header, and not try to insert it's own IIS header.
So, once we specify that option, the only other minor work we have to do, is tie into the RenderEmailContents event. Once we tie into that event, we get access to the (you guessed it) rendered contents of the email. We then sign these contents, and override the contents that would be used by aspNetEmail for ending to the IIS SMTP pickup directory.
Below is a code example of how to do this.
private void IisSmtpMerge()
{
EmailMessage msg = new EmailMessage();
msg.Subject = "Order Confirmation";
msg.To = "##EmailAddress##";
msg.From = "me@myCompany.com";
//logging for easier troubleshooting
msg.Logging = true;
msg.LogPath = "c:\\email.log";
//set the plain text body
msg.Body = "Hi ##EmailAddress##, \r\nhere you go...";
//add some custom X-Headers
msg.AddHeader( "X-Company", "Acme Corp" );
msg.AddXHeader( "X-FirstName", "##fldFirstName##" );
//add the render event, so we can sign the message
msg.RenderEmailContents += new RenderEmailContentsEventHandler( OnRenderEmail );
//a method that gets our mail merge data
DataTable dt = GetDataTable();
//create an instance of the IISSmtp object
IISSmtp iis = new IISSmtp( msg );
//add the arrival time, so IIS doesn't (which would break the DomainKeys signature)
iis.AddOriginalArrivalTime();
iis.PickupDirectory = "c:\\temp\\";
iis.SendMailMerge( dt );
}
private void OnRenderEmail( object sender, RenderEmailContentsEventArgs e )
{
string contents = e.Contents;
//sign the contents using a Domainkeys method
contents = SignDomainKeys( contents );
//or sign the contents using a Dkim method
contents = SignDkimRelaxed( contents );
//reset the contents
e.Contents = contents;
}
Examples of the SignDK or SignDkimRelaxed methods can be found here:
http://www.advancedintellect.com/post/2010/10/04/DomainKeys-and-DKIM-Beta-Plugin-Available.aspx
If anyone has any questions, feel free to contact me using the Contact Us page.
Thanks!
Dave Wanta