11

I found this solution for showing an image in the body of the email: Add image to body of an email

And it works fine but it also adds the image as an attachment to the email.

Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;

//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";

There is a line of code with the comment to display as inline and not as attachment but this line isn't working because the image still gets attached to the email:

//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

How can I stop the image from attaching to the email?

Community
  • 1
  • 1
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • 1
    Please see this post : http://stackoverflow.com/questions/18358534/send-inline-image-in-email – Creep Jan 05 '17 at 15:20
  • 1
    is the L missing from ` a typo in `mailMsg.Body = " ";` line, or is it present in your main app? – Takarii Jan 05 '17 at 16:06
  • 1
    Take a look at [this post](http://stackoverflow.com/a/32767496/3110834) for some options. – Reza Aghaei Jan 05 '17 at 23:33
  • you cannot remove image attachment to the email. The only thing you can do is to convert the image attachmento to base64 and include in the html source. – hpfs Jan 08 '17 at 15:09

3 Answers3

5

Use AlternateView to store your html code with image embedded as LinkedResource:

string contentID = "Image";

var inlineLogo = new LinkedResource(EmailLogo.ImageUrl, "image/png");    
inlineLogo.ContentId = contentID;

var htmlView = AlternateView.CreateAlternateViewFromString(
    "<html><body> <img height=\"30\" width=\"30\" src=\"cid:" + contentID + "\"> </body></html>",
    Encoding.UTF8, "text/html");
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
htmlView.LinkedResources.Add(inlineLogo);

mailMsg.AlternateViews.Add(htmlView);

P.S. Embedding image as base24 string is not very good idea, because many mail clients do not support such ability.

arbiter
  • 9,447
  • 1
  • 32
  • 43
2

If you want to display an image in an email it has to exist somewhere. It is either attached as part of the message payload (regardless of whether it is "displayed inline" or as a true "attachment") - or is fetched from a remote web server when the reader reads the email (and optionally has to choose to "view images")

To not attach the image to the email payload itself:

  1. You have to host the image on a public web server so that the reader opening the message can access it.
  2. You have to use a fully qualified URL in your message body source, so it can find it.

Assuming you have stored the image on your web server at the following URL: http://www.example.com/images/myimage.jpg

... then your source should simply change to reflect:

mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"http://www.example.com/images/myimage.jpg\"> </body></html>";

No need to attach it at all.

Anthony Gray
  • 344
  • 4
  • 7
1

An alternative that can be used which embeds the image inline, but also isnt generally filtered by email clients is (which is generally the case today in things like junk mail) You could use a DataURL.

<img src="data:image/<type>;base64,<string>"/>

where <type> is the image type, ie jpg, gif,png, and is a base64 string. Just convert the image to a base64 string and assign it to the source using the above syntax

For example, with a jpeg...

mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";
Takarii
  • 1,612
  • 2
  • 18
  • 29