Sometimes it might be necessary to send a message by email (in HTML form) through a VBS or a batch script....so how could we create a simple email with common fields to send later?
Here's the sample code:
Dim strBody Dim strBody0 Dim strBody1 Dim strBody2 Dim strBody3 Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Any Subject" objMessage.From = "[email protected]" objMessage.To = "[email protected]" objMessage.AddAttachment "C:\text.txt" strBody0 = "<p align=center><h1><u>AnyText</u></h1>" strBody1 = "<br>AnYText" strBody2 = "<br><ul><li>AnyText</li></ul>" strBody3 = "AnyText<br>" strBody4 = "<br>Final AnyText</p>" strBody = strBody0 & strBody1 & strBody2 & strBody3 & strBody4
Defines the message will be sent with HTML content;
objMessage.HTMLBody = strBody
If you want to send an email using the network, set the following property to "2";
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Defines the IP Address of your SMTP Server (it has to be reachable and ping-able);
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "{ip}"
Set to NONE if your SMTP lets anonymous users to send email through him;
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = NONE
Defines the "Port", trying to send the e-mail, to establish a valid connection to your SMTP Server;
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
If your SMTP Server is configured to accept only SSL connection requests set this property to "True";
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
If you would to be sure to be able to send the e-mail even in high-traffic environments, or even if you're trying to send a very-large-emails, try to increase this value to 120 or higher;
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 objMessage.Configuration.Fields.Update
Finally Send the e-mail.
objMessage.Send
So, this is a sample code ready to send an e-mail, remember to set the ip address of your SMTP Server and your Port, through a VBS script.
Email with Authentication
If you want to let the script to authenticate to the SMTP server/service, you will have to modify/add the following properties in the Configuration.Items block:
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
It's obvious that you could insert parameters to this VBS and pass them by a batch DOS, a Powershell custom cmdlet or something else.
It could be a future article to be posted. So stay tuned. :-)