嵌入式系统添加邮件发送功能_如何发送带有嵌入式图片HTML电子邮件

嵌入式系统添加邮件发送功能

When our application needs to send an email, there are two major options. One is to use Outlook, which is what most articles on the web are about - especially on Microsoft-related websites. Naturally, this assumes that Outlook is installed. Fortunately, there's more lightweight way to do it, by using .Net library System.Net.Mail. The following simple code will do the job of sending HTML email which content is stored in the file htmlpage1.htm, part of our project:

当我们的应用程序需要发送电子邮件时,有两个主要选项。 一种是使用Outlook,这是Web上大多数文章的主题,尤其是与Microsoft相关的网站。 自然,这假定已安装Outlook。 幸运的是,通过使用.Net库System.Net.Mail,可以实现更轻量的方式。 以下简单代码将完成发送HTML电子邮件的工作,该内容存储在文件htmlpage1.htm中,这是我们项目的一部分:

    Dim f As New StreamReader(My.Application.Info.DirectoryPath & "\htmlpage1.htm")
        Dim HTML As String = f.ReadToEnd()
        f.Close()

' (assuming that the file is in fact a template, at this point we probably will manipulate the text by replacing some placeholders by the current values)

        Dim email As New System.Net.Mail.MailMessage("from-address","to-address")
        email.Subject = "experts-exchange article"
        email.Body = HTML
        email.IsBodyHtml = True

        Dim smtp As New SmtpClient
        smtp.Host = "smtp-server-address"
        smtp.Credentials = CredentialCache.DefaultNetworkCredentials 
        smtp.Send(email)

<img src="https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png" />

<img src =“ https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png ” />

However, this solution has the following major drawbacks:

但是,此解决方案具有以下主要缺点:

1. it assumes that external website is accessible by the user, and that the hosted image is in place. Considering the fact that this email may be stored and looked at years later, this is not a solid assumption.

not-found
2. Modern email clients will block external image by default, for security reasons. The user more likely than not will see a placeholder, possibly with the option to download the image, which he more likely than not will not do. 3. External content will increase the chance of the email to be blocked by antispam software.
detected as spam

1.假设用户可以访问外部网站,并且托管图像到位。 考虑到此电子邮件可能会在几年后存储和查看的事实,所以这并不是一个可靠的假设。 2.出于安全原因,现代电子邮件客户端默认情况下将阻止外部图像。 用户很可能会看到一个占位符,可能带有下载图像的选项,而他很可能不会这样做。

被客户阻止
3.外部内容将增加反垃圾邮件软件阻止电子邮件的机会。

Much better is to "host" the image within the email itself. This can be achieved by the following steps:

更好的是将图像“托管”在电子邮件本身中。 这可以通过以下步骤实现:

1. attach the image file as regular attachment to the email

1.将图像文件作为常规附件附加到电子邮件

2. for the attachment, specify content-id - can be any string

2.对于附件,请指定content-id-可以是任何字符串

3. in the HTML, specify image source as cid:<content-id> where <content-id> is the string from step #2.

3.在HTML中,将图像源指定为cid:<content-id> ,其中<content-id>是步骤#2中的字符串。

Accordingly, if we want the above email to have the image sourced from logo.jpg embedded in the body, the code will become:

因此,如果我们希望上述电子邮件将来自logo.jpg的图像嵌入到主体中,则代码将变为:

    Dim f As New StreamReader(My.Application.Info.DirectoryPath & "\htmlpage1.htm")
      Dim HTML As String = f.ReadToEnd()
      f.Close()

        Dim email As New System.Net.Mail.MailMessage("from-address","to-address")
        email.Subject = "test"
        email.Body = HTML
        email.IsBodyHtml = True

        email.Attachments.Add(New Attachment(My.Application.Info.DirectoryPath & "\logo.jpg"))
        email.Attachments(0).ContentId = "logo_jpg"

        Dim smtp As New SmtpClient
        smtp.Host = "smtp-server-address"
        smtp.Credentials = CredentialCache.DefaultNetworkCredentials
        smtp.Send(email)

while in the HTML the image will appear as

而在HTML中,图片将显示为

        <img src="cid:logo_jpg" />

<img src =“ cid:logo_jpg” />

As the final touch, let's not forget to double the content of our HTML email in plain text format, for the mail clients not capable of showing HTML - by using AlternatePart.

最后,对于不能显示HTML的邮件客户端,请不要忘记使用纯文本格式将HTML电子邮件的内容以纯文本格式加倍-通过使用AlternatePart。

        Dim PlainPart As String = "plain text representation"
        email.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(PlainPart, New System.Net.Mime.ContentType("text/plain")))

翻译自: https://www.experts-exchange.com/articles/10747/How-to-send-HTML-email-with-inline-picture.html

嵌入式系统添加邮件发送功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值