简体   繁体   中英

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邮件

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("xxxxxxxxxxxxx@gmail.com", "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 = "xxxxxxxxxxxxx@gmail.com"
       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

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

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!

屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM