我已经严格搜索了这个问题,但无法让它发挥作用。我正在尝试从vb.net应用程序中发送电子邮件,其中图像嵌入在电子邮件正文中。我将它作为附件工作,但在Outlook和其他电子邮件客户端中,图像不显示。图像存储在本地,我想知道它是否与它有关。我尝试首先在字符串中添加图像,但这不起作用;
emailString.AppendLine()
emailString.AppendLine()
emailString.AppendLine("<br />")
emailString.AppendLine("<br />")
emailString.AppendLine("<img src =""cid:C:\Users\Public\Pictures\Sample Pictures\br1.gif"" alt='STUPID IMAGE'>")
然后尝试使用linkedresource
的另一种方式 Sub SendEmail(ByVal errorMessage As String)
Dim mail As New MailMessage()
mail.From = New MailAddress("med@person.com", "**DAILY STATS**")
mail.Subject = "STATS ALERT"
mail.To.Add("med@person.com")
mail.IsBodyHtml = True
'mail.Body = "TEST EMAIL" 'errorMessage
'mail.BodyEncoding = Encoding.UTF8
smptpServer.Timeout = 30000
smptpServer.Credentials = New NetworkCredential("me", "person")
Dim imageBody As String = " <img src =""cid:C:\Users\Public\Pictures\Sample Pictures\br1.gif"">"
Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(errorMessage, Nothing, "text/html")
Dim plainTextView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(errorMessage, Nothing, "text/plain")
Dim imageResource As New System.Net.Mail.LinkedResource("C:\Users\Public\Pictures\Sample Pictures\br1.gif", MediaTypeNames.Image.Gif)
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment("C:\Users\Public\Pictures\Sample Pictures\br1.gif")
mail.Attachments.Add(attachment)
'imageResource.ContentId = "HDIImage"
htmlView.LinkedResources.Add(imageResource)
htmlView.TransferEncoding = Net.Mime.TransferEncoding.Base64
htmlView.ContentType = New System.Net.Mime.ContentType("text/html")
'mail.AlternateViews.Add(plainTextView)
mail.AlternateViews.Add(htmlView)
Try
smptpServer.Send(mail)
Catch ex As Exception
If ex.Message.Contains("Timeout") Then
Exit Sub
End If
End Try
我甚至将图像类型从png更改为gif。正常的手动创建的html表在将其附加到字符串时起作用,但这种方式不起作用。
只显示:
答案 0 :(得分:1)
在我看来,如果您的图片附加到电子邮件中,则 src 属性应为“cid:imagename.png”,不带磁盘路径。看看这个:embedding image in html email
答案 1 :(得分:0)
对于那些正在寻找有效的 VB.NET 代码的“最终版本”和/或添加多个图像的方法的人来说,这就是我最终的结果。基本上,这会在消息底部的签名中嵌入 2 张图像:
Image1 = New Net.Mail.LinkedResource(Directory.GetCurrentDirectory + "\mailsrvr\image001.jpg", "image/jpeg")
Image1.ContentId = Guid.NewGuid().ToString()
Image2 = New Net.Mail.LinkedResource(Directory.GetCurrentDirectory + "\mailsrvr\image002.jpg", "image/jpeg")
Image2.ContentId = Guid.NewGuid().ToString()
Signature = Replace(Replace(Signature, "image001.jpg", Image1.ContentId), "image002.jpg", Image2.ContentId)
myMessage.Body = myMessage.Body & Signature
Dim View = Net.Mail.AlternateView.CreateAlternateViewFromString(myMessage(msgInd).Body, Nothing, "text/html")
View.LinkedResources.Add(Image1)
View.LinkedResources.Add(Image2)
myMessage.AlternateViews.Add(View)