In Memory File Attachments to MailMessage

Encountered a problem when attaching file attachments to mail messages that are handled by a BizTalk orchistration.

Our application basically had a dynamically generated PDF document that needed to be attached to an e-mail and sent to an intake center. The funny business started when we got a call that nobody was getting our PDFs. After further investigate we found out they were getting our e-mails and our e-mails did have the attachments on them. They get our e-mail, with the attachment, so the problem is on their end! Hooray! Our job is done! Right?

Not really. The problem was that we did not explicitly set the Content Disposition Type, which is required by some e-mail readers (and apparently not provided by others, thus not required for some systems).

We had a BizTalk orchestration that was handling the e-mails and attempting to pull the attachments off and dump them into our document management system. This article by Scott Breeze saved the day. Apparently BizTalk is looking for some additional MIME properties to come through that are not provided (or necessary) with your basic MailMessage Attachment.

MailMessage m = new MailMessage();
foreach (SubmissionAttachment t in attachments)
{
    MemoryStream stream = new MemoryStream(t.FileContents);
    Attachment att = new Attachment(stream, t.FileName);
    m.Attachments.Add(att);
}

Changed to:

MailMessage m = new MailMessage();
foreach (SubmissionAttachment t in attachments)
{
    MemoryStream stream = new MemoryStream(t.FileContents);
    Attachment att = new Attachment(stream, t.FileName);
    att.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
    att.ContentDisposition.FileName = t.FileName;
    m.Attachments.Add(att);
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s