2.0 Mail attachment - Content-Type: multipart/related

  • Thread starter Thread starter Will
  • Start date Start date
W

Will

The new System.Net.Mail.MailMessage is awesome.... but ...

I can't seem to find a way to send an html email with an image attachment,
and group them together with Content-Type: multipart/related. The code I
have works great for including the HTML images in the email. However, in
Outlook Express it will automatically insert the image at the end of the
email.. so all images show up twice. This doesn't happen if I could nest the
HTML and image inside of a multipart/related boundary.

Here's the code:

using(MailMessage message = new MailMessage())
{
message.Subject = Subject_TextBox.Text;
message.Body =
@"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>My Email</title>
</head>
<body>
<img src='cid:image1.jpg' />
</body>
</html>";
message.IsBodyHtml = true;
message.From = new MailAddress("(e-mail address removed)", "Webmaster");
message.To.Add(new MailAddress("(e-mail address removed)"));
message.Attachments.Add(new Attachment(@"c:\image1.jpg"));
message.Attachments[0].ContentId = "image1.jpg";
message.Attachments[0].ContentDisposition.Inline = true;
message.Attachments[0].ContentDisposition.FileName = "image1.jpg";

SmtpClient mailClient = new SmtpClient();
mailClient.Send(message);
}
...
...
...
...
Here is what the message looks like: (without headers)

----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982
content-type: text/html; charset=us-ascii
content-transfer-encoding: quoted-printable

<html>...
....</html>
----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982
content-type: image/jpeg; name=image1.jpg
content-transfer-encoding: base64
content-id: <image1.jpg>
content-disposition: inline; filename=image1.jpg

/9j/4AAQSkZJRgABAQEAYABgAAD/4QAWRXhpZgAASUkqAAgAAAAAAAAAAAD/2wBDAAgGBgcG
....
TCM81IcYpntTAjbqKKcaKdguz//Z
----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982--

...
...
...
...
...


Here is what I'd like it to look like: (without headers)

----boundary_7_035ffa64-2aaf-4135-9f4f-9b36d877cdc4
Content-Type: multipart/related;
boundary="----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982"

----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982
content-type: text/html; charset=us-ascii
content-transfer-encoding: quoted-printable

<html>...
....</html>
----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982
content-type: image/jpeg; name=image1.jpg
content-transfer-encoding: base64
content-id: <image1.jpg>
content-disposition: inline; filename=image1.jpg

/9j/4AAQSkZJRgABAQEAYABgAAD/4QAWRXhpZgAASUkqAAgAAAAAAAAAAAD/2wBDAAgGBgcG
....
TCM81IcYpntTAjbqKKcaKdguz//Z
----boundary_6_bbca7c7e-266e-425a-bb11-62ad5d412982--

----boundary_7_035ffa64-2aaf-4135-9f4f-9b36d877cdc4--
...
...
...
...
...
...
Anyone know how to get this to happen?
Otherwise, I'm quite pleased with the new MailMessage setup.


Thanks!
-Will
 
ARGH! I'm 0 for 3...

That is, I've only had to ask 3 questions here my entire career and none
have got replies.

Do I smell or something? (that doesn't count as question #4)
-Will
 
I don't get replies either. But here's the best this I can sugest. You
already created the HTML body in message.body = @.... therefore you shouldn't
have to create the attachment add functionality. I would not do the HTML
body directly but let the message class build it by using
AutoGenerateHtmlBody = true. I think that is something to try.
Anyway it's a reply.
 
Back
Top