0 votes
by (880 points)

I'm having problems adding eml files as attachments.

1) If I add the eml file in the default way, the message is added as a MIME part without the specified filename.

2) Because of memory issues, we use Attachment.Options = DoNotPreloadAttachments. This way, an attachment with filename is added, but it is empty (in contrast to attachments with other content types).

3) If I set the content type to something different than "message/rfc822" (e.g. "text/plain"), it works correctly.

What can be done? And what is the "correct" content type to add an eml file as a file attachment (not inline message), without zipping it or doing other ticks?

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (144k points)
selected by
 
Best answer

1) I'm not sure what you mean by default way, but the following code seems to do what you need:

MailMessage mail = new MailMessage();
mail.Subject = "Message subject";
mail.BodyText = "Message text";
mail.Attachments.Add(new Attachment(@"c:\data\attachment.eml"));
mail.Save(@"c:\data\message.eml");

2) Using DoNotPreloadAttachment option with embedded messages is not an intended scenario, but specifying DoNotParseMimeTree options as well works around the unintended behavior. Please give this a try:

var mail = new MailMessage();
mail.Subject = "Message subject";
mail.BodyText = "Message text";

var attachment = new Attachment();
attachment.Options |= MimeOptions.DoNotPreloadAttachments | MimeOptions.DoNotParseMimeTree;
attachment.SetContentFromFile(@"c:\data\attachment.eml", "attachment.eml", "message/rfc822");

mail.Attachments.Add(attachment);
mail.Save(@"c:\data\message.eml");

3) Content type of "message/rfc822" implies that the MIME node is an embedded message, while "text/plain" implies it's a plain text attachment. I'm not aware of any content type to specify an message as file attachment, but a quick test reveals that clients such as Outlook use the generic attachment content type of "application/octet-stream" for this.

Please give the code above a try. If it actually does what you need, please let us know what you were doing differently. Thanks!

by (880 points)
The code above works (that's what I meant with default way), but we cannot use it, since some of our customers tend to send large attachments with their mals (up to several 100 MB). That's why we use the DoNotPreloadAttachments option, which works fine, except for "message/rfc822" attachments. Just give it a try, the attachment will be there, but empty, after calling Attachments.Add with that option enabled (at least it is for me).

Manually specifying the "application/octet-stream" does work, and we'll probably stick to that for now, though I've read that some systems (e.g. Lotus Notes) cannot open mail attachments not specified as "message/rfc822" correctly (for whatever reason).
by (144k points)
Thanks!

1) The "default way" code above actually does add the filename to the attachment MIME part. Have you been doing anything differently?

2) DoNotPreloadAttachment option was intended to be used for attachments. It does not play well with embedded messages that we actually parse in addition to loading. However, enabling DoNotParseMimeTree option in addition to DoNotPreloadAttachments seems to solve this issue. I have added the sample code to my answer above, please give it a try.

3) When "application/octet-stream" is specified, most mail client apps use the attachment's extension to determine the file type or launch a default associated application through the OS. But Lotus Notes probably treats the attachment just as uknown data. Which is technically correct as well, although it's inconvenient.
by (880 points)
Thanks, adding DoNotParseMimeTree seems to do the trick :-)
...