How to Send Email using VBScript and GMail (SSL)?


Sending Email on Linux is very easy, which is made possible by installing the sendmail package and then using the mail command line utility. On Windows, you can use the VBScript or JScript that runs on Windows Scripting Host. Sending Email Notifications is a very commonly administrative task, for example, if there are things that do not run well, you are expected to get email notifications sent by the system automatically.

To send a email via VBScript, you can then use the following VBScript,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
' VBScript to Send Email Notification
' Author: https://helloacm.com
' Usage: cscript.exe sendemail.vbs email subject text
' 23/Dec/2014
 
Sub SendEmail(ToAddress, Subject, Text)
    Dim iMsg 
    Dim iConf
    Dim Flds
 
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
 
    iConf.Load -1
    Set Flds = iConf.Fields
    
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "gmail account"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "gmail password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" 'smtp mail server
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 'stmp server
        .Update
    End With
 
    With iMsg
        Set .Configuration = iConf
        .To = ToAddress
        .From = "dr.justyy.lai at gmail.com"
        .Subject = Subject
        .TextBody = Text
        .Send
    End With
 
    Set iMsg = Nothing
    Set iConf = Nothing
End Sub
 
If WScript.Arguments.Count <> 3 Then
    WScript.Echo "Usage: cscript.exe " & WScript.ScriptFullName & " email subject text"
Else 
    SendEmail WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2)
End If
' VBScript to Send Email Notification
' Author: https://helloacm.com
' Usage: cscript.exe sendemail.vbs email subject text
' 23/Dec/2014

Sub SendEmail(ToAddress, Subject, Text)
    Dim iMsg 
    Dim iConf
    Dim Flds

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1
    Set Flds = iConf.Fields
    
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "gmail account"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "gmail password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" 'smtp mail server
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 'stmp server
        .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .To = ToAddress
        .From = "dr.justyy.lai at gmail.com"
        .Subject = Subject
        .TextBody = Text
        .Send
    End With

    Set iMsg = Nothing
    Set iConf = Nothing
End Sub

If WScript.Arguments.Count <> 3 Then
    WScript.Echo "Usage: cscript.exe " & WScript.ScriptFullName & " email subject text"
Else 
    SendEmail WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2)
End If

To use the above script, first you would need to replace the account name and password of gmail, then you can run via the command line:

C:\Windows\system32>cscript.exe sendemail.vbs [email protected] "Hello World" "//helloacm.com"
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Windows\system32>

Now, if something goes wrong (for instance, someone just commits the code that causes compilation problems or the code does not pass the unit tests), then you will get email notifications on time so that this won’t become too difficult to fix as more commits are submitted.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
426 words
Last Post: Anker Wireless Bluetooth Speaker
Next Post: Batch Script: Return Value Via ErrorLevel Code from Sub Function or Script

The Permanent URL is: How to Send Email using VBScript and GMail (SSL)?

2 Comments

  1. francobe
  2. yalcinn

Leave a Reply