[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

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 = "system@mydomain.com"
    objEmail.To = "itsupport@mydomain.com"
    objEmail.Subject = strSubject 
    objEmail.Textbody = strText
    objEmail.AddAttachment strAttachedFile
    objEmail.Configuration.Fields.Item _
    	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item _
    	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpserver.domain.local"
    objEmail.Configuration.Fields.Item _
    	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 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.
  • 2
1 Solution
Neil RussellTechnical Development LeadCommented:
From http://www.paulsadowski.com/wsh/cdo.htm

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"" <me@my.com>" 
objMessage.To = "test@paulsadowski.com" 
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 _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"

'Server port (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

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now