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?
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.
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.
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.
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.
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:
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.
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>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With