Nov 032010
 

I had a requirement to consume .PDFs and attach all of them that arrived within 30 minutes to an email with the PDFs as attachments.  I created a multi part message that I named msg_Email of type System.Xml.XmlDocument.  I created the message using a Construct Message shape.  I then loaded the body of the message using the .LoadXML() method.  I then used a helper class named EmailAttacher to attach the new messages needing to be attachments to the existing multi part email message.  Here is the helper class code.

namespace EmailAttacher
{
    public class MessageHelper
    {
        public static void AddAttachment(XLANGMessage destination, order check XLANGMessage attachment, case string filename)
        {
            try
            {
                int count = destination.Count;
                destination.AddPart(attachment[0], string.Format(“Attachment_{0}”, count));
                destination[count].SetPartProperty(typeof(MIME.FileName), filename);
            }
            finally
            {
                //decrement reference count
                destination.Dispose();
                attachment.Dispose();
            }
        }

}

The message assignment shape has the following code to add the newest message to the existing message.

EmailAttacher.MessageHelper.AddAttachment(msg_Email, msg_ToAddAsAttachment,”NameOfAttachedFile”);

Don’t forget to add this, to ensure all message parts that aren’t the body are attachments.

msg_Email(SMTP.MessagePartsAttachments) = 2;