System.Web.Mail embedding images?

T

Tim T

Hi, I hope there is someone reading this that has the answer, Please Help!

I have the need to send a html email via asp.net. its easy enough to send
an html email and add attachments.
My question is, how to you set the Content-Location of each attachment in
the mail headers so that the images are embedded in the html email rather
than attached?

This is my code:

private void Page_Load(object sender, System.EventArgs e)
{

MailMessage objEmail = new MailMessage();

objEmail.From = (e-mail address removed);
objEmail.Subject = "news flash ";
string body = "<html>.....
{some html code with images as <img src=\"news_r1_c1.jpg\"> ... etc
</html>";
objEmail.Body = body;
objEmail.Priority = MailPriority.Normal;
objEmail.BodyFormat = MailFormat.Html;

MailAttachment i1 = new
MailAttachment(Server.MapPath("images/news_r1_c1.jpg"));
MailAttachment i2 = new
MailAttachment(Server.MapPath("images/news_r1_c3.gif"));
MailAttachment i3 = new
MailAttachment(Server.MapPath("images/spacer.gif"));
MailAttachment i4 = new
MailAttachment(Server.MapPath("images/news_r4_c1.jpg"));
MailAttachment picture = new
MailAttachment(Server.MapPath("images/default.jpg"));

objEmail.Attachments.Add(i1);
objEmail.Attachments.Add(i2);
objEmail.Attachments.Add(i3);
objEmail.Attachments.Add(i4);
objEmail.Attachments.Add(picture);

string sendto = Request.Form["recipients"];
string[] recipients= sendto.Split(',',';');
foreach(string recipient in recipients)
{
objEmail.To = recipient.Trim();
SmtpMail.SmtpServer = "smtp.server,com";

try
{
SmtpMail.Send(objEmail);
}
catch (Exception exc)
{
Response.Write("Send failure: " + exc.ToString());
}
}
}

as said, html email is coming through fine, images are in email as
Attachments, but DON'T display inline with the page (even though the
attachments are named correctly corresponding to the image names in the HTML
(<img src="imagename.jpg"> where imagename.jpg is attached).

I have sent HTML emails with embedded images in Classic ASP using CDONTS
quite easily, i need to do the same in .NET!
[FYI code to attach in ASP is:
objMail.AttachURL server.MapPath("images/imagename.jpg"), "imagename.jpg"
]

a quick look at the message source shows this at the top of the part that
holds the binary information about the image, in the email source:

for the ASP version (that works) :

------=_NextPart_000_000A_01C3EF89.62C8CA30
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment
Content-Location: news_r1_c1.jpg

in the ASP.NET version (doesn't show images inline)

------=_NextPart_000_005D_01C3F184.ECF34990
Content-Type: image/jpeg;
name="news_r1_c1.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="news_r1_c1.jpg"

notice how there is no Content-Location with the image name in the .NET
version, this is obviously needed to tell the mail client where the attached
images should be displayed in the HTML email.

Now does anyone know how to set this in the .NET mail object??
any help/leads in the right direction are appreciated.
The doesn't seem to be anywhere to set this property in the MailAttachment,
or MailAttachments.Add methods, and i couldn't find any answers in the docs

Please help

Thanks
Tim

PS. (although i'm using c#, vb code will do if you code in VB.NET)
 
C

Chad Z. Hower aka Kudzu

Tim T said:
I have the need to send a html email via asp.net. its easy enough to
send an html email and add attachments.
My question is, how to you set the Content-Location of each attachment
in the mail headers so that the images are embedded in the html email
rather than attached?

System.WebMail does not support alternative types which is the proper way to
send HTML. If you send as attachments not all mail clients will read it, and
many spam filters will kill your mail.

Indy supports HTML mail thoguh, and its free.

http://www.indyproject.org/
string body = "<html>.....
{some html code with images as <img src=\"news_r1_c1.jpg\"> ... etc

You didnt send this right. You are just stuffing HTML into a text body. This
will trigger spam filters like crazy, and very few mail clients will display
it.

You have to send a properly formatted multi part altnernative message.
MailAttachment i1 = new
MailAttachment(Server.MapPath("images/news_r1_c1.jpg"));

You dont send them as attachments. You send them as message parts instead
with Content ID headers.
as said, html email is coming through fine, images are in email as
Attachments, but DON'T display inline with the page (even though the
attachments are named correctly corresponding to the image names in the
HTML (<img src="imagename.jpg"> where imagename.jpg is attached).

Thats becauase you are not formatting the mail according to standards.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
T

Tim T

Thanks for that Chad, now it certainly helped in my understanding on how to
send html emails properly.
I had a look at Indy, and couldn't find anything on sending email using .NET
do you know of any example c# code to do what i need to do properly?
I downloaded this .NET component that does it propertly:
http://www.waldorf.uklinux.net/index.php
just wondering if anyone knows if there is any good source code examples out
there to do this??

Thanks again

Tim
 
C

Chad Z. Hower aka Kudzu

Tim T said:
Thanks for that Chad, now it certainly helped in my understanding on how
to send html emails properly.

Your welcome.
I had a look at Indy, and couldn't find anything on sending email using
.NET do you know of any example c# code to do what i need to do

http://www.atozed.com/indy then click demos.

Its just plain text, but google will turn up a bunch of Delphi code that
shows you how to do the HTML parts.
properly? I downloaded this .NET component that does it propertly:
http://www.waldorf.uklinux.net/index.php

If it works for you.. but it looks a bit limited even by its own admissions.
Im also not sure that its doing the HTML mails properly. If its not, you are
going to get filtered a LOT by spam tools.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top