【问题标题】:embedding image in body of email from vb.net在 vb.net 的电子邮件正文中嵌入图像
【发布时间】:2016-09-07 12:28:08
【问题描述】:

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

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

  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 表可以工作,但上述方法不起作用。

只出现这个:

【问题讨论】:

    标签: html vb.net email visual-studio-2015


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。是的,我正在尝试作为附件和身体。你是说图片只有在项目目录中才会加载?
    • 这实际上对我有用。使用 contentid 添加对附件的引用。 stackoverflow.com/questions/1212838/…
    【解决方案2】:

    对于那些正在寻找有效的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-16
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2013-08-31
      相关资源
      最近更新 更多