Sending Emails in Biztalk
Biztalk is all about automation, while getting email notices from your system can be helpful and give you peace of mind about system status. There are basically two ways to send emails in Biztalk, one way is using SMTP adaptor through static port as you have figured out which is not different from other standard adapters like FILE or WCF-SQL, another way is using dynamic port.
The difference of static port and dynamic port:
Sending Emails through Static port:
Recipient: fixed address, sending email messages to ONE email address
Construction: through Biztalk Administration console, not hard coded, configurable for To,CC,Subject etc, you can choose to send messages as email body or attachments, and send port is agnostic to your code
Usage : sending error messages to development etc
How to: Just configure a static one way port using SMTP as type, PassThruTransmit as send pipeline, configuration of SMTP adaptor is all straight forward. Multipart messages will be packaged into multiple attachments if Attach All Parts is selected.
Sending Emails through Dynamic port:
Recipient: programmable addresses, you can get addresses from a Biztalk message
Construction: Through programming, you will have more control on construction of email
Usage: sending emails to customers etc
How to: here are steps of using dynamic port to send emails in Biztalk, names are just for demonstration purposes only
1. Create a send pipeline : SendPipeline1.btp
Drag MIME/SMIME encoder and drop it at Encode section of pipeline
Accept the default settings for MIME/SMIME encoder
SendPipeline1.btp will be used by the dynamic port
2. In orchestration Create MultipartType1
MessagePart_1 as the body, type could be your message type, or system one like System.Xml.XmlDocument, or System.String, here I use System.Xml.XmlDocument
One of the use of multi part message in Biztalk is sending different parts of a message as attachments in email, so you can add as many MessagePart as you like to MultipartType1, depends on need, you can also just have one.
As an example I have MessagePart_2 using System.String
3. As an example I have InMSG using type System.Xml.XmlDocument, it must be the same type as MessagePart_1 as it will be assigned to MessagePart_1, this is supposed to be your internal Biztalk message.
4. I have multipartMessage as MultipartType1, this message is going to be sent out in an email
5. A Construct Message Shape with a Message Assignment in it
Messages Constructed : multiPartMessage
Expression of message assignment:
multipartMessage.MessagePart_1= InMSG; multipartMessage.MessagePart_2="This is message part2 as a string"; multipartMessage(SMTP.Subject) ="Email From Dynamic Port"; multipartMessage(SMTP.From) ="email@example.com"; multipartMessage(SMTP.SMTPHost) ="yoursmypserver.com"; multipartMessage.MessagePart_2(MIME.FileName) = "Attachment_Name"; multipartMessage(SMTP.SMTPAuthenticate) =0;
As an example InMSG is assigned to MessagePart_1 this is the email body, other parts of email, here is MessagePart_2, will be attachments of email, and use MIME.FileName to set the attachment name.
6. Create a port with name : Send_SMTP_Port
Binding : Dynamic
Communication Direction: Send
Send Pipeline: SendPipeline1
Note that it is using SendPipeline1 we created earlier
7. Program the To address of Send_SMTP_Port in an Expression Shape after above Construct Message shape
8. A send shape and link it with Send_SMTP_Port Operation_1
9. That is all, as Send_SMTP_Port is a dynamic port, you cannot change anything though Administration Console and it is all hard coded in the orchestration
10. What is the difference of message type with System.Xml.XmlDocument and System.String
If type is System.Xml.XmlDocument the message will be accepted as IT IS, if it is a pure text, then it will just be text.
On the other hand if it is System.String, the serialization of it will be wrapped by string node. So the MessagePart_2 is like
<?xml version="1.0"?> <string>This is message part2 as a string </string>
So this tells us we had better use XmlDocument for body part to avoid Xml wrapping, and the message itself will be presented as IT IS.
There is also a custom serializer to RawString example you can get from Microsoft.