如何使用 CDO 物件來寄 E-Mail

如何使用 CDO 物件來寄 E-Mail

如何使用 CDO 物件來寄 E-Mail

CDO ( Collaboration Data Objects ) 物件 , 對應檔案 CDOSYS.dll , 供E-Mail 寄送使用;

可應用於 VB 、 ASP 、 .Net 等 , 是簡單的信件寄送方式之一

Dim objCDO As Object

Dim strCfg As String

Set objCDO = CreateObject("CDO.Message")

strCfg = "http://schemas.microsoft.com/cdo/configuration/"

With objCDO

.Sender = "別胡亂送@OhMyGod.com"

.From = "誰是寄件者@NoOneKnows.com"

.To = "要寄給誰@whois.com.tw"

.Fields("urn:schemas:mailheader:X-Priority") = 1 ' Priority = PriorityUrgent 高優先順序

.Fields("urn:schemas:mailheader:return-receipt-to") = "誰是寄件者@NoOneKnows.com" ' 要求讀取回條

' .Fields("urn:schemas:httpmail:importance") = 2 ' Importance = High

' .Fields("urn:schemas:httpmail:priority") = 1 ' Priority = PriorityUrgent

.Fields.Update ' 更新欄位

.Subject = "沒有主旨(放主題啦)"

.TextBody = "ORZ" ' Text 文字格式信件內容

' 或 HTML 網頁格式信件內容

.HTMLBody = "<HTML>" & _

"<BODY>" & _

"<table border=""1"" width=""100%"">" & _

"<tr><td>I</td><td>am</td><td>Hammer</td><td>!</td></tr>" & _

"<tr><td>Who</td><td>r</td><td>u</td><td>?</td></tr>" & _

"</table>" & _

"</BODY>" & _

"</HTML>"

.AddAttachment "C:\AttFile.zip" ' 附加檔案

.CC = "Xman@yahoo.com.tw" ' 副本

.BCC = "SpiderMan@hotmail.com.tw" ' 密件副本

.Configuration(strCfg & "sendusing") = 2 ' Sendusing = SendUsingPort

.Configuration(strCfg & "smtpserver") = "msa.hinet.net" ' SMTP Server

' .Configuration(strCfg & "smtpserverport") = 25 ' SMTP Server Port ( 預設即為 25 )

' SMTP Server 如需登錄 , 則需設定 UserName / Password

' .Configuration(strCfg & "sendusername") = "UserName" ' Send User Name

' .Configuration(strCfg & "sendpassword") = "Password" ' Send Password

.Configuration.Fields.Update ' 更新 (欄位) 組態

' .DSNOptions = 4 ' 回傳信件傳送狀態

' cdoDSNDefault = 0 , DSN commands are issued.

' cdoDSNDelay = 8 , Return a DSN if delivery is delayed.

' cdoDSNFailure = 2 , Return a DSN if delivery fails.

' cdoDSNNever = 1 , No DSNs are issued.

' cdoDSNSuccess = 4 , Return a DSN if delivery succeeds.

' cdoDSNSuccessFailOrDelay = 14 ,Return a DSN if delivery succeeds, fails, or is delayed.

.Send ' 傳送

End With

Set objCDO = Nothing