在电子邮件中插入本地图像

时间:2013-10-01 16:00:50

标签: html .net image email

我想将图片插入HTML格式的电子邮件中。我可以看到直接嵌入图像不会在Outlook中产生好的结果,所以我必须找到另一种方法。使用服务器来托管图像也是不可能的,因为我没有服务器。但是,当我查看通过Outlook发送的电子邮件的原始内容时,我看到类似的内容。

--_004_F8160BEB14FE9D41AE00F41E46A5412B109DEBCFAAdruexc02dillo_
Content-Type: image/gif; name="image001.gif"
Content-Description: image001.gif
Content-Disposition: inline; filename="image001.gif"; size=3038;
creation-date="Tue, 01 Oct 2013 14:30:35 GMT";
modification-date="Tue, 01 Oct 2013 14:30:35 GMT"
Content-ID: <image001.gif@01CEBE91.4373C900>
Content-Transfer-Encoding: base64

R0lGODlhbgA6AOfRAMQAEMUBEcUCGMYFGccJGsMWGMQYGcUaGsUaIMYcIcgeIsceKMkfI8ggKcoh
KcsjKswkK8wlMcksK8gsMMotLMotMcsuMswwM80xNM0xOc8yNc4zOs80O9E1PFZYVc06PM48Pc48...

等等。我也在HTML中看到了这一点:

<img width=3D110 height=3D58 id=3D"Image_x0020_1" src=3D"cid:image001.gif@01CEBE91.4373C900" alt=3Dimage001>

我从中理解的是,图像以某种方式插入到电子邮件中,然后使用cid proterty在HTML中引用。

我的问题是,使用.net objets MailMessage,有没有办法实际执行此操作?如果是这样,我如何'嵌入'图像,获取它使用的cid并插入到我的HTML消息中?

谢谢,

1 个答案:

答案 0 :(得分:2)

您创建一个AlternateView,然后将图像添加到它的LinkedResources集合中,这是一个示例:

Dim mailMessage as New MailMessage()

Dim bodyHTML as String = (create your html email here however you like)

Dim htmlView as Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString(bodyHTML, Nothing, "text/html")

Dim pathOfImages as String = "\Images" (or wherever they are in your project)

Dim imagePath as String = Path.Combine(pathOfImages, "testimage.png")

Dim imageResource as New Mail.LinkedResource(imagePath, "image/png")
imageResource.ContentId = "testimageID" <- this is the cid: you have noticed and that you need to use to reference the image in your html email, so this one will be <img src='cid:testimageID'>
imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64
htmlView.LinkedResources.Add(imageResource)

mailMessage.AlternateViews.Add(htmlView)
mailMessage.IsBodyHtml = True
相关问题