如何使用SMTP发送带有图像的电子邮件?

时间:2018-04-14 22:14:42

标签: html vb.net email smtp gmail

我正在尝试发送电子邮件发送验证模块,我想在邮件开头显示我的公司标签图像,但我找到的所有解决方案都是如何在电子邮件中放置图像附件。

我要做的是将公司标签图像放在邮件的开头,如下所示:

Steam Email

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

要将嵌入图像附加到电子邮件,您应首先向电子邮件添加附件。然后,您应该为此附件分配唯一标识符(contentid)。最后,您应该使用<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)

9.0.1

我找到了一些链接:

  1. Send Email with Embedded Images in VB.NET
  2. SOF - embedding image in body of email from vb.net
  3. 这对你有帮助:

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

答案 1 :(得分:2)

我不明白为什么我之前没有看到这个......

你这里有一个错字:

img1.ContentId = "Image 1"

您提供了图片ID Image 1,但在您的HTML代码中引用了Image1

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

只需将第一行更改为:

img1.ContentId = "Image1"

它有效!

  

Screenshot

相关问题