SMTP authentication in VB script sending email

Posted on 2011-10-26
Last Modified: 2012-05-12
I've been successfully using the following function in a VB script to send an email:

'SendMail function - create and send an email message with attachment
Function SendMail(strSubject, strText, strAttachedFile)
    Dim objEmail
    Set objEmail = CreateObject("CDO.Message")
    'objEmail.MIMEFormatted = False
    objEmail.From = ""
    objEmail.To = ""
    objEmail.Subject = strSubject 
    objEmail.Textbody = strText
    objEmail.AddAttachment strAttachedFile
    objEmail.Configuration.Fields.Item _
    	("") = 2
    objEmail.Configuration.Fields.Item _
    	("") = "smtpserver.domain.local"
    objEmail.Configuration.Fields.Item _
    	("") = 25
End Function

Open in new window

Everything is working fine with the above.  I need to add in SMTP server authentication as this is now required on our SMTP server.  Can anyone provide the correct additional configuration to enable me to do this - if it's possible

Thanks in advance.
Question by:ipajones
    LVL 37

    Accepted Solution


    Sending a text email using authentication against a remote SMTP server. 
    More and more administrators are restricting access to their servers to control spam or limit
    which users may utilize the server. This example shows you how to use basic authentication,
    the most commonly used authentication method, when the SMTP server you are using requires it. 
    This code is slightly more complex but not very difficult to understand or work with.
    Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
    Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
    Const cdoAnonymous = 0 'Do not authenticate
    Const cdoBasic = 1 'basic (clear-text) authentication
    Const cdoNTLM = 2 'NTLM
    Set objMessage = CreateObject("CDO.Message") 
    objMessage.Subject = "Example CDO Message" 
    objMessage.From = """Me"" <>" 
    objMessage.To = "" 
    objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
    '==This section provides the configuration information for the remote SMTP server.
    objMessage.Configuration.Fields.Item _
    ("") = 2 
    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("") = ""
    'Type of authentication, NONE, Basic (Base64 encoded), NTLM
    objMessage.Configuration.Fields.Item _
    ("") = cdoBasic
    'Your UserID on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("") = "youruserid"
    'Your password on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("") = "yourpassword"
    'Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    ("") = 25 
    'Use SSL for the connection (False or True)
    objMessage.Configuration.Fields.Item _
    ("") = False
    'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
    objMessage.Configuration.Fields.Item _
    ("") = 60
    '==End remote SMTP server configuration section==

    Open in new window

    LVL 37

    Expert Comment

    Oh and you will often find that SMTP servers using secure authentication are not on port 25, ask your admins.

    The important lines for you are 31-49
    LVL 6

    Author Closing Comment

    Great, all done and working.  Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 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

    10 Experts available now in Live!

    Get 1:1 Help Now