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
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