Richard Seroter has a good post here on using the improved SMTP adapter in BizTalk Server 2006 and the options it provides. One question he asks is how to use it to send HTML-formatted messages but using dynamic content, instead of just a static one.
I've managed to do this successfully using our good old RawString friend and multi-part message types. For this, I create a multi-part message type (let's call it EMailType) and adding two parts for it: the first one will hold the email's body (EmailBody, marked with Message Body part=true) and is of type RawString, while the second holds the actual original BizTalk message you want to add as an attachment (let's call it OriginalMessage).
Then, all I have to do is create a new message of type EMailType and initialize it in a message assignment shape like this:
msgbody = new System.Text.StringBuilder();
msgbody.AppendLine("");
msgbody.AppendLine("
The attached document failed on processing in BizTalk Server.
");
msgbody.AppendLine("
Error details:
");
msgbody.AppendLine("Error Description: " + FailedMessage(ErrorReport.Description) + "
");
//msgbody.AppendLine("Message Type: " + FailedMessage(ErrorReport.MessageType) + "
");
msgbody.AppendLine("Error Type: " + FailedMessage(ErrorReport.ErrorType) + "
");
msgbody.AppendLine("Failure Category: " + System.Convert.ToString(FailedMessage(ErrorReport.FailureCategory)) + "
");
msgbody.AppendLine("Failure Code: " + FailedMessage(ErrorReport.FailureCode) + "
");
msgbody.AppendLine("Receive Port Name: " + FailedMessage(ErrorReport.ReceivePortName) + "
");
msgbody.AppendLine("Receive Location: " + FailedMessage(ErrorReport.InboundTransportLocation) + "
");
msgbody.AppendLine("");
EMail.EmailBody = new Microsoft.Samples.BizTalk.XlangCustomFormatters.RawString(msgbody.ToString());
EMail.EmailBody(Microsoft.XLANGs.BaseTypes.ContentType) = "text/html";
EMail.OriginalMessage = FailedMessage;
EMail(SMTP.Subject) = "Failed error message notification";
EMail(SMTP.From) = "someone@somewhere.com";
EMail(SMTP.EmailBodyFileCharset) = "UTF-8";
EMail(SMTP.SMTPHost) = "localhost";
EMail(SMTP.MessagePartsAttachments) = 2;
As you can probably guess, this is an HTML version of the HandlingFailures sample included with BizTalk 2006. The key parts to notice here is how we just create into a StringBuilder the entire HTML message content, construted dynamically. Once that's done, we assign the resulting string to the RawString object in out EmailBody member of the email and set the part's ContentType Context property to "text/html". Do notice we assign the PART's context property, not to the entire message. The second thing to notice is that we configure the SMTP.MessagePartsAttachments context property of the message to 2, which tells the SMTP adapter to use the part marked with Message Body Part=true as the email's body, and attach all remaining parts as message attachments.
You can download the entire example here. Notice you'll need to get the RawString project from the BTS documentation, though. (Speaking of which, I really think RawString should be part of the core BizTalk API, it's too useful not to).