Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove image as attachment but show in body of email

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?

like image 806
user123456789 Avatar asked Jan 03 '17 14:01

user123456789


People also ask

Why are my attachments showing up in the body of the email?

If the message format is HTML or plain text, attachments appear in the attachment box below the Subject line. If the message format is Rich Text, attachments appear in the body of the message.

How do I get an attachment to show in the body of an email?

If you want to always insert attachment in email body while editing email, you can click File > Options > Mail and select Rich Text in the drop down list of Compose message in this format in Compose message section.

How do I stop Outlook from showing attachments in the body of an email?

Once there, simply choose “Mail” from the left menu and then choose “Layout” in the subsequent menu. Navigate to Mail > Layout 4. Turn off inline previews for attachments and save: The final step is to turn off inline previews in the Outlook.com mail settings.


3 Answers

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.

like image 84
arbiter Avatar answered Oct 17 '22 04:10

arbiter


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.

like image 33
Anthony Gray Avatar answered Oct 17 '22 05:10

Anthony Gray


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>";
like image 1
Takarii Avatar answered Oct 17 '22 06:10

Takarii