views:

1488

answers:

5

SmtpClient() allows you to add attachments to your mails, but what if you wanna make an image appear when the mail opens, instead of attaching it?

As I remember, it can be done with about 4 lines of code, but I don't remember how and I can't find it on the MSDN site.

EDIT: I'm not using a website or anything, not even an IP address. The image(s) are located on a harddrive. When sent, they should be part of the mail. So, I guess I might wanna use an tag... but I'm not too sure, since my computer isn't broadcasting.

A: 

What about converting images in Base64 strings? AFAIK this can be easily embedded within mail body.

Take a look here.

m.bagattini
I'm still convinced this has to be considered as an option..http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html
m.bagattini
The image will be base64 encoded anyway, that's nothing to do with making it appear within an HTML email. The -1 wasn't from me but the answer is a little wide of the mark.
Lazarus
All I want to say is that if you want to embed an image inside an HTML document, you can. Just encode it in base64 and place the resultant string inside the src attribute of img tag.Take a look here: http://www.tekasarts.com/stuff/base_64_img.htmlWhat I did was:- gradient-fill a gif in photoshop- Convert.ToBase64String() image bytes- paste to an HTML documentAs far as I can understand this may not be a preferred solution but it's in topic and in a certain way a pretty good solution.
m.bagattini
A: 

The process of making an image appear on the client when the mail is opened is a client function. As long as the client knows how to render the image & has not blocked any image content it will open it right away. You do not have to do anything special while sending the email to make it open on the client as long as you have correctly specified the image mime attachment type.

msvcyc
Would someone care to explain the reason for the downvote?
msvcyc
Read the question again. He wants to attach the image and show it in the body.
Thomas Freudenberg
+4  A: 

The HTML Email and the images are attachments so it's just a case of referring to the image(s) by their content ids, i.e.

    Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text)
    Dim RGen As Random = New Random()
    A.ContentId = RGen.Next(100000, 9999999).ToString()
    EM.Body = "<img src='cid:" + A.ContentId +"'>"

There seems to be comprehensive examples here: Send Email with inline images

Lazarus
A: 

I just send the email as HTML and insert an img tag, relating back to the website.

andrewWinn
+3  A: 

When you say 4 lines of code, are you referring to this?

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;
Brandon
That looks like it... but there was more to it I think...
WebDevHobo