SMTP authentication in VB script sending email

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.
Who is Participating?
Neil RussellConnect With a Mentor Technical Development LeadCommented:

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

Neil RussellTechnical Development LeadCommented:
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
ipajonesAuthor Commented:
Great, all done and working.  Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.