Using CDO with VB Script

Posted on 2006-05-01
Last Modified: 2011-09-20
Can someone please give a sample VB Script using CDO as the method of sending out an email? Using Outlook object will not work for me (can't get by the security prompt).

Question by:brclaveria
    LVL 44

    Accepted Solution

    Hi brclaveria,

    you could look at

    hope this helps a bit
    LVL 9

    Assisted Solution

    Should be what your looking for. Assuming you have a mail server installed to localhost, if not you can specify an IP address and use authentication.

    Function SendEmail(sFrom, sTo, sCc, sBcc, sSubject, sBody)
          Const cdoSendUsingMethod        = ""
          Const cdoSendUsingPort          = 2
          Const cdoSMTPServer             = ""
          Const cdoSMTPServerPort         = ""
          Const cdoSMTPConnectionTimeout  = ""
          Const cdoSMTPAuthenticate       = ""
          Const cdoBasic                  = 1
          Const cdoNone                   = 0
          Const cdoSendUserName           = ""
          Const cdoSendPassword           = ""

          Dim objConfig  ' As CDO.Configuration
          Dim objMessage ' As CDO.Message
          Dim Fields     ' As ADODB.Fields

          ' Get a handle on the config object and it's fields
          Set objConfig = Server.CreateObject("CDO.Configuration")
          Set Fields = objConfig.Fields

          ' Set config fields we care about
          With Fields
            .Item(cdoSendUsingMethod)       = cdoSendUsingPort
            .Item(cdoSMTPServer)            = "localhost"
            .Item(cdoSMTPServerPort)        = 25
            .Item(cdoSMTPConnectionTimeout) = 10
            .Item(cdoSMTPAuthenticate)      = cdoNone
          End With

          Set objMessage = Server.CreateObject("CDO.Message")
          Set objMessage.Configuration = objConfig
          SendEmail = ""      
          On Error Resume Next
          With objMessage
            .To       = sTo
            If Not IsNull(sCc) Then .Cc      = sCc
            If Not IsNull(sBcc) Then .Bcc      = sBcc
            .From     = sFrom
            .Subject  = sSubject
            .TextBody = sBody
            '.HTMLBody = "<html><body><p>" & Replace(sBody, vbCrLf, "<br>") & "</p></body></html>"
          End With
          If Err.Number <> 0 Then
                SendEmail = CStr(Err.Number) & ": " & Err.Description
          End If
          On Error Goto 0
          Set Fields = Nothing
          Set objMessage = Nothing
          Set objConfig = Nothing
    End Function

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now