472,989 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

System.Web.Mail embedding images?

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 = as****@domain.com;
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)

Nov 18 '05 #1
3 5636
"Tim T" <br*********@hotmail.com> wrote in
news:ed**************@tk2msftngp13.phx.gbl:
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

Nov 18 '05 #2
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

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1...
"Tim T" <br*********@hotmail.com> wrote in
news:ed**************@tk2msftngp13.phx.gbl:
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

Nov 18 '05 #3
"Tim T" <br*********@hotmail.com> wrote in
news:ur**************@TK2MSFTNGP10.phx.gbl:
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.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: prasanna | last post by:
Hello I tried to send mail from PHP in html format. But an ! mark appears in mail body in Inbox. I tried to print body text on page before sending mail and it does not show ! mark. I replaced !...
2
by: Dean J. Garrett | last post by:
Hi, I need to create HTML e-mail from an ASP program that includes embedded images that go along with the e-mail, e.g. logos, product image, etc. Is there a technique in HTML that people use to...
1
by: Will Pittenger | last post by:
I have a program where I used Win32 calls to add items to my form's system menu. Now I am attempting to have those items use the same icon that the corresponding toolbar button uses. I thought...
2
by: Ersin Gençtürk | last post by:
is it possible to embedd images as inline images into e-mail in pure asp.net ? I don't want my pictures sent like attachment , but I want to send them as inline pictures.Is there any way to do...
3
by: Ersin Gençtürk | last post by:
hi, which component do you prefer to send emails with embedded images ? Is there a freeware and native .net component available ? Note that I don't want to send images as attachments.
6
by: Edward | last post by:
I have been doing some research about embedding images in HTML using the data URL src method of the format: <img src="/-/data:image/gif;base64,<DATA>"> My question is, how does one generate...
1
by: MissMarie | last post by:
I've been playing around with DIV tables in myspace to better learn how to rewrite my own code for my business site without having to pay someone to design it. I've tried embedding a slideshow into...
0
digicrowd
by: digicrowd | last post by:
http://bytes.com/images/howtos/applemail_sig_icon.jpg At first glance, it may not appear that Apple Mail (otherwise known as Mail.app) supports the use of HTML signature emails. However, with a...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.