embedding image in body of email from vb.net

爷,独闯天下 提交于 2020-01-06 23:48:13

问题


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'>")

then tried a different way with 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

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.

Only this appears:


回答1:


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



来源:https://stackoverflow.com/questions/37173898/embedding-image-in-body-of-email-from-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!