by Dave
5. October 2010 08:13
Note:This code example will only be valid until aspNetEmail v4.0 releases. Once it releases, the aspNetDkim will no longer be needed, as the DKIM and DomainKeys signing functionality will be baked into aspNetEmail.
I recently had a customer (thanks Noah!) email me and ask how to perform a DomainKeys and/or DKIM signed mail merge against the SmarterMail queue directory.
Great Question!
It turns out its relatively easy. What you will need to do, is:
a)Tie into aspNetEmail's new Queuing namespace, that has a new SmarterMail class.
b)Tie into the new RenderEmailContents event
c)In that event, sign the message, and return the signed contents to be written to the queue folder.
Here is the code example that demonstrates this functionality.
private void SmarterMailTest()
{
//create the EmailMessage object, and set some basic properties
EmailMessage msg = new EmailMessage();
msg.From = "me@mycompany.com";
msg.AddTo( "##EmailAddress##" );
msg.Subject = "Order Confirmation";
//a very simple body for testing purposes
msg.Body = "Hi ##FirstName## ##LastName##, here is your order total ##Amount##";
//add the render event, so we can sign the message
msg.RenderEmailContents += new RenderEmailContentsEventHandler( SignForSmartermail );
//set the SmarterMail queuing
aspNetEmail.Queuing.SmarterMail sm = new aspNetEmail.Queuing.SmarterMail( msg );
sm.SpoolDirectory = "c:\\temp\\";
//GetDataTable() is a method that fetches our data, and returns a DataTable object
DataTable dt = GetDataTable();
sm.SendMailMerge( dt );
}
private void SignForSmartermail( object sender, RenderEmailContentsEventArgs e )
{
string contents = e.Contents;
//sign the contents with DomainKeys
contents = SignDKForSmartermail( contents );
//we can also sign with Dkim
//contents = SignDkimForSmartermail( contents );
//reset the contents
e.Contents = contents;
}
private string SignDKForSmartermail( string contents )
{
DomainKeys dkeys = new DomainKeys( contents );
//parse the private rsa key that was generated by openSSL.exe
//we can get it from the file
dkeys.DKCertificate = DKCertificate.ParsePemFile( "testing\\rsa.private" );
dkeys.Canonicalization = CanonicalizationType.Simple;
//the dns record selector
dkeys.Selector = "testing";
//sign the email contents
string signedContents = dkeys.SignMail();
return signedContents;
}
private string SignDkimForSmartermail( string contents )
{
Dkim dk = new Dkim( contents );
dk.Identity = "@mycompany.com";
//parse the private rsa key that was generated by openSSL.exe
//we can get it from the file
dk.DKCertificate = DKCertificate.ParsePemFile( "testing\\rsa.private" );
dk.SendingDomain = "mycompany.com";
//the dkim dns record selector (based upon our DNS server settings
dk.Selector = "testing";
dk.HeaderNamesOption = HeaderNamesOptions.AllMessageHeaders;
//sign the email contents
string signedContents = dk.SignMail();
return signedContents;
}
As always, if anyone has any questions, comments, or feature requests, feel free to let me know.
Thanks!
Dave Wanta