Skip to Content
Technical Articles
Author's profile photo Jai Vignesh R

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. 🙂

Assigned Tags

      14 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sugandhan Vazhumuni
      Sugandhan Vazhumuni

      Nice blog

       

      Author's profile photo Saranya Sampath
      Saranya Sampath

      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

      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      Hi Saranya,

      Try including \\ (double slashes and kindly let me know)

      “D:\\Sample\\bin\\test.jpg”

      Best Regards,

      Jai Vignesh R

      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      Thank you 🙂

      Author's profile photo Doan Ha
      Doan Ha

      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!

      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      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

      Author's profile photo Doan Ha
      Doan Ha

      Hi Bro Jai Vignesh R

      Thanks you Bro.. my program could not attached files into outlook...

      Is there any thing incorrect here ??

       

      GLOBAL.step({ Custom: function(ev, sc, st) {
      	var rootData = sc.data;
      	ctx.workflow('SendMailFileIncorrect', 'c41f9a51-e9b6-4fb2-89ab-b6f8acc8023d') ;
      	// Custom
      	
      	var fso = new ActiveXObject("Scripting.FileSystemObject");
      	var path = "C:\\Temp\\";
      	var files = ctx.fso.folder.getFileCollection(path, true);
      	var i = 0;
      	
      	//if (!fso.FileExists(path+ "\\<filename>")) {
      	if (files.length > 0) {
        	function sendMail(){
      		try {
      			ctx.outlook.init();
      			ctx.outlook.mail.create({To:"abc@gmail.com", Subject:"Test", Body : "Hi\n TEst only."})
      			
      			var folder = 'C:\\Temp\\';
       		// get result as an Enumerator
       			var files = ctx.fso.folder.getFileCollection( folder );
       			for ( ; !files.atEnd( ); files.moveNext( ) ) {
         			var file = files.item( );
         			var filename = file.Name;
         			var filedate = file.DateCreated;
         			var filepath = file.Path;
      			}
      			// get result as an Array
       			var files = ctx.fso.folder.getFileCollection( folder, true );
       			ctx.each( files, function( file, item ) {
         			var filename = file.Name;
         			var filedate = file.DateCreated;
         			var filepath = file.Path;
      			ctx.outlook.mail.attach(0,"C:\\Temp\\" & filepath);
       			} );
      			
      			var res = ctx.outlook.mail.send(0);
      			ctx.outlook.end();
      		} catch (err) {
      			ctx.log("Sending of \"Microsoft Outlook mail in failure ("+err.description+").\"");
      			try {
      			ctx.outlook.end();
      			} catch (ex) {
      				ctx.log("issue during outlook.end");
      				return e.error.KO;
      			}
      			return e.error.KO;
      		}
      		return e.error.OK;
      	}
      
      		sendMail();
      	} 
      	else {
          // file does not exist!
      }
      
      	sc.endStep(); // end Scenario
      	return;
      }});
      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      Hello Doan Ha

      Please use this along with the folder path specified.

      // get result as an Array
       			var files = ctx.fso.folder.getFileCollection( folder, true );
       			ctx.each( files, function( file, item ) {
         			var filename = file.Name;
         			var filedate = file.DateCreated;
         			var filepath = file.Path;
      } );

      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.

       

      Author's profile photo Doan Ha
      Doan Ha

      Hello Jai Vignesh R

      I have changed something as below. It has anything incorrectly there ?

      var path = "C:\\Temp\\";
      var files = ctx.fso.folder.getFileCollection(path, true);
      ctx.each( files, function( file, item ) {
         	var filename = file.Name;
         	var filedate = file.DateCreated;
         	var filepath = file.Path;
      	var fileNames = [];
      	fileNames.push(filename)
      	ctx.outlook.mail.attach(0,fileNames);
      } );
      Author's profile photo Doan Ha
      Doan Ha

      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 ?

      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      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"]);

       

      Author's profile photo Doan Ha
      Doan Ha

      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] );

       

      Author's profile photo Jai Vignesh R
      Jai Vignesh R
      Blog Post Author

      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

      Author's profile photo Doan Ha
      Doan Ha

      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!

      GLOBAL.step({ Custom: function(ev, sc, st) {
      	var rootData = sc.data;
      	ctx.workflow('SendMailFileIncorrect', 'c41f9a51-e9b6-4fb2-89ab-b6f8acc8023d') ;
      	// Custom
      	
      	var fso = new ActiveXObject("Scripting.FileSystemObject");
      	var path = "C:\\Temp\\";
      	var files = ctx.fso.folder.getFileCollection(path, true);
      	
      	//if (!fso.FileExists(path+ "\\<filename>")) {
      	if (files.length > 0) {
        	function sendMail(){
      		try {
      			ctx.outlook.init();
      			ctx.outlook.mail.create({To:"abc@gmail.com", Subject:"Test", Body : "Hi\n Test."})
      			
      // get result as an Array
      ctx.each( files, function( file, item ) {
      var filename = file.Name;
      var fileNames = [];
      fileNames.push(filename)
      ctx.outlook.mail.attach(0, "C:\\Temp\\" & filename);
      } );
      			
      var res = ctx.outlook.mail.send(0);
      ctx.outlook.end();
      		} catch (err) {
      			ctx.log("Sending of \"Microsoft Outlook mail in failure ("+err.description+").\"");
      			try {
      			ctx.outlook.end();
      			} catch (ex) {
      				ctx.log("issue during outlook.end");
      				return e.error.KO;
      			}
      			return e.error.KO;
      		}
      		return e.error.OK;
      	}
      
      		sendMail();
      	} 
      	else {
          // file does not exist!
      }
      
      	sc.endStep(); // end Scenario
      	return;
      }});