]> Forwarding on SMTP email through BizTalk Server 🌐:aligrant.com

Forwarding on SMTP email through BizTalk Server

Alastair Grant | Friday 22 November 2019

I have recently stumbled on some interesting behaviour on the BizTalk SMTP adapter, which I don't think is documented.

I was handling an untampered SMTP message (pass through receive location with no MIME decoding), sometimes known as a *.eml file.  These raw messages contain the headers, text, and attachments encoded with MIME.  As with other arbitrary file formats, you can handle these in Orchestrations by setting the message type to be System.Xml.XmlDocument, which BizTalk will treat as-is (even though there is no XML to be seen).

If you send this out via a SMTP Send Port, and have set the body of the message to be the body-part, you will receive an email which contains that raw SMTP text as an email with that as the content.  Not especially useful.

But here's the odd trick.  Consider this assignment on your outbound message:

msgEmailOut.Part(Microsoft.XLANGs.BaseTypes.ContentType) = "message/rfc822";

RFC 822 is an old RFC for defining early versions of the email format (and what we see over the wire in a SMTP data part).

Now instead of receiving the original email as a bunch of text in the body of a new email, the e-mail appears exactly as it did originally.  Further than that, it seems to work in a similar way to actual SMTP, as the headers are handled by the receiving mail server and a new Received: is appended to the header.

But now this is where it gets interesting.  Assume your original email was sent to bob@example.com, and your Send Port was configured to send to alice@example.com.  Your final message out of BizTalk will get delivered to both.

I found this strange, as in SMTP the target an email is delivered to is actually nothing to do with the To: field (which is why you can still get an email with a blank To: where everybody is BCC'd etc).  So I dug out my trusty Wireshark and had a look at what was going on.

What I found was the SMTP send adapter was interpreting the e-mail and sending the e-mail to all contacts in the To: field of the body (even though according to BizTalk, it's just an arbitrary blob of text).

The SMTP conversation looked something similar to this:

MAIL FROM: <sender@example.com>
250 2.1.0 Sender OK
RCPT TO: <alice@example.com>
250 2.1.5 Recipient OK
RCPT TO: <bob@example.com>
250 2.1.5 Recipient OK

In this scenario, this basically made BizTalk another SMTP hop.  In the situation where BizTalk is reading from a mailbox it is actually very frustrating as despite changing the To: address in the Send Port, the mail is sent to the original destination too - which lands up in an endless loop of BizTalk forwarding e-mails to itself.

But this could be very useful if you're wanting to craft outbound e-mails in a more dynamic fashion.

Breaking from the voyeuristic norms of the Internet, any comments can be made in private by contacting me.