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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5793
  • Last Modified:

Email with vbsript

I am trying to setup an email program with vbsript using Microsoft Online Service which are company uses. Microsoft requires the item below which I do not know how to do.

  Make sure that Transport Layer Security (TLS) is enabled in your SMTP client.
o    On most client applications, this is under “My outgoing mail server (SMTP) requires an encrypted connection (SSL)” – this should be configured “Yes”

Does someone have an example script of how to setup an email program with Microsoft Online Serverices? I do have the smtp address and a user name and password with them.
0
dthillsr
Asked:
dthillsr
  • 6
  • 3
1 Solution
 
RobSampsonCommented:
Hi, try this VBScript code from here:
http://www.paulsadowski.com/wsh/cdo.htm

Under the section titled:
Sending a text email using authentication against a remote SMTP server

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") = True

'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


Just change the From, To, SMTPServer, and username and password.

Regards,

Rob.
0
 
dthillsrAuthor Commented:
This will not work for me. I get an error, "The transport failed to connect to the server".

I had to change the following lines that come from Microsofts Site form emailing from a script using Microsoft Online.
http://blogs.technet.com/b/msonline/archive/2009/09/02/using-smtp-relay-with-exchange-online.aspx

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

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587

Below are the instructions from Microsoft but I m just not getting it.
-----------------------------------------------------------------------------------------------------------------------
To connect to the Microsoft Online Services SMTP server, open the SMTP client application and provide the following information:
·         The fully qualified domain name (FQDN) of the Microsoft Online Services SMTP server. The FQDN will be slightly different depending on which Microsoft Online Services Data Center your organization is connected to. The server FQDNs are:
o    North America Data Center:  Smtp.mail.microsoftonline.com
o    Europe Data Center:  Smtp.mail.emea.microsoftonline.com
o    Asia Pacific Data Center:  Smtp.mail.apac.microsoftonline.com
 

·         The user name and password of a Microsoft Online Services user account with an Exchange Online license.
o    On most client applications, this is under “My outgoing mail server (SMTP) requires authentication” – this should be configured “Yes”, and the corresponding Microsoft Online username and password supplied.
 

·         The SMTP port to use: 587.
o    On most client applications, this is under “Server Port Numbers for Outgoing Server (SMTP)” – this should be configured to use port 587
 

·         Make sure that Transport Layer Security (TLS) is enabled in your SMTP client.
o    On most client applications, this is under “My outgoing mail server (SMTP) requires an encrypted connection (SSL)” – this should be configured “Yes”
 

NOTE:  The FROM address must use a SMTP domain of type “Authoritative” – this can be confirmed in Microsoft Online Administration Center, under the Users - Domains tab.
-----------------------------------------------------------------------------------------------------------------------

I am currently logged on to Microsoft Online Services with the account information I tried in the code sent  in the code above so I know that part is good. I would appreciate any other help you could provide.
0
 
RobSampsonCommented:
The SSL configuration should be the TLS settings:
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

Try this version anyway.

Regards,

Rob.
Dim iMsg
Dim iConf
Dim Flds
Const cdoSendUsingPort = 2
Const cdoBasic = 1
Const cdoNTLM = 2
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
	.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Smtp.mail.microsoftonline.com"
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
	.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username@domain.com"
	.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
	.Update
End With
With iMsg
	Set .Configuration = iConf
	.From = "youruser@domain.com"
	.To = "anotheruser@domain.com"
	.Subject = "This is a test message"
	.HTMLBody = "Test Message"
	.Send
End With
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
WScript.Echo "Mail Sent!"

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mvozilaCommented:
I can not get this to work. Been trying for hours. I keep getting the server rejected the sender address error message. I tried my code and the code above and get the same error message. Any ideas? I am on Office 365.
0
 
RobSampsonCommented:
It depends on your environment I guess.  Make sure you change these:
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Smtp.mail.microsoftonline.com"
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587

Open in new window


to match your SMTP gateway, and if you require or don't require authentication, change these to match your environment:
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
	.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username@domain.com"
	.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
	.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

Open in new window


You can omit all of those lines if you don't need authentication, but if you do this, I'm pretty sure the workstation you're emailing from will need to be allowed through the anti-relaying rules of your SMTP server.

Regards,

Rob.
0
 
mvozilaCommented:
Thank you. I am using SMTP Authentication and below is the error.

SMTP Client not Authenticated
I have smtpauthentication set as False though to allow for TLS. If I try true I get a transport error.

Transport Error
SMTP Transport Error
Here is my code:
Dim iMsg
Dim iConf
Dim Flds
Const cdoSendUsingPort = 2
Const cdoBasic = 1
Const cdoNTLM = 2
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "MSFTServer.outlook.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "blah@mydomain.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")= "addmypassword"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
      .Update
End With
With iMsg
      Set .Configuration = iConf
      .From = "blah@mydomain.com"
      .To = "blah@mydomain.com"
      .Subject = "This is a test message"
      .HTMLBody = "Test Message"
      .Send
End With
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
WScript.Echo "Mail Sent!"

In the rest of my organization I use an IIS smtp server to act as a smart host to pass things off to Office 365. That works great. However, in this case I can't do that because the VLAN I am on now will not integrated into this network for some time and that SMTP server isn't open to the outside.  Therefore, I am trying to send an e-mail as an autenticated user (same account and settings as used on my IIS server).
0
 
RobSampsonCommented:
If your server is expecting TLS, you will need to have
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

and not False.
0
 
mvozilaCommented:
Ok, great. Thanks. If I do that I get the transport error message I attached.
0
 
RobSampsonCommented:
Are you sure you have the right SMTP port specified?  Try changing
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort

to
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1

If you grab a utility like bmailer.exe or blat.exe, can you run a command line test from the same network points to see if you can route mail through it?  On the mail server, are there any events about connections from the source?

Rob.
0
 
RobSampsonCommented:
Also try the SMTP server by IP address instead of DNS name.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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