使用 vb.net 嵌入图像不显示在电子邮件中

发布于 2024-10-17 08:03:02 字数 1547 浏览 0 评论 0原文

我正在尝试在电子邮件正文中显示嵌入图像。已发送,但没有图像。

下面是代码:

Dim mail As New MailMessage()

mail.[To].Add("[email protected]")

mail.From = New MailAddress("[email protected]")

mail.Subject = "Test Email"


Dim Body As String = "<b>Welcome to codedigest.com!!</b><br><BR>Online resource for .net articles.<BR><img alt="""" hspace=0 src=""cid:imageId"" align=baseline border=0 >"

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(Body, Nothing, "text/html")

Dim imagelink As New LinkedResource(Server.MapPath(".") & "\uploads\CIMG1443.JPG", "image/jpg")

imagelink.ContentId = "imageId"

imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64

htmlView.LinkedResources.Add(imagelink)

mail.AlternateViews.Add(htmlView)

Dim smtp As New SmtpClient()

smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
smtp.Host = ConfigurationManager.AppSettings("SMTP")
smtp.Port = 587
'smtp.EnableSsl = True

smtp.Credentials = New System.Net.NetworkCredential(ConfigurationManager.AppSettings("FROMEMAIL"), ConfigurationManager.AppSettings("FROMPWD"))

smtp.Send(mail)

在电子邮件正文中仅显示以下内容:

欢迎来到 CodeDigest.Com!!

知道如何让 CIMG1443.JPG 显示吗?

谢谢

I am trying to display an embed an image within the body of an email. The is sent, however without the image.

Below is the code:

Dim mail As New MailMessage()

mail.[To].Add("[email protected]")

mail.From = New MailAddress("[email protected]")

mail.Subject = "Test Email"


Dim Body As String = "<b>Welcome to codedigest.com!!</b><br><BR>Online resource for .net articles.<BR><img alt="""" hspace=0 src=""cid:imageId"" align=baseline border=0 >"

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(Body, Nothing, "text/html")

Dim imagelink As New LinkedResource(Server.MapPath(".") & "\uploads\CIMG1443.JPG", "image/jpg")

imagelink.ContentId = "imageId"

imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64

htmlView.LinkedResources.Add(imagelink)

mail.AlternateViews.Add(htmlView)

Dim smtp As New SmtpClient()

smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
smtp.Host = ConfigurationManager.AppSettings("SMTP")
smtp.Port = 587
'smtp.EnableSsl = True

smtp.Credentials = New System.Net.NetworkCredential(ConfigurationManager.AppSettings("FROMEMAIL"), ConfigurationManager.AppSettings("FROMPWD"))

smtp.Send(mail)

In the body of the email only the following is display:

Welcome to CodeDigest.Com!!

Any idea how I can get the CIMG1443.JPG displaying?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-10-24 08:03:02

您可以尝试使用以下方法将图像转换为 base64 字符串来内联图像:

Public Function ImageToBase64(image As Image, format As System.Drawing.Imaging.ImageFormat) As String
    If image Is Nothing Then Return ""

    Using ms As New MemoryStream()
        ' Convert Image to byte[]
        image.Save(ms, format)
        Dim imageBytes As Byte() = ms.ToArray()

        ' Convert byte[] to Base64 String
        Dim base64String As String = Convert.ToBase64String(imageBytes)
        Return base64String
    End Using
End Function

然后使用类似的方法将图像添加到 HTML(其中 yourImage 是 Image- 的实例) class):

dim imageString = "<img src=""data:image/png;base64," + ImageToBase64(yourImage, ImageFormat.Png) + "="" />"

这样您就可以避免将图像添加为资源。这在很多地方都对我有用,尽管我必须承认我还没有在 hmlt 电子邮件上尝试过。

萨沙

you could try to inline the image, by converting it to a base64-string with the following method:

Public Function ImageToBase64(image As Image, format As System.Drawing.Imaging.ImageFormat) As String
    If image Is Nothing Then Return ""

    Using ms As New MemoryStream()
        ' Convert Image to byte[]
        image.Save(ms, format)
        Dim imageBytes As Byte() = ms.ToArray()

        ' Convert byte[] to Base64 String
        Dim base64String As String = Convert.ToBase64String(imageBytes)
        Return base64String
    End Using
End Function

Then you add the image to your HTML using something like this (where yourImage is an instance of the Image-class):

dim imageString = "<img src=""data:image/png;base64," + ImageToBase64(yourImage, ImageFormat.Png) + "="" />"

That way you would get around adding the image as resource. This worked for me in several places, even though I must admit that I haven't tryed it on hmlt emails.

Sascha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文