Adding attachment into MailMessage direct from MIME string

718 Views Asked by At

There are a number of good examples of adding attachments from file, but I was wondering if I could do it in a similar way to the way AlternateView.CreateAlternateViewFromString works.

I am separately using IMAP to get the body_text and body_headers of an email from one address and then wish to send it from another (via SMTP) with some changes made, but the attachment added as faithfully as possible. I am using MailMessage and SmtpClient ( System.Net & System.Net.Mail )

I have written code to extract MIME parts from BODY[TEXT] by their "Content-Type:" tag and happily add plain text the html views this way. I have the attachment in the MIME too (see example below) so it seems like I should be able to easily add the attachment directly from the string I can extract from the MIME with "Content-Type: application/octet-stream". At present, I am only interested in it working with application/octet-stream.

I have this available in parts of my code as a string containing either:

Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0

YWJjIGZpbGU=

Or

Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0

YWJjIGZpbGU=

From example:

* 53 FETCH (BODY[TEXT] {614}
--001a1140ae52fcc4690558c101ec
Content-Type: multipart/alternative; boundary="001a1140ae52fcc4630558c101ea"

--001a1140ae52fcc4630558c101ea
Content-Type: text/plain; charset="UTF-8"

Blah blah

--001a1140ae52fcc4630558c101ea
Content-Type: text/html; charset="UTF-8"

<div dir="ltr">Blah blah</div>

--001a1140ae52fcc4630558c101ea--
--001a1140ae52fcc4690558c101ec
Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0

YWJjIGZpbGU=
--001a1140ae52fcc4690558c101ec--
)
$ OK Success

All of the other aspects of my code work, but is there an easy way to directly add the attachment from the MIME data I have. I am not using MimeKit or any libraries other than standard VS ones.

Thank you for reading this long question.

1

There are 1 best solutions below

0
On

Not really what I wanted to do, but I achieved what I needed as follows:

string name = find_item_by_key(attachment_content, "filename"); // looks for key="value" and returns value
string attachment_base64 = attachment_content.Substring(attachment_content.LastIndexOf("\r\n\r\n"));
attachment_base64 = attachment_base64.Trim('\r', '\n');
byte[] attachment_bytes = Convert.FromBase64String(attachment_base64);
Attachment att = new Attachment(new System.IO.MemoryStream(attachment_bytes), name);
mail.Attachments.Add(att);

Lots of pointless conversions going on, since the data is already Base64 and mail.Attachments.Add will no doubt be converting it back again.