CDO (Collaboration Data Objects) 物件 , 對應程式庫檔案 Cdosys.dll
提供自動 E-Mail 寄送使用;可應用於 VB, ASP, ASP.Net 等,是簡易的信件寄送方式。
<%
strYouEmail = "me@your.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 物件產生的錯誤問題可能為下列之情況:
- 未正確地註冊 Cdosys.dll 檔案。
- 使用者帳戶沒有足夠的權限來存取程式庫 (Cdosys.dll) 的 CDO 的登錄機碼。
- 無效的通訊協定 SMTP 或是 SmtpMail.SmtpServer 屬性的設定不正確。
- 使用者沒有透過 SMTP 虛擬伺服器轉送電子郵件訊息的權限。
- MailMessage.From 屬性不是設定為有效的電子郵件地址。
屬性說明
- Subject 郵件的主旨
- From 寄件人的電子郵件信箱
- To 收件人的電子郵件信箱 (可用分號;或逗號,分開成多位收件人)
- CC 副本收件人的電子郵件信箱
- BCC 密送副本收件人的電子郵件信箱
- TextBody 郵件的本文 純文字模式
- HTMLBody 郵件的本文 HTML 模式
- AddAttachment "C:\AttachFile.txt" 附件
- CreateMHTMLBody "http://www.aua.com/" 將網頁用 HTML 格式送出
- CreateMHTMLBody "file://C:/AttachFile.htm" 將本機中的網頁用 HTML 格式送出
透過 Gmail SMTP 發信
Send mail in asp via Gmail SMTP
<%
cdoConfig.Fields.Item(sch & "smtpserver") = smtp.gmail.com
cdoConfig.Fields.Item(sch & "smtpserverport") = 465 '## HTML 網頁格式信件
cdoConfig.Fields.Item(sch & "smtpauthenticate") = 1 '## cdoBasic 基本驗證
cdoConfig.Fields.Item(sch & "smtpusessl") = True
cdoConfig.Fields.Item(sch & "sendusername") = "UserName" '## 帳號
cdoConfig.Fields.Item(sch & "sendpassword") = "Password" '## 密碼
%>
引用:http://www.eion.com.tw/Blogger/?t=Create-CDO-Message-Object&Pid=1134
