1

我想将图像插入 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 属性在 HTML 中引用。

我的问题是,使用 .net 对象 MailMessage,有没有办法真正做到这一点?如果是这样,我如何“嵌入”图像,获取它使用的 cid 并插入到我的 html 消息中?

谢谢,

4

1 回答 1

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
于 2013-10-02T05:52:13.790 回答