How to send an email from a BizTalk Expression shape (without using the SMTP Send Ports).
Note: There are two mail objects, the older one is System.Web.Mail and the newer one It also shows how to connect to an SMTP Host (Server) that requires userid/password authentication.

Note: The SMTPHost, from email, user/password were retrieved in the previous expression shape from the <AppSettings> of the BTNTSVC.exe.config file.

objMail = new System.Net.Mail.MailMessage();

objMailAddressFrom = new System.Net.Mail.MailAddress(strMailFrom);
objMail.From = objMailAddressFrom;

objMailAddressTo = new System.Net.Mail.MailAddress(msgEmailFromSQL.EmailAddress);

objMail.Subject = msgEmailFromSQL.Status_Code.Description;

objMail.Body =
    msgEmailFromSQL.Status_Code.Text +
    "&lt;BR&gt;&lt;BR&gt;" +
    "&lt;a href='" + msgEmailFromSQL.WebPageAddress + "'&gt;" +
    msgEmailFromSQL.WebPageAddress + "&lt;/a&gt;";

objMail.IsBodyHtml = true;

objCredentials = new System.Net.NetworkCredential(strMailUser, strMailPassword);
objSmtpClient = new System.Net.Mail.SmtpClient(strMailServer);
objSmtpClient.Credentials = objCredentials;


XML Serializer – Sample Code – How to take a C# object and write it to a file in XML “serialized” format.

NOTE: Serialize means to take an object (in memory) and turn it to a format that can be preserved to disk (or sent over a wire). Deserialize means to take the serialized-format, and turn it back into an object (in memory).

Calling Program – to use Subroutine in Next Code Sample Below

using System.Xml.Serialization;
using System.IO;
using System.Xml; 

//  Serialize an object in memory to disk. 

            XmlSerializer xs1 = new XmlSerializer(typeof(YourClassName));
            StreamWriter sw1 = new StreamWriter(@"c:DeserializeYourObject.xml");
            xs1.Serialize(sw1, objYourObjectFromYourClassName);

// Now to deserialize it - assuming this might be a different program, run later.

            XmlSerializer xs2 = new XmlSerializer(typeof(YourClassName));
            StreamReader sr2 = new StreamReader(@"c:DeserializeYourObject.xml");
            // deserialize and cast back to proper class
            objYourObjectFromYourClassName = (YourClassName) xs2.Deserialize(sr2);
            Console.WriteLine ("Deserialize2 completed");

NOTE: Serializing to a string in memory is a lot trickier than you might think, because you have to deal with byteArrays, and encoding. Here’s a link to a page that explains how to do it:

XMLHelper Class Library – with SerializeObject and DeserializeObject

Based on the above article, I put the following code in my “Common” C# Component library that is deployed on all my systems:

        /// <summary>
        /// Method to convert a custom Object to XML string
        /// </summary>
        /// Object that is to be serialized to XML
        /// XML string
        public static String SerializeObject(Object pObject)


                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(pObject.GetType());
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

                xs.Serialize(xmlTextWriter, pObject);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;


            catch (Exception e)
                return null;

        /// <summary>
        /// Method to reconstruct turn XMLString back into Object
        /// </summary>

        public static Object DeserializeObject(String pXmlizedString, Type classType)

            XmlSerializer xs = new XmlSerializer(classType);
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return xs.Deserialize(memoryStream);


        private static String UTF8ByteArrayToString(Byte[] characters)
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);

        private static Byte[] StringToUTF8ByteArray(String pXmlString)
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;

Just wrap the above with your class name, and add these using statements:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Xml.Schema;
using System.IO;
using System.Xml.Serialization;
using System.Text;

Added June 2020

Example C# Class To Serialize

The object to be serialized is created from the PurchaseOrder850 class below. This class happens to represent a EDI 850 Purchase Order, but same concept works with any class. This is shows a two level hierarchy, where one Purchase Order has many line items. So the first class (PurchaseOrder850) references the second class (PurchaseOrder850LineItem).

    public class PurchaseOrder850
        public string PONum;
        public string PODateText;
        public DateTime PODate; 
        public string POType;
        public string VendorNumber;
        public string BuyerName;
        public string BuyerTelephone; 
        public List<PurchaseOrder850LineItem> LineItems; 
    public class PurchaseOrder850LineItem
        public string lineItem;
        public int quantity;
        public string uom;
        public decimal price;
        public string basisOfUnitPrice;
        public string catalogNumber;
        public string description;
        public string dateRequiredTxt;
        public DateTime dateRequired; 

Example Serialized XML

<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrder850 xmlns:xsi="" xmlns:xsd="">
  <BuyerName>JANE WATSON</BuyerName>
      <description>CABLE RETAINER CLIP PLASTIC</description>
      <description>T-BAR *** BOX HANGER</description>