文章目录使用SMTP服务器发送电子邮件
第 5 章向电子邮件添加附件向电子邮件添加附件
可以将附件添加到电子邮件或消息部分(特别是添加到 %Net.MailMessagePart 或 %Net.MailMessage 的实例)。为此,请使用以下命令:
这些方法中的每一个都会将附件添加到原始电子邮件(或电子邮件部分)的部件列表中,并手动将 IsMultiPart 属性设置为 1。
附加文件()
method AttachFile(Dir As %String, File As %String, isBinary As %Boolean = 1, charset As %String = "", ByRef count As %Integer) as %Status
将给定文件附加到电子邮件中。默认情况下,文件作为二进制补码附件发送,但您可以将它们指定为文本。如果文件是文本163邮件没有其他smtp服务器,还可以指定文件使用的字符集。
具体来说,该方法创建一个%Net.MailMessagePart的实例,根据需要将文件内容放入BinaryData或TextData属性,并根据需要设置CharSet属性和TextData.TranslateTable属性。该方法通过引用返回一个整数,表示新消息部分在部件列表中的位置。
此方法还设置消息或消息部分的 Dir 和 FileName 属性。
附加流()
method AttachStream(stream As %Stream.Object, Filename As %String, isBinary As %Boolean = 1, charset As %String = "", ByRef count As %Integer) as %Status
将给定的流附加到电子邮件中。如果指定了 Filename,则附件被视为文件附件。否则,它将被视为内联附件。
AttachNewMessage()
method AttachNewMessage() as %Net.MailMessagePart
创建 %Net.MailMessage 的新实例,将其添加到消息中,并返回新更改的父消息或消息部分。
AttachEmail()
给定一封电子邮件(%Net.MailMessage 的一个实例),此方法会将其添加到电子邮件中。此方法还设置消息或消息部分的 Dir 和 FileName 属性。
注意:此方法将 contentType 设置为“message/rfc822”。在这些情况下,无法添加其他附件。
示例:MessageWithAttach()
以下示例生成带有硬编码附件的简单电子邮件。它不提供任何短信地址;该信息可以在实际发送短信时提供
/// w ##class(PHA.TEST.HTTP).MessageWithAttachment() ClassMethod MessageWithAttachment() As %Net.MailMessage { Set msg = ##class(%Net.MailMessage).%New() Set msg.Subject="Message with attachment "_$h Set msg.IsBinary=0 Set msg.IsHTML=0 Do msg.TextData.Write("This is the main message body.") //add an attachment Set status=msg.AttachFile("E:", "HttpDemo.pdf") If $$$ISERR(status) { Do $System.Status.DisplayError(status) Quit $$$NULLOREF } b Quit msg }
使用 SMTP 服务器发送电子邮件
如果您可以访问 SMTP 服务器,则可以发送电子邮件。 SMTP 服务器必须正在运行并且必须具有使用它的必要权限。发送电子邮件:
创建 %Net.SMTP 的实例并根据需要设置其属性,尤其是以下属性:
此对象描述将使用的 SMTP 服务器。
如果 SMTP 服务器需要身份验证,请指定必要的凭据。因此:
一个。创建 %Net.Authenticator 的实例。
b.设置此对象的用户名和密码属性。
c。将 %Net.SMTP 实例的身份验证器属性设置为等于该对象。
d。如果电子邮件本身有授权发件人,请设置 %Net.SMTP 实例的 AuthFrom 属性。
使用 SSL/TLS 连接到 SMTP 服务器:
一个。将 SSLConfiguration 属性设置为要使用的已激活 SSL/TLS 配置的名称。
SSL/TLS 配置包含一个名为 ConfigurationName 的选项,它是此设置中使用的字符串。
b.将 UseSTARTTLS 属性设置为 0 或 1。
在大多数情况下,使用值 0。如果服务器交互在普通 TCP 套接字上开始,然后在与普通套接字相同的端口上切换到 TLS,则使用值 1。
或者,将 SSLCheckServerIdentity 属性设置为 1。如果要验证证书中的主机服务器名称,请执行此操作。
创建要发送的电子文本消息(如“创建单部分电子邮件”和“创建多部分电子邮件”中所述)。调用 SMTP 实例的 send() 方法。此方法返回一个状态,应检测到该状态。如果返回的状态指示错误,请查看包含错误消息本身的 Error 属性。检测 FailedSend 属性,该属性包含发送操作失败的电子邮件地址列表。
以下部分中的示例使用了编写本指南时可用的两种不同的免费 SMTP 服务。选择这项服务并不意味着非常认可。另请注意,此示例未显示实际密码。
Samples 命名空间中还有其他示例。要找到它们,请在此命名空间中搜索 %Net.SMTP。
重要提示:%Net.SMTP 将电子邮件正文写入临时文件流。默认情况下,文件被写入命名空间目录,如果该目录需要特殊的写权限,文件将不会被创建,你会得到一个空的消息体。
您可以为这些临时文件定义新路径,并选择不限制写访问的路径(例如,/tmp)。因此,设置全局节点 %SYS("StreamLocation",namespace),其中 NAMESPACE 是运行代码的命名空间。如:
Set ^%SYS("StreamLocation","SAMPLES")="/tmp"
如果 %SYS("StreamLocation",namespace) 为 NULL,InterSystemsIRIS 使用 %SYS("TempDir",namespace) 指定的目录。如果 %SYS("TempDir",namespace) 没有设置,IRIS 使用 %SYS("TempDir") 指定的目录
示例 1:HotPOPAsSMTP() 和 SendSimpleMessage()
这个例子包含两种一起使用的方式。第一个使用已在 HotPOPSMTP 服务器上设置的测试帐户创建 %Net.SMTP 实例:
/// w ##class(PHA.TEST.HTTP).HotPOPAsSMTP() ClassMethod HotPOPAsSMTP() As %Net.SMTP { Set server=##class(%Net.SMTP).%New() Set server.smtpserver="smtp.hotpop.com" //HotPOP SMTP服务器使用默认端口(25) Set server.port=25 //创建对象以进行身份验证 Set auth=##class(%Net.Authenticator).%New() Set auth.UserName="isctest@hotpop.com" Set auth.Password="123pass" Set server.authenticator=auth Set server.AuthFrom=auth.UserName b Quit server }
下一个方法使用提供的 SMTP 服务器作为参数发送一条简单、唯一的消息:
ClassMethod SendSimpleMessage(server As %Net.SMTP) As %List { Set msg = ##class(%Net.MailMessage).%New() Set From=server.authenticator.UserName Set:From="" From="xxx@xxx.com" Set msg.From = From Do msg.To.Insert("xxx@xxx.com") //Do msg.Cc.Insert("yyy@yyy.com") //Do msg.Bcc.Insert("zzz@zzz.com") Set msg.Subject="Unique subject line here "_$H Set msg.IsBinary=0 Set msg.IsHTML=0 Do msg.TextData.Write("This is the message.") Set status=server.Send(msg) If $$$ISERR(status) { Do $System.Status.DisplayError(status) Write server.Error Quit "" } Quit server.FailedSend }
示例 2:YPOPsAsSMTP()
此示例使用 YPOPS 创建 %Net.SMTP 实例,YPOPS 是一种客户端软件,可提供对 Yahoo 电子邮件帐户的 SMTP 和 POP3 访问。它使用为此目的设置的测试帐户:
ClassMethod YPOPsAsSMTP() As %Net.SMTP { Set server=##class(%Net.SMTP).%New() //local host acts as the server Set server.smtpserver="127.0.0.1" //YPOPs uses default port, apparently Set server.port=25 //Create object to carry authentication Set auth=##class(%Net.Authenticator).%New() //YPOPs works with a Yahoo email account Set auth.UserName="isc.test@yahoo.com" Set auth.Password="123pass" Set server.authenticator=auth Set server.AuthFrom=auth.UserName Quit server }
它可以与上例所示的 SendSimpleMessage 方法一起使用。
示例 3:SendMessage()
以下是接受 SMTP 服务器和电子邮件的更灵活的方法。电子邮件应已包含主题行(如果 SMTP 服务器要求),但不包含地址。之后163邮件没有其他smtp服务器,此方法将电子文本消息发送到一组硬编码的测试目的地:
ClassMethod SendMessage(server As %Net.SMTP, msg As %Net.MailMessage) As %Status { Set From=server.authenticator.UserName //make sure From: user is same as used in authentication Set msg.From = From //finish addressing the message Do msg.To.Insert("xxx@xxx.com") //send the message to various test email addresses Do msg.To.Insert("isctest@hotpop.com") Do msg.To.Insert("isc_test@hotmail.com") Do msg.To.Insert("isctest001@gmail.com") Do msg.To.Insert("isc.test@yahoo.com") Set status=server.Send(msg) If $$$ISERR(status) { Do $System.Status.DisplayError(status) Write server.Error Quit $$$ERROR($$$GeneralError,"Failed to send message") } Quit $$$OK }
%Net.SMTP 的其他属性
%Net.SMTP 类还具有您可能需要的一些其他属性,具体取决于所使用的 SMTP 服务器: