Geeks With Blogs

Charles Young

A few weeks ago I was trying to send rich email messages from an orchestration using the SMTP adapter.   It was simple to pass messages via the adapter, but the ContentType setting in the SMTP header was always either text/plain or text/xml, depending on the type of pipeline selected.   For HTML mail, you really want to pass a MIME multipart message with plain and HTML parts.   I guessed this might be possible using a bespoke pipeline and the MIME/SMIME encoder, but didn't have time to experiment, so I  abandoned using the adapter in favour of using .NET's SmtpMail class instead, which all worked very well.

The latest (April 2004) release of the documentation for BizTalk 2004 directly addresses the issue of sending HTML mail via the SMTP adapter.   Although the adapter itself doesn't know anything about HTML mail, you can indeed do this by using the MIME/SMIME encoder in your own send pipeline.   However, you also have to know about another slightly obscure feature, which is that you can set a ContentType property on individual parts of a Multi-Part message in orchestration.   There is sample code in the new documentation showing you how to do this.   By creating a multi-part message in orchestration, setting the ContentType properties of each part programmatically and then encoding the outbound message using MIME before submitting to the SMTP adapter, you should be able to provide rich email message to your users.

There is one other issue you must address.   By default, message part data is serialised into XML (presumably using the .NET XmlSerializer class).   A simple approach to sending HTML mail is therefore to type your body message part as an XmlDocument, and build your HTML as XML (e.g., as XHTML).   However, if you want to follow best practice, and have a plain text part as well as an HTML part, or if you want to use HTML that isn't well-formed XML, you have to use a different form of serialisation.   Microsoft's documentation includes some sample code for building a .NET class and an associated formatter.   By annotating the class with the CustomFormatter attribute, you can get BizTalk to serialise your data using the formatter rather than the Xml Serialiser.   This is really powerful stuff, because it means, for example, that you can create non-XML messages in orchestration and pass them straight out of a Send port without further processing.   Microsoft includes code for a RawString class that will allow BizTalk to serialise your message parts directly into raw strings rather than XML.  As we BTS2004 developers have now come to expect, the use of custom formatters is all rather poorly documented.  For example, read the Remarks section of the CustomFormatterAttribute class, and see if you think it makes much sense.   The first sentence starts well, but then somehow the technical authors fail to explain clearly that the whole point of the attribute is to move beyond the strictures of XML serialisation.   Oh dear.

There is another minor issue with the code.  It uses the typeof() operator to return the type of the Microsoft.XLANGs.BaseTypes.ContentType class within a call to the SetPartProperty method of the message part.   However, this class is suppressed within IntelliSense in the expression designer.  This appears to be because Microsoft has annotated the class as representing a property in much the same way as they annotate the message property classes in Microsoft.BizTalk.GlobalPropertySchemas.  

Unfortunately, unlike message properties, XLANG/s doesn't appear to allow you to set the ContentType property on a message part in the same way (e.g., by using something like msgMyEmailMessage.partHTML(Microsoft.XLANGs.BaseTypes.ContentType) = "text/html"; ).   You can never see the ContentType class in IntelliSense.  You just have to know that it is there.   Nice!

Of course, to send rich email, you could simply use the .NET SmtpMail class within your orchestration code (my original strategy), which may be good enough and a bit easier.

Posted on Tuesday, April 13, 2004 12:20 PM BizTalk Server 2004/2006 | Back to top

Comments on this post: Sending rich HTML mail via the SMTP adapter

# How messages work in Orchestrations
Requesting Gravatar...
The finer points of message handling in Biztalk Server 2004 are not always well understood. This article attempts to shed some light on how messages are handled, and the more confusing aspects of message initialisation.
Left by Charles Young on May 05, 2004 11:51 AM

# re: Sending rich HTML mail via the SMTP adapter
Requesting Gravatar...
<H2> hello <h2>
Left by on Jul 09, 2004 11:21 PM

# re: Sending rich HTML mail via the SMTP adapter
Requesting Gravatar...

Hope a sample code will give a better idea instead of explaining it.

Left by Mangai on Apr 15, 2005 7:02 AM

# re: Sending rich HTML mail via the SMTP adapter
Requesting Gravatar...
Hi Charles,

I am using the same sample for sending HTML email. But I need to know how to catch SMTP Send Port failure exceptions(Error 550 unknown user account) in the Orchestration.
How can these exceptions be caught in Dynamic SMTP send port ?

Left by Pankaj on Oct 24, 2005 3:36 AM

Your comment:
 (will show your gravatar)

Copyright © Charles Young | Powered by: