嵌入式系统添加邮件发送功能
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 = HTMLemail.IsBodyHtml = TrueDim smtp As New SmtpClientsmtp.Host = "smtp-server-address"smtp.Credentials = CredentialCache.DefaultNetworkCredentialssmtp.Send(email)
https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png" />
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.
![]()
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.
![]()
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: where is the string from step #2.
3.在HTML中,将图像源指定为cid: ,其中是步骤#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 = HTMLemail.IsBodyHtml = Trueemail.Attachments.Add(New Attachment(My.Application.Info.DirectoryPath & "logo.jpg"))email.Attachments(0).ContentId = "logo_jpg"Dim smtp As New SmtpClientsmtp.Host = "smtp-server-address"smtp.Credentials = CredentialCache.DefaultNetworkCredentialssmtp.Send(email)
while in the HTML the image will appear as
而在HTML中,图片将显示为
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")))