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

标签 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

最佳答案

我不明白为什么我没有早点看到这个...

这里有错字:

img1.ContentId = "Image 1"

您提供图像 ID Image 1,但在您的 HTML 代码中您引用的是 Image1:

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

只需将第一行更改为:

img1.ContentId = "Image1"

而且有效!

Screenshot

关于html - 如何使用 SMTP 发送正文带有图像的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49836690/

相关文章:

html - CSS 菜单在悬停时移动内容

javascript - 为什么 createjs DOMElements 不能像所有其他对象一样正确定位在 Canvas 的左上角?

电子邮件字符集的 Magento 问题

vb.net - 如何将长行换行到标签控件中?

php - DomainKey-Signature 和 DKIM-Signature 之间有什么区别?

email - MS Exchange 服务器的 Grails 邮件插件配置

html - 使用*通用选择器和html有什么区别?

javascript - onclick 从表单返回输入而不是重定向

asp.net - Visual Studio 2008 中的项目级导入存储在哪里?

c# - 如何捕获主线程可以监视的多线程应用程序中的事件?