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(objec t sender, System.EventArg s e)
{
MailMessage objEmail = new MailMessage();
objEmail.From = as****@domain.c om;
objEmail.Subjec t = "news flash ";
string body = "<html>.... .
{some html code with images as <img src=\"news_r1_c 1.jpg\"> ... etc
</html>";
objEmail.Body = body;
objEmail.Priori ty = MailPriority.No rmal;
objEmail.BodyFo rmat = 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.Attach ments.Add(i1);
objEmail.Attach ments.Add(i2);
objEmail.Attach ments.Add(i3);
objEmail.Attach ments.Add(i4);
objEmail.Attach ments.Add(pictu re);
string sendto = Request.Form["recipients "];
string[] recipients= sendto.Split(', ',';');
foreach(string recipient in recipients)
{
objEmail.To = recipient.Trim( );
SmtpMail.SmtpSe rver = "smtp.server,co m";
try
{
SmtpMail.Send(o bjEmail);
}
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.AttachU RL 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.6 2C8CA30
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.E CF34990
Content-Type: image/jpeg;
name="news_r1_c 1.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)