我正在尝试发送带有嵌入图像的电子邮件,但遗憾的是图像没有显示在电子邮件中,这是电子邮件中显示的内容
这是我使用的代码
Dim img1 As LinkedResource = New LinkedResource("E:\OLD\avatar\bela.jpg", MediaTypeNames.Image.Jpeg)
img1.ContentId = "Image1"
e_mail.Body = "<div style=""border-style: solid; border-width: thin; border-color: gray; background-color: RGB(247, 248, 249); margin-left: 25%; margin-right: 25%;"">
<p>This is the Logo<p>
<img src=""cid:Image1"" alt='image descddription'/>
</div> "
你认为我在这里遗漏了什么?是语法错误还是gmail本身有问题?
答案 0 :(得分:0)
我当然不是这方面的专家,但我最近确实创建了一封带有嵌入图像的电子邮件,所以我会分享我在那里所做的事情。我可能遗漏了一些相关的细节,可以让它做得更好,但你应该能够像我一样做并让它工作。请注意,我是用 C# 编写代码的,但我会将相关部分翻译成 VB。
我认为您不能将链接的资源直接添加到消息中。我认为您至少需要创建一个 AlternateView
并将资源添加到其中。我为那些支持它的客户创建了一个带有 HTML 视图的消息,为那些不支持它的客户创建了一个文本视图。我创建了一种通用方法来发送各种格式相似的消息。以下是我为您考虑的相关部分:
Private Sub SendEmail(fromAddress As String,
toAddress As String,
subject As String,
body As String,
resourceFilePath As String)
Using message As New MailMessage(fromAddress,
toAddress,
subject,
body) With {.IsBodyHtml = True},
htmlView = AlternateView.CreateAlternateViewFromString(body,
Encoding.UTF8,
MediaTypeNames.Text.Html),
resource As New LinkedResource(resourceFilePath,
MediaTypeNames.Image.Jpeg) With {.ContentId = Path.GetFileNameWithoutExtension(resourceFilePath)}
htmlView.LinkedResources.Add(resource)
message.AlternateViews.Add(htmlView)
'Send message here.
End Using
End Sub
首先要注意,所有的一次性对象都是用using
语句创建的,所以它们都在最后被释放。
至于问题,请注意备用视图的创建、链接资源的创建、将资源添加到视图以及将视图添加到消息。
在本例中,我使用文件名作为资源的 ID。