2010年11月10日 星期三

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

以下內容轉載自強力鎯頭 の VB 部落
如何使用 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


以上內容轉載自強力鎯頭 の VB 部落
其他參考資料:http://www.pro-soho.com/blog/article.asp?id=295


CDO 郵件元件使用方法
Microsoft從Windows Server 2000 及 Windows XP開始內建郵件元件已改為CDO
在之前的系統內建郵件元件則是 CDONTS,各位要注意,以下是CDO的用法

屬性 說明
Subject 郵件的主旨
From 寄件人的電子郵件信箱
To   收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
CC   副本收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
BCC 密送副本收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
TextBody 郵件的本文-純文字模式
HTMLBody 郵件的本文-HTML模式


沒有留言: