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
    objEmail.Configuration.Fields.Update
    objEmail.Send
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.
--IJ
LVL 6
ipajonesAsked:
Who is Participating?
 
Neil RussellConnect With a Mentor Technical 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

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send

Open in new window

0
 
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
0
 
ipajonesAuthor Commented:
Great, all done and working.  Thanks
--IJ
0
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.