Technical Articles
How to create a mail including attachments with Microsoft Outlook in SAP Intelligent Robotic Process Automation?
One of the most common use cases while creating a bot is to notify a person about the results or logs of the operation executed using a bot.
SAP Intelligent Robotic Process Automation provides easy integration with Microsoft Outlook.
Note: The pre-requisite step is to include the Outlook library script.
Right-click on the Scripts pane and click on the “Include library Script” option.
In the corresponding Add Library Script Popup, check “Outlook Integration” option.
Now, let us see a step by step procedure to create a mail.
Step 1: Initialize the Outlook application.
ctx.outlook.init();
Step 2: Create the mail with the recipient mail address including subject and mail body.
ctx.outlook.mail.create( {
To: <Recipient Mail Address>,
Subject: <Mail Subject>,
Body: <Mail Body>
});
The body is of type string.
You could set the appropriate mail body format using the following snippet. By default, it is of HTML format.
ctx.outlook.mail.setBodyFormat(0, 1);
where the second parameter “1” represents a plain body format. The various options available are
0 (unspecified), 1 (plain), 2 (HTML), 3 (rich text)
Step 3: You could set the importance of the mail using the following snippet.
ctx.outlook.mail.setImportance(0, 2);
where the second parameter “2” represents a mail of high importance. The various options available are
1 (default), 2 (high), 0 (low)
In case, an acknowledgment on receipt of the mail is required, use the following snippet.
ctx.outlook.mail.setAskAR(0);
Step 4: You could include attachments to the mail using the following snippet.
ctx.outlook.mail.attach(0, <File Location>);
If multiple attachments are to be attached, then provide the file locations in an array as follows,
ctx.outlook.mail.attach(0, [<File 1 Location>, <File 2 Location>, <File 3 Location>]);
Step 5: Final step? No. This is the pre-final step. Send the mail.
ctx.outlook.mail.send(0);
In all the above snippets, the first parameter “0” represents the mail index of the working mail collection.
Step 6: Final and important step. It is necessary that an initialized Outlook application is ended properly using the following snippet.
ctx.outlook.end();
For more information, refer to the wiki.
That’s it, folks. 🙂
Nice blog
I am trying to send attachment in mail.I kept my file inside project path bin.
I tried direct path "D:\\Sample\bin\test.jpg" and
ctx.option.path.bin + "text.jpg"
I am getting error as "attachment is null or not an object"
Thanks
Saranya
Hi Saranya,
Try including \\ (double slashes and kindly let me know)
“D:\\Sample\\bin\\test.jpg”
Best Regards,
Jai Vignesh R
Thank you 🙂
Hi Saranya.
If i want to loop some files in folder ("C\Temp\Files\") and attach all of them into outlook while sending email.
How to do that ?
Thanks in advance!
Hi Doan Ha ,
I have mentioned the same in the above blog on how to attach multiple files. To fetch those files from a specific folder refer here.
Thanks and Regards,
Jai Vignesh R
Hi Bro Jai Vignesh R
Thanks you Bro.. my program could not attached files into outlook...
Is there any thing incorrect here ??
Hello Doan Ha
Please use this along with the folder path specified.
and then you get only the file names and push them into an array. Then you should basically use the syntax ctx.outlook.mail.attach(0, [<File 1 Location>, <File 2 Location>, <File 3 Location>]);
to attach the files.
Hello Jai Vignesh R
I have changed something as below. It has anything incorrectly there ?
i don't understand the syntax : ctx.outlook.mail.attach(0, [<File 1 Location>, <File 2 Location>, <File 3 Location>]);
What's File1 Location , File2 Location... ????? I need to push array of filename replace them.
How to do that ?
Hi Doan Ha
Your code mentioned on top is right. Please keep the outlook snippet out of the loop.
Make sure that the file names are specifying the paths as shown below.
Example syntax:
ctx.outlook.mail.attach(0, ["C:\\Temp\\Example1.xlsx", "C:\\Temp\\Example.xlsx", "C:\\Temp\\Example3.xlsx"]);
Hi Jai Vignesh R
Thanks you .
But the file names always be changed, it not fixed. So could not defined correct file names.
I have used some below syntaxs to attach files into outlook but it not correct.
ctx.outlook.mail.attach( 0, [ “C:\\RPA Projects\\RPA020\\Files\\SubFiles Incorrect\\” & fileNames ] );
ctx.outlook.mail.attach( 0, “C:\\RPA Projects\\RPA020\\Files\\SubFiles Incorrect\\” & [fileNames] );
Hi Doan Ha
You could just specify the folder. Automatically, the files present under it irrespective of the name will be taken into consideration.
Best Regards,
Jai Vignesh R
Hi Bro Jai Vignesh R
Sorry but i really could not understood your mean.
What's incorrectly points in my below code ?
I could not attached all files into outlook.
Thanks you!