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