To Handle Ad Blocker

VBScript to send email using Outlook

In this Article, we will learn how to send email using VBScript programs using Outlook.

During RPA Development for a process, we requires to send Automated emails response/notifications to notify the user for a processing start or it's end. We also requires to send output of the RPA Execution. For this purpose most of the Automation Tools (like Automation Anywhere) requires SMTP Host name and its port number.


It happens that sometimes IT team do not provide you the SMTP host name and port number due to security purpose. In such cases, you have two solutions as options:


1- Automated Outlook (Not Recommended): Mimic all the action of human to send email from Outlook. Such as open a new email message in outlook, write recipient email address in "To" field, write some silly text in body and then click on sends button.

2- Use Custom Program (Recommended): It will be a script or it could be a .Dll file as well. By the time you open a new email message in outlook, such custom programs shoots mail to recipient.

If you have Outlook configured with an email address in your machine then you can use below script for sending automated email as Custom Program Solution.

On Error Resume Next
vEmailFrontUserID = WScript.Arguments.Item(0)
vEmailAddressTo = WScript.Arguments.Item(1)
vEmailAddressCC = WScript.Arguments.Item(2)
vEmailSubject = WScript.Arguments.Item(3)
vEmailAttachment = WScript.Arguments.Item(4)
vEmailBody = WScript.Arguments.Item(5)
vEmailSignature = WScript.Arguments.Item(6)

vEmailBody = Replace(vEmailBody, vbLf, "<br>")
vEmailBody = vEmailBody & "<br>"

Dim vObjOutlookApp : Set vObjOutlookApp = Wscript.CreateObject("Outlook.application")
Dim vObjOutlookAppNameSpace : Set vObjOutlookAppNameSpace = vObjOutlookApp.getNamespace("MAPI")
Dim vObjOutlookAppNewMailItem : Set vObjOutlookAppNewMailItem = vObjOutlookApp.CreateItem(olMailItem)
Dim vObjFSO : Set vObjFSO = CreateObject("Scripting.FileSystemObject")

vObjOutlookAppNewMailItem.SentOnBehalfOfName = vEmailFrontUserID
vObjOutlookAppNewMailItem.To = vEmailAddressTo
vObjOutlookAppNewMailItem.CC = vEmailAddressCC
vObjOutlookAppNewMailItem.Subject = vEmailSubject

If vEmailAttachment <> "" Then
    vAttachmentsArr = Split(vEmailAttachment,";")
    For i = LBound(vAttachmentsArr) To UBound(vAttachmentsArr)
        If vAttachmentsArr(i) <> "" Then
            If vObjFSO.FileExists(Trim(vAttachmentsArr(i))) = True Then
                vObjOutlookAppNewMailItem.Attachments.Add Trim(vAttachmentsArr(i))
            End If
        End If
    Next
End If

If vObjFSO.FileExists(vEmailSignature) = True Then
    vObjOutlookAppNewMailItem.Attachments.Add vEmailSignature , 1, 0
    vEmailBody = vEmailBody & "<BR> <IMG src=""" & StrReverse(Mid(StrReverse(vEmailSignature),1,(InStr(StrReverse(vEmailSignature),"\")-1))) & """><BR>"
End If

vEmailBody = "<HTML><BR>" & vEmailBody & "<BR></HTML>"
vObjOutlookAppNewMailItem.HTMLBody = vEmailBody
vObjOutlookAppNewMailItem.Send

Set vObjOutlookApp = Nothing
Set vObjOutlookAppNameSpace = Nothing
Set vObjOutlookAppNewMailItem = Nothing
Set vObjFSO = Nothing

If Err.Number <> 0 Then
    Set vObjOutlookApp = Nothing
    Set vObjOutlookAppNameSpace = Nothing
    Set vObjOutlookAppNewMailItem = Nothing
    Set vObjFSO = Nothing
End If


I hope this post is helpful. If you have any questions, feel free to comment it below.

Happy Learning.. 😏