CDO.Message 传递电子邮件

ASPX 使用 SMTP 传输通讯协定 Send Mail 传送电子邮件。

Dim cdoIMessage = CreateObject("CDO.Message")
cdoIMessage.From = "name@yourServer.com.tw" '发信者电子信箱
cdoIMessage.To = "name@customer.com.tw" '收件者电子信箱
cdoIMessage.Subject = "信件主旨"
cdoIMessage.TextBody = "信件内容"
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2  '1 代表使用本机 SMTP, 2 为外部 SMTP
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpserver.your.com.tw"  'SMTP 伺服器位址
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465  'SMTP 伺服器连线 Port
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True  '是否使用 SSL 连线 (False 或 True)
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60  '连线伺服器逾时时间
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  'SMTP 伺服器是否需要验证
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "UserName"  'SMTP 伺服器使用者名称
cdoIMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"  'SMTP 伺服器使用者密码
cdoIMessage.Configuration.Fields.Update
cdoIMessage.Send()

必须宣告引用邮件的协作数据对象 System.Net.Mail 命名空间、用来初始化建立 MailMessage 物件,设定寄件者、收件者、主旨、内容等属性参数。使用 ASPX 传送电子邮件也会受到网站空间的影响,不同的网站空间可能会有不同的限制或规范条,网站空间是否支援该功能的问题件。




ASP Class 修正用于服务器 Authentication 验证 ASP 上配合 CDO.Message 物件使用 SMTP 验证发信。使用 Windows 虚拟主机,需要于程式码中标示、在 SMTP 服务器上需要一组有效的「电子邮件地址」和「密码」进行 Mail Server 邮件伺服器验证。

Dim cdo_Config, cdo_Message
On Error Resume Next
Set cdo_Config = Server.CreateObject("CDO.Configuration")
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpserver.your.com.tw"
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
'### 使用 SSL 进行连接 (True or False)
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'### 如果服务器需要传出身份验证,并使用有效的电子邮件地址和密码
'### 基本身份验证 Authentication
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'### 在 SMTP 服务器上的用户 ID 及 PASSWORD 密码
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name@yourServer.com.tw"
cdo_Config.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "******"
cdo_Config.Fields.Update
Set cdo_Message = Server.CreateObject("CDO.Message")
Set cdo_Message.Configuration = cdo_Config
cdo_Message.BodyPart.Charset = "utf-8"
cdo_Message.From = "通知信"
cdo_Message.To = "name@customer.com.tw" '## 收件者
cdo_Message.CC = "name@yourServer.com.tw" '## 副本
cdo_Message.Bcc = "name@yourServer.com.tw" '## 密件副本
cdo_Message.Subject = "(信件主旨)"
cdo_Message.htmlBody = "信件内容" '## HTML 网页格式信件 or TextBody
cdo_Message.Send
If Err.Number <> 0 Then
	'Call 处理错误之后的程式。
End If
Err.Clear
On Error GoTo 0
Set cdo_Message = Nothing
Set cdo_Config = Nothing



旧版本 Create CDO.Message object

使用 ASP 中透过外部 SMTP 发信 CDO (Collaboration Data Objects) 物件,对应程式库档案 Cdosys.dll 提供自动 E-Mail 寄送使用;可应用于 VB, ASP, ASP.Net 等,是简易的信件寄送方式。

<%
strYouEmail = "name@yourServer.com.tw"
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = Server.CreateObject("CDO.Configuration")
cdoConfig.Fields.Item(sch & "sendusing") = 2 '## (1) 使用本地 Local SMTP, (2) 为外部 SMTP
cdoConfig.Fields.Item(sch & "smtpserver") = www.your-smtpserver.com '## 您的网址
cdoConfig.Fields.Item(sch & "smtpserverport") = 25 '## SMTP Server Port (预设即为 25)
cdoConfig.Fields.Update
Set cdoMessage = Server.CreateObject("CDO.Message")
Set cdoMessage.Configuration = cdoConfig
cdoMessage.From = "通知信<" & strYouEmail & ">"
cdoMessage.To = strYouEmail '## 收件者
cdoMessage.CC = strYouEmail '## 副本
cdoMessage.BCC = strYouEmail '## 密件副本
cdoMessage.Subject = "(信件主旨)"
cdoMessage.HTMLBody = "信件内容" '## HTML 网页格式信件
cdoMessage.TextBody = "信件内容" '## 文字格式信件内容
cdoMessage.AddAttachment = "C:\AttachFile.txt" '## 附加档案
cdoMessage.Send
Set cdoMessage = Nothing
Set cdoConfig = Nothing

'## SMTP Server 如需登录,则需加入以下设定 UserName/Password
'## .Configuration(strCfg & "sendusername") = "UserName"
'## .Configuration(strCfg & "sendpassword") = "Password"
%>


CDO.Message 属性说明

  • Subject 邮件的主旨
  • From 寄件人的电子邮件信箱
  • To 收件人的电子邮件信箱 (可用分号;或逗号,分开成多位收件人)
  • CC 副本收件人的电子邮件信箱
  • BCC 以密件传送副本的电子邮件信箱
  • TextBody 邮件的本文 纯文字模式
  • HTMLBody 邮件的本文 HTML 模式
  • AddAttachment "C:\AttachFile.txt" 附件档案
  • CreateMHTMLBody "http://www.your.com.tw/" 将网页用 HTML 格式送出
  • CreateMHTMLBody "file://C:/AttachFile.htm" 将本机中的网页用 HTML 格式送出

无法建立在 CDO 物件产生的错误问题可能为下列之情况:

未正确地註册 Cdosys.dll 档案。使用者帐户没有足够的权限来存取程式库 (Cdosys.dll) 的 CDO 的登录机码。无效的通讯协定 SMTP 或是 SmtpMail.SmtpServer 属性的设定不正确。使用者没有透过 SMTP 虚拟伺服器转送电子邮件讯息的权限。MailMessage.From 属性不是设定为有效的电子邮件地址。


CDO.Configuration 发送错误的可能原因

  • -2147220973 网路连接时 Smtp, Smtp Server Port 引发的错误,例如 25, 465, 587。传输过程网路连接中断。
  • -2147220975 使用 smtpauthenticate 身份验证、帐号或密码错误不正确。
  • -2147220960 可能的原因 Configuration 配置错误。
  • -2147220977 电子邮件地址的格式不正确。
  • -2147220980 没有提供电子邮件地址。



使用 SmtpClient 传输通讯协定传送电子邮件

Dim mailClient As New SmtpClient("smtpserver.your.com.tw")
mailClient.Port = 465
mailClient.EnableSsl = True
mailClient.UseDefaultCredentials = True
Dim credentials As New System.Net.NetworkCredential("UserName", "Password")
mailClient.Credentials = credentials
Dim message As New MailMessage()
message.From = New MailAddress("name@yourServer.com.tw", "My Email Address Name")
message.[To].Add("name@customer.com.tw")
message.Subject = "信件主旨"
message.Body = "信件内容"
mailClient.Send(message)

Credentials 取得或设定用来验证寄件者的认证。

DeliveryFormat 取得或设定 SmtpClient 用来传送电子邮件的电子邮件传递格式。
DeliveryMethod 指定将要如何处理外送的电子邮件讯息。

EnableSsl 指定 SmtpClient 是否使用 Secure Sockets Layer (SSL) 加密连线。

Port 取得或设定用于 SMTP 交易的连接埠。
Host 取得或设定用于 SMTP 传递电子邮件的主机名称或 IP 位址。
ServicePoint 取得用来传递电子邮件讯息的网路连线。

TargetName 取得或设定 Active Directory 服务提供者服务主体名称 SPN 使用延伸保护时会用此名称进行为验证请求提供服务。

Timeout 取得或设定值,指定同步 Send 传送唿叫逾时的时间长度。

UseDefaultCredentials Boolean、取得或设定,控制是否随着要求传送 DefaultCredentials。
PickupDirectoryLocation 取得或设定资料夹,SmtpClient 会要求由本机 SMTP 伺服器处理的邮件讯息。

SmtpClient 传递电子邮件可能会重复使用相同的 SmtpClient 物件,将许多不同的电子邮件传送至相同的 SMTP 伺服器和可能会是不同的 SMTP 伺服器。因此没有方法可以判断应用程式物件是否完成且加以清除、并且需要使用的伺服器端是否支持。

https://docs.microsoft.com/zh-tw/dotnet/api/system.net.mail.smtpclient?view=net-6.0