1

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:

enter image description here

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
vbNewbie
  • 3,291
  • 15
  • 71
  • 155

2 Answers2

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

Community
  • 1
  • 1
VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • Thanks for your response. Yes I am trying both as an attachment and in the body. Are you saying that the image will only load if within the project directory? – vbNewbie May 12 '16 at 15:42
  • 2
    this actually worked for me. To add a reference to the attachment with the contentid. http://stackoverflow.com/questions/1212838/c-sharp-sending-mails-with-images-inline-using-smtpclient – vbNewbie May 12 '16 at 15:42
0

For those that are looking for a "final version" of a VB.NET code that is working and/or a way to add more than one image, this is what I finally ended up. Basically, this embed 2 images in a signature at the bottom of the message :

    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)
Fabien TheSolution
  • 5,055
  • 1
  • 18
  • 30