简体   繁体   English

如何删除图像作为附件,但显示在电子邮件正文中

[英]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? 如何阻止图像附加到电子邮件?

Use AlternateView to store your html code with image embedded as LinkedResource : 使用AlternateView存储您的html代码,其中图像嵌入为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);

PS Embedding image as base24 string is not very good idea, because many mail clients do not support such ability. PS嵌入图像作为base24字符串不是很好主意,因为许多邮件客户端不支持这种能力。

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") 它作为消息有效负载的一部分附加(无论是“显示为内联”还是作为真正的“附件”) - 或者在读取器读取电子邮件时从远程Web服务器获取(并且可选地必须选择“查看图片”)

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. 您必须在公共Web服务器上托管映像,以便打开邮件的读者可以访问它。
  2. You have to use a fully qualified URL in your message body source, so it can find it. 您必须在邮件正文源中使用完全限定的URL,以便它可以找到它。

Assuming you have stored the image on your web server at the following URL: http://www.example.com/images/myimage.jpg 假设您已将图像存储在Web服务器上的以下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. 可以使用的替代方法是嵌入图像,但通常也不会被电子邮件客户端过滤(现在通常就像垃圾邮件这样)您可以使用DataURL。

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

where <type> is the image type, ie jpg, gif,png, and is a base64 string. 其中<type>是图像类型,即jpg,gif,png,并且是base64字符串。 Just convert the image to a base64 string and assign it to the source using the above syntax 只需将图像转换为base64字符串,然后使用上述语法将其分配给源

For example, with a jpeg... 例如,使用jpeg ...

mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM