Lucifer Rodstark
Lucifer Rodstark

Reputation: 216

How to send an email with an image on the body using SMTP?

I am trying to do an email sending verification module and I want to display my company label image at the beginning of the message but all the solutions I found is how to put an image attachment on the email.

What I'm trying to do is putting the Company Label image at the beginning of the message like this:

Steam Email

Here is my code :

Try
       Dim mm As New MailMessage 'Email of Sender'
       Dim NetworkCred As New NetworkCredential()
       Dim smtp As New SmtpClient()
       Dim img1 As LinkedResource = Nothing

       Try
           img1 = New LinkedResource("https://image.ibb.co/iowsbn/Umbrella_Corporation_Company_PNG.png", MediaTypeNames.Image.Jpeg)

           img1.ContentId = "Image1"
       Catch ex As Exception
           MetroMessageBox.Show(Login, ex.Message, "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
       End Try


       mm.From = New MailAddress("[email protected]", "Company")
       mm.[To].Add(New MailAddress(Login.MetroTextBox5.Text))

       mm.Subject = "Password Recovery"

       mm.Body = String.Format("") 'Message'

       mm.Body = mm.Body & "<font color=red> <h1> Dear " + firstname + " " + lastname + ", </h1> </font>"
       mm.Body = mm.Body & "<h3> The New Generated Password you need to Login into your Account is : </h3>"
       mm.Body = mm.Body & "<font color=red> <h1> " + lbl1.Text + " </h1> </font>"
       mm.Body = mm.Body & "This Email and Password was Generated upon your request. The Generated Password is required to complete the login."
       mm.Body = mm.Body & "<strong> No one can access your account without also accessing this email. <br> If you are not attempting to login </strong>"
       mm.Body = mm.Body & "then please change your password immediately and consider changing your email password as well to ensure your account security. </br>"
       mm.Body = mm.Body & "<td><img src=cid:Image1 alt=></td>"

       Dim av1 As AlternateView = AlternateView.CreateAlternateViewFromString(mm.Body, Nothing, MediaTypeNames.Text.Html)
       av1.LinkedResources.Add(img1)

       mm.AlternateViews.Add(av1)

       mm.IsBodyHtml = True
       smtp.Host = "smtp.gmail.com"
       smtp.EnableSsl = True
       smtp.Port = 587

       NetworkCred.UserName = "[email protected]"
       NetworkCred.Password = "xxxxxxxxxx"
       smtp.UseDefaultCredentials = True
       smtp.Credentials = NetworkCred
       smtp.Send(mm)

Catch ex As Exception
   MetroMessageBox.Show(Login, ex.Message, "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try

Upvotes: 3

Views: 10466

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18320

I don't understand why I didn't see this earlier...

You have a typo here:

img1.ContentId = "Image 1"

You give the image ID Image 1, but in your HTML code you are referencing Image1:

mm.Body = mm.Body & "<td><img src=cid:Image1 alt=></td>"

Simply change the first line to:

img1.ContentId = "Image1"

and it works!

Screenshot

Upvotes: 4

v.slobodzian
v.slobodzian

Reputation: 493

To attach an embedded image to email, you should add an attachment to email at first. Then you should assign a unique identifier(contentid) to this attachment. Finally, you should use <img src="cid:yourcontentid" /> instead of <img src="your file name" /> .

Dim oAttachment As Attachment = oMail.AddAttachment("d:\test.gif")

        Dim contentID As String = "test001@host"
        oAttachment.ContentID = contentID
        oMail.HtmlBody = "<html><body>this is a <img src=""cid:" _
                 & contentID & """> embedded picture.</body></html>"

        oSmtp.SendMail(oServer, oMail)

I have found a few links for you:

  1. Send Email with Embedded Images in VB.NET
  2. SOF - embedding image in body of email from vb.net

It just would helpful for you:

  1. A complete breakdown of the CSS support for the most popular mobile, web and desktop email clients

Upvotes: 4

Related Questions