在来自 vb.net 的电子邮件正文中嵌入图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37173898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:51:54  来源:igfitidea点击:

embedding image in body of email from vb.net

htmlvb.netemailvisual-studio-2015

提问by vbNewbie

I have rigorously searched for a soln on this but cannot get this to work. I am trying to send an email from within a vb.net application where an image is embedded in the body of the email. I get it to work as an attachment but in outlook and other email clients, the image does not show. The image is stored locally and I wonder if that has anything to do with it. I tried adding the image within a string first and that did not work;

我已经对此进行了严格的搜索,但无法使其正常工作。我正在尝试从 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'>")

then tried a different way with linkedresource

然后用链接资源尝试了不同的方式

  Sub SendEmail(ByVal errorMessage As String)
    Dim mail As New MailMessage()
    mail.From = New MailAddress("[email protected]", "**DAILY STATS**")
    mail.Subject = "STATS ALERT"
    mail.To.Add("[email protected]")
    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

I even changed the image type from png to gif. The normal manually created html tables work when appending it to the string but this above way does not work.

我什至将图像类型从 png 更改为 gif。正常的手动创建的 html 表在将其附加到字符串时工作,但上述方法不起作用。

Only this appears:

只出现这个:

enter image description here

在此处输入图片说明

回答by VBobCat

It seems to me that if your image goes attached to the email, your srcattribute should be "cid:imagename.png", withouta disk path. Take a look at this: embedding image in html email

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