我正在尝试做一个电子邮件发送验证模块,我想在消息的开头显示我的公司标签图像,但我找到的所有解决方案都是如何在电子邮件上放置图像附件。
我想做的是将公司标签图像放在消息的开头,如下所示:
这是我的代码:
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
最佳答案
我不明白为什么我没有早点看到这个...
这里有错字:
img1.ContentId = "Image 1"
您提供图像 ID Image 1
,但在您的 HTML 代码中您引用的是 Image1
:
mm.Body = mm.Body & "<td><img src=cid:Image1 alt=></td>"
只需将第一行更改为:
img1.ContentId = "Image1"
而且有效!
关于html - 如何使用 SMTP 发送正文带有图像的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49836690/