本文介绍了将图像嵌入来自vb.net的电子邮件正文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了严格的搜索,但无法使其正常工作。我正在尝试从vb.net应用程序中发送电子邮件,该应用程序中的图像嵌入在电子邮件正文中。我可以将其作为附件使用,但是在Outlook和其他电子邮件客户端中,该图像没有显示。该图像存储在本地,我想知道是否与此有关。我尝试先将图片添加到字符串中,但这没有用;

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;

    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

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

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

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.

仅出现以下情况:

推荐答案

在我看来,如果您的图像如果附加到电子邮件,则您的 src 属性应为 cid:imagename.png,不带磁盘路径。看看以下内容:

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

这篇关于将图像嵌入来自vb.net的电子邮件正文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:01