Sending file as attachment using BizTalk Orchestration | How to send email with attachment in BizTalk orchestration

{tocify} $title={Table of Contents}


The Simple Mail Transfer Protocol (SMTP) adapter is used to exchange information between a server running Microsoft BizTalk Server and other applications by means of the SMTP protocol. 

BizTalk Server can send messages to other applications by creating an e-mail message and delivering it to a specified e-mail address. 

BizTalk supports both ways - Static configuration and Dynamic configuration of SMTP adapter.

You would need to send email from Orchestration for various scenarios like dynamic mailto address/ Subject/Body or in workflow notification is to be send  where Subject changes depending upon  various scenarios ex. on success -- Subject is to be sent is "Transaction/Process completed successfully" and in error scenario "Transaction/Process failed" etc.

To demonstrate how to configure SMTP Port dynamically and how to add attachments, have used following scenario:


Keeping it simple, you receive a message which has details of email to be sent in it. You construct email message and configure SMTP adapter using those details.

Building the Solution

Create schema for message holding email details

create schema

Create orchestration

Create an orchestration containing a Receive shape to receive Email_Details message from a static one way Receive port  followed by a Construct message shape containing a Message assignment shape where SMTP adapter is configured dynamically, and a send shape to send Email message through Dynamic port(code below). 

We also need to create two message, one for the input message based on Email Details schema and one for the output message based on String datatype.

create orchestration

SMTP.Attachments is the property available with SMTP adapter which provides a way to pass the path of files to be attached. 

If more than one fie is to be attached then, paths can be concatenated using pipe(|).

Constructing Email message and configuring dynamically SMTP server

MSG_Email= System.String.Empty;

MSG_Email(SMTP.Subject) = MSG_EmailDetails.Subject;

MSG_Email(SMTP.From) = MSG_EmailDetails.FromAddress;

MSG_Email(SMTP.SMTPHost) = "DemoMachinePM";

MSG_Email(SMTP.EmailBodyText) = MSG_EmailDetails.Body;

MSG_Email(SMTP.EmailBodyTextCharset) = "UTF-8";

MSG_Email(SMTP.Attachments) = MSG_EmailDetails.Attachments;

//For sending email to cc address use below line of code

MSG_Email(SMTP.CC) = MSG_EmailDetails.CcAddress;

MSG_Email(Microsoft.XLANGs.BaseTypes.ContentType) = "text/plain";

Port_Dynamic_SendEmail(Microsoft.XLANGs.BaseTypes.Address) = "mailto:"+MSG_EmailDetails.ToAddress;

I have set MSG_Email as System.String, as I don't intent to do anything with BizTalk message(MSG_EmailDetails). 

In case I needed to send the BizTalk message as an attachment then I would set MSG_Email as System.Xml.XmlDocument and assign the MSG_EmailDetails to it.

MSG_Email = MSG_EmailDetails;

And to send this also as an attachment would add following code

MSG_Email(SMTP.MessagePartsAttachments) = 1;

The send port used to send email is set as Dynamic one way (only send) with Pass Thru pipeline and address is set to toAddress.

Deploy and Test

Sign the application and deploy it, create a Recieve port with Receive Location having XmlReceive Pipeline. Send port would be already available as it is created while deploying solution because of it's dynamic binding.

To test the application, create the instance of MSG_EmailDetails and populate it with values and drop the file at location which is binded to the receive location.

Case 1: Input file (with one attachment info)

<ns0:Email xmlns:ns0="http://EmailAttachmentDemo.EmailDetails">




  <Subject>Email with attachment from demo server</Subject>

  <Body>Dynamic Email with attachment from demo server.</Body>



Output (Email delivered with one attachment)
Email delivered with one attachment

Case 2: Input file (with two attachment info)

In order to sent multiple attachment, '|' (pipe) is used as delimeter(marked in red below sample)

<ns0:Email xmlns:ns0="http://EmailAttachmentDemo.EmailDetails">




  <Subject>Email with attachment from demo server</Subject>

  <Body>Dynamic Email with multiple attachment from demo server.</Body>



Output (Email delivered with two attachment)
Email delivered with two attachment
Case 3: Sending Email Message also as an attachment

For sending MSG_Email message as attachment use below line of code


               MSG_Email(SMTP.MessagePartsAttachments) = 1;

As can be seen mail is received by address in cc along with EmailMessage as attachment too

1.The location where attachments are, should be accessible
2.Toemail address must not be empty and its size must not exceed 256 characters
3.CCemail address size must not exceed 1024 characters

Download Sample

If you have questions or suggestions, feel free to do in comments section below !!!

Do share if you find this helpful .......
                          Knowledge Sharing is Caring !!!!!!


If you have any suggestions or questions or want to share something then please drop a comment

  1. Is your download code sample still available, link goes to a generic search page.

Previous Post Next Post