網路城邦
上一篇 回創作列表 下一篇   字體:
Sending E-Mail with Microsoft CDO
2009/05/14 05:40:28瀏覽1175|回應0|推薦1

Sending E-Mail with Microsoft CDO

With Windows 2000, Microsoft introduced Collaborative Data Object (CDO). The Microsoft CDO API allows sending e-mail using SMTP servers and with very little resource/memory overhead.

The sample code below shows how it's done - this code is intended for the more advanced programmer and is only a starting point. Be sure to modify all of the SMTP servers, passwords, and e-mail addresses to fit your system.

More information can be found in MSDN's CDO documentation: http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_messaging.asp

'Sending a text email using authentication against a remote SMTP server
' Sample code submitted by Clint Baldwin, +1 (918) 671 3429
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM

' Create the message object.
Set objMessage = CreateObject("CDO.Message")
'Set the from address this would be your email address.
objMessage.From = """Your Name"""
' Set the TO Address separate multiple address with a comma
objMessage.To = "SomeEmail@YourDomain.com"
' Set the Subject.
objMessage.Subject = "An Email From Active Call Center."
' Now for the Message Options Part.

' Use standared text for the body.
objMessage.TextBody = _
"This is some sample message text.." & _
vbCRLF & _
"It was sent using SMTP authentication."

' Or you could use HTML as:

' objMessage.HTMLBody = strHTML

' ATTACHMENT : Add an attachment Can be any valid url
objMessage.AddAttachment("file://C:\Program Files\Active Call Center\Examples\Goodbye.wav")

' This section provides the configuration information for the SMTP server.
' Specifie the method used to send messages.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = _
cdoSendUsingPort

' The name (DNS) or IP address of the machine
' hosting the SMTP service through which
' messages are to be sent.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smtp.YourServer.com" ' Or "mail.server.com"

' Specify the authentication mechanism
' to use.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = _
cdoBasic

' The username for authenticating to an SMTP server using basic (clear-text) authentication
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = _
"YourLogin@YourDomain.com"

' The password used to authenticate
' to an SMTP server using authentication
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = _
"Password"

' The port on which the SMTP service
' specified by the smtpserver field is
' listening for connections (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = _
25

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = _
False

' Set the number of seconds to wait for a valid socket to be established with the SMTP service before timing out.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = _
60

' Update configuration
objMessage.Configuration.Fields.Update

' Use to show the message.
' MsgBox objMessage.GetStream.ReadText

' Send the message.
objMessage.Send

web: http://www.activecallcenter.com/manual/591.htm#o591

VB subroutine Example:
' --------------------------------------------------------
sub sendCDOMail( fromWho, toWho, Subject, Body, MyFormat, blnStatus )
'Input: fromWho = """Your Name""<Youremail@YourDomain.com>"
'       to Who  = "SomeEmail@YourDomain.com
' Set the TO Address separate multiple address with a comma
'       Subject = "Mail title"
'       Body = " My mail contents"
' MyFormat = 0 for html format, 1 for text format
'
 Const cdoSendUsingPickup = 1
 Const cdoSendUsingPort = 2
 Const cdoAnonymous = 0
 ' Use basic (clear-text) authentication.
 Const cdoBasic = 1
 ' Use NTLM authentication
 Const cdoNTLM = 2 'NTLM
 DIM cdoMessage,sErr,sch,cdoConfig
 dim sAuthenticationEmail, sAuthenticationEmailpwd,
 dim sAuthenticationEmailServer

 sAuthenticationEmail = "MyUserID@MyCompany.com"
 sAuthenticationEmailpwd = "MyPassword"
 sAuthenticationEmailServer = "mail.MyCompany.com"
 
 blnStatus = false

 sch = "http://schemas.microsoft.com/cdo/configuration/
 Set cdoConfig = CreateObject("CDO.Configuration") 
 With cdoConfig.Fields 
            .Item(sch & "sendusing") = cdoSendUsingPort 
            .Item(sch & "smtpserver")= sAuthenticationEmailServer 
            .Item(sch & "smtpauthenticate")= cdoBasic
            .Item(sch & "sendusername")= sAuthenticationEmail
            .Item(sch & "sendpassword")= sAuthenticationEmailpwd
            .Item(sch & "smtpserverport")= 25
            .Item(sch & "smtpusessl")= False
            .Item(sch & "smtpconnectiontimeout")= 60
            .update 
 End With 
     
 SET cdoMessage = Server.CreateObject("CDO.Message")
 with cdoMessage
      set .Configuration = cdoConfig
      .From = fromWho
      .To = toWho
      .Subject = Subject
      if MyFormat = 0 then
          .HTMLBody = Body
      else
          .TextBody = Body
      end if 
      .Send
 End With 
 if err <> 0 then
     sErr = err.Description
     Response.Write "Error=" & sErr  

 else
     Response.Write "Send email successfully" 
     blnStatus = true
 end if 

SET cdoMessage = Nothing
SET cdoConfig = Nothing

END sub
' ------------------------------------------------------------

( 知識學習檔案分享 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=jcchuang&aid=2947096