Asp.net 防止LinkedResource映像作为附件添加
在使用System.Net.Mail发送电子邮件的页面上,我有一个将图像嵌入html格式电子邮件的功能Asp.net 防止LinkedResource映像作为附件添加,asp.net,email,embedded-resource,Asp.net,Email,Embedded Resource,在使用System.Net.Mail发送电子邮件的页面上,我有一个将图像嵌入html格式电子邮件的功能 string logoPath = "W:\\WebSites\\logo.jpg"; LinkedResource imagelink = new LinkedResource(logoPath, "image/jpg"); imagelink.ContentId = "imageId"; imagelink.Trans
string logoPath = "W:\\WebSites\\logo.jpg";
LinkedResource imagelink = new LinkedResource(logoPath, "image/jpg");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MessageHeader + Message.ToString() + MessageFooter, null, "text/html");
htmlView.LinkedResources.Add(imagelink);
在创建电子邮件的代码中,图像是这样嵌入的
MessageHeader += "<img alt=\"Company Logo\" hspace=0 src=\"cid:imageId\" align=baseline border=0>";
MessageHeader+=”;
一切正常。该徽标显示在html格式的电子邮件中,它应该在哪里。但是,当用户收到电子邮件时,还会有一个附加图像-在Outlook中,您会看到一个始终称为ATT0001的附件-如果您下载,它就是徽标图像
如何防止徽标作为附件添加和嵌入?它看起来不专业,有标志的消息,但也附加等待下载。用户抱怨说他们认为有一个附件,但实际上它只是一个徽标。我想出了这个问题的解决方案。添加LinkedResource对象时,必须定义正确的MIME类型,如果这样做,电子邮件客户端将不会将嵌入的图像作为附件提供 下面是从我的场景中获取的示例代码(vb.net代码):
”您的电子邮件
Dim _htmlSource作为新的StringBuilder(“…您的HTML电子邮件…”)
Dim_linkedResources作为(LinkedResource的)新列表
Dim _href作为字符串=”http://your.image.url/image.png"
“添加图像
Dim_imageStream作为内存流
Dim\u mimeType作为字符串
使用_wc=新的网络客户端
_imageStream=新内存流(_wc.DownloadData(_href))
_mimeType=_wc.ResponseHeaders(“内容类型”)
终端使用
Dim\u contentId为String=Guid.NewGuid.ToString
_htmlSource.Replace(_href,“cid:&_contentId)
_添加(新的LinkedResource(_imageStream,新的ContentType(_MimetType))和{.ContentId=_ContentId})
'编译邮件消息
将邮件设置为新邮件消息
mail.IsBodyHtml=True
Dim_htmlView As AlternateView=AlternateView.CreateAlternateView-FromString(_htmlSource.ToString(),Nothing,MediaTypeNames.Text.Html)
_htmlView.transferncode=transferncode.EightBit
_linkedResources.ForEach(Sub(lr)\u htmlView.linkedResources.Add(lr))
我一直都知道。我一直认为这是因为我设置了一个默认情况下不显示图像的设置,但它们必须以某种方式包括在内。不过我不确定。你两个都有,图像和att?是的,我两个都有。该图像正确地嵌入到我的html电子邮件中,但在电子邮件客户端中查看时,它也显示为附件。我只是尝试将图像从jpg更改为png,但它没有连接。科技不是很棒吗!你找到解决办法了吗?我的代码过去工作正常,但由于某种原因,过了一段时间,它开始做与您描述的相同的事情。
'Your email
Dim _htmlSource As New StringBuilder("<html> ... YOUR HTML EMAIL ... </html>")
Dim _linkedResources As New List(Of LinkedResource)
Dim _href As String = "http://your.image.url/image.png"
'Adding images
Dim _imageStream As MemoryStream
Dim _mimeType As String
Using _wc = New WebClient
_imageStream = New MemoryStream(_wc.DownloadData(_href))
_mimeType = _wc.ResponseHeaders("content-type")
End Using
Dim _contentId As String = Guid.NewGuid.ToString
_htmlSource.Replace(_href, "cid:" & _contentId)
_linkedResources.Add(New LinkedResource(_imageStream, New ContentType(_mimeType)) With {.ContentId = _contentId})
'Compile mail message
Dim mail As New MailMessage
mail.IsBodyHtml = True
Dim _htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(_htmlSource.ToString(), Nothing, MediaTypeNames.Text.Html)
_htmlView.TransferEncoding = TransferEncoding.EightBit
_linkedResources.ForEach(Sub(lr) _htmlView.LinkedResources.Add(lr))