Html 从vb.net在电子邮件正文中嵌入图像

Html 从vb.net在电子邮件正文中嵌入图像,html,vb.net,email,visual-studio-2015,Html,Vb.net,Email,Visual Studio 2015,我一直在努力寻找解决方案,但无法实现。我正在尝试从vb.net应用程序中发送电子邮件,在该应用程序中,电子邮件正文中嵌入了一个图像。我让它作为附件工作,但在outlook和其他电子邮件客户端中,图像不会显示。图像存储在本地,我想知道这是否与此有关。我先尝试在字符串中添加图像,但没有成功 emailString.AppendLine() emailString.AppendLine() emailString.AppendLine("<br />") e

我一直在努力寻找解决方案,但无法实现。我正在尝试从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'>")
emailString.AppendLine()
emailString.AppendLine()
emailString.AppendLine(“
”) emailString.AppendLine(“
”) emailString.AppendLine(“”)
然后用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
子发送电子邮件(ByVal errorMessage作为字符串)
将邮件设置为新邮件消息()
mail.From=新邮件地址(“med@person.com“,”**每日统计数据**”)
mail.Subject=“统计警报”
mail.To.Add(“med@person.com")
mail.IsBodyHtml=True
'mail.Body=“测试电子邮件”'错误消息
'mail.BodyEncoding=Encoding.UTF8
smptpServer.Timeout=30000
smptpServer.Credentials=新网络凭据(“我”、“人”)
Dim imageBody As String=“”
Dim htmlView As System.Net.Mail.AlternateView=System.Net.Mail.AlternateView.CreateAlternateView-FromString(errorMessage,Nothing,“text/html”)
Dim PLAINTEXTEXTVIEW作为System.Net.Mail.AlternateView=System.Net.Mail.AlternateView.CreateAlternateViewFromString(错误消息,无,“文本/普通”)
将imageResource设置为新的System.Net.Mail.LinkedResource(“C:\Users\Public\Pictures\Sample Pictures\br1.gif”,MediaTypeNames.Image.gif)
Dim附件作为System.Net.Mail.attachment
附件=新系统.Net.Mail.attachment(“C:\Users\Public\Pictures\Sample Pictures\br1.gif”)
mail.Attachments.Add(附件)
'imageResource.ContentId=“HDIImage”
htmlView.LinkedResources.Add(图像资源)
htmlView.transferncode=Net.Mime.transferncode.Base64
htmlView.ContentType=新系统.Net.Mime.ContentType(“text/html”)
'mail.AlternateViews.Add(纯文本视图)
mail.AlternateViews.Add(htmlView)
尝试
smptpServer.Send(邮件)
特例
如果ex.Message.包含(“超时”),则
出口接头
如果结束
结束尝试
我甚至将图像类型从png更改为gif。普通的手动创建的html表在将其追加到字符串时可以工作,但上述方法不起作用

仅此显示:


在我看来,如果您的图像被附加到电子邮件中,您的src属性应该是“cid:imagename.png”,而没有磁盘路径。看看这个:

对于那些正在寻找一个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)

谢谢你的回复。是的,我试着作为一个附件和在体内。你是说只有在项目目录中才会加载图像吗?这对我来说确实有效。添加对contentid为的附件的引用。