MS Outlook Module

This interface allows us to use Microsoft Outlook in our robots. It provides two types of actions:

  • Reading: It's possible to list emails of a folder, move them, save them, extract their attachments or their recipients.
  • Sending: We can reply, forward a received email or send a new one.

To use the MS Outlook module in our projects, we need to include the following dependency.

1
2
3
4
5
<dependency>
    <groupId>com.novayre.jidoka.module</groupId>
    <artifactId>jidoka-msoutlook-api</artifactId>
    <version>${jidoka.version}</version>
</dependency>

To connect to Outlook, the MS Outlook module uses Jacob COM Bridge. It is necessary to save the Jacob-1.18-x64.dll library in the execution resource, in the same directory as the AppianRPAagent.exe executable file. Download the library.

In addition, the execution resource must have the Microsoft Outlook Client installed and have a valid email account configured.

To get an instance of IJidokaOutlook, we have to use the following method:

1
IJidokaOutlook outlook = IJidokaOutlook.getInstance(this);

Where this refers to the class that implements the IRobot interface, that is, the class that represents our robot.

When the robot finishes, we always have to run the close() method, making sure there's no emails pending to save.

1
outlook.close();

The IJidokaOutlook interface was developed using Outlook VBA as reference. The elements created implement a some of the objects, methods and properties defined in this architecture, but with a slightly different approach.

Once this interface is initialized, we can obtain the corresponding manager instances depending on the task to implement.

  • IOlFolderManager getOlFolderManager(): This method allows us to work with the folders in the Outlook account configured on the resource.
  • IOlMailManager getOlMailManager(): This method allows us to manage existing emails and create new ones to be sent.

In the following examples, we will show how to use the IOlFolderManager and IOlMailManager interfaces.

IOIFolderManager

This interface is used to manage the folders of an Outlook account configured in the execution resource.

Let's describe its main features.

List folders

The first method you will likely work with is the one that shows the path of all the folders contained in the Outlook account. Apart from allowing us to validate the access to the account, we will be able to get the complete path of the folders we want to work with. Next, we show the call to the getAllFolderList() method, which performs this task and returns a list of IOlFolder objects with details of each of them.

1
2
3
4
5
server.info("Show Folders:");
List<IOlFolder> foldersList = outlook.getOlFolderManager().getAllFoldersList();
for (IOlFolder folder : foldersList) {
    server.info("Folder Path <" + folder.getFolderPath()+">");
}

Also, we can get the list of folders contained in another one. The method to use is getFolders() of the IOlFolder object. In the following example, the root folder is searched using the findMailFolder() method, which returns one or more objects that match the criteria specified by IOlFolderFW.

1
2
3
4
5
6
7
8
9
IOlFolderFW criteria = new OlFolderFW();
criteria.setFolderPath("\\\\myaccount@mycompany.com POP");
          
List<IOlFolder> folderList = outlook.getOlFolderManager().findMailFolder(criteria);
IOlFolder mainFolder = folderList.get(0);
          
for (IOlFolder folder : mainFolder.getFolders()) {
    server.info("- " + folder.getFolderPath());
}

List emails

To get the list of emails inside a folder, we use getMailList(). This method needs an input parameter with the reference to the source folder.

The result is a list of objects of type IOlMailItem.

1
2
3
4
5
6
7
8
9
IOlFolderFW criteria = new OlFolderFW();
criteria.setFolderPath("\\\\myaccount@mycompany.com POP\\Inbox");
List<IOlFolder> folderList = outlook.getOlFolderManager().findFolder(criteria);
         
List<IOlMailItem> mailList = outlook.getOlFolderManager().getMailList(folderList.get(0).getEntryID());
         
for (IOlMailItem mailItem : mailList) {
    server.info("Mail Subject <"+mailItem.getSubject()+">");
}

IOIMailManager

This interface will allow us to manage emails and create new ones to be sent.

Read an email

Before analyzing the available methods in this interface, we will show the properties of the IOlMailItem object. This object contains all the information about a specific email.

Below there is an example of how to print the properties of this object.

1
2
3
4
5
6
7
8
9
10
11
12
List<IOlMailItem> mailList = outlook.getOlFolderManager().getMailList(folderEntryId);
                  
for (IOlMailItem mailItem : mailList) {
    server.info("Mail Subject <"+mailItem.getSubject()+">");
    server.info("     Sender <"+mailItem.getSenderEmailAddress()+">");
    server.info("     To <"+mailItem.getTo()+">");
    server.info("     CC <"+mailItem.getCc()+">");
    server.info("     BCC <"+mailItem.getBcc()+">");
    server.info("     HtmlBody <"+mailItem.getHtmlBody()+">");
    server.info("     Body <"+mailItem.getBody()+">");
    server.info("     CreationTime <"+mailItem.getCreationTime()+">");
}

The IOlMailItem object contains much more properties that can consulted in the interface and that are based on the properties of the VBA MailItem object.

Show an email

To display an email on the screen, you must use the showMail() method of the IOlMailManager interface.

1
outlook.getOlMailManager().showMail(mailItem);

Close an email

To close it, we use the method closeMail().

1
outlook.getOlMailManager().closeMail(mailItem.getEntryID(),EInspectorClose.SAVE);

Save attached files

To extract attachments from emails, the IOlMailManager interface implements the downloadAttachments() method. In the following example, we save the attachments in a temporary directory that is deleted at the end of the execution.

1
2
3
4
5
6
7
IOlMailItem mailItem = mailList.get(0);
File directory = FileUtil.createTempDirectory();
outlook.getOlMailManager().downloadAttachments( mailItem, directory.getAbsolutePath());
         
for (File fileDownloaded : directory.listFiles()) {
    server.info(String.format("Downloaded file %s", fileDownloaded.getAbsolutePath()));
}

Save an email

The MS Outlook module offers the possibility to save the desired email in a file. The method is saveMail(). Here is an example of saving an email in a temporary file.

1
2
3
4
File directory = FileUtil.createTempDirectory();
File messageFile = FileUtils.getFile(directory, mailItem.getSubject()); 
outlook.getOlMailManager().saveMail(mailItem, messageFile.getAbsolutePath());
server.info(String.format("Downloaded Message to %s",   messageFile.getAbsolutePath()));

Move an email

To move an email between two folders, you must use the move() method. This method needs two parameters, the IOlMailItem object to move and the reference to the target folder. Here is an example.

1
2
3
4
5
IOlFolderFW destFolderCriteria = new OlFolderFW();
destFolderCriteria.setFolderPath("\\\\myaccount@mycompany.com POP\\Inbox\\destFolder");
List<IOlFolder> folderDestList = outlook.getOlFolderManager().findFolder(destFolderCriteria);
IOlFolder target = folderDestList.get(0);
outlook.getOlMailManager().move(mailItem, target);

Create an email

The following method allows a robot to create a new email. The call to createMail() returns an IOlMailItem object. This mail is not sent but stored in the drafts folder.

In the following example, after creating the email we add some attachments and close the Outlook window with corresponding to the created mail.

1
2
3
4
5
6
7
8
9
10
IOlMailItem newMail = new OlMailItem();
newMail.setSubject("Subject for IJidokaOutlook mail");
newMail.setTo("toAccount@myCompany.com");
newMail.setCc("ccAccount@myCompany.com");
newMail.setBody("Body for IJidokaOutlook mail");
IOlMailItem createdMail = outlook.getOlMailManager().createMail(newMail, true);
         
outlook.getOlMailManager().addAttachmets(createdMail, "C:\\temp\\attachment.pdf");
 
outlook.getOlMailManager().closeMail(createdMail.getEntryID(),EInspectorClose.SAVE);

Send

With the sendMail() method we can send a new email previously created with the createMail() method. The shown code is similar to the previous one, but instead of closing the email, we're sending it.

1
outlook.getOlMailManager().sendMail(createdMail);

Reply

Using the IOlManager interface you can reply or reply to all a received email, just adding an additional optional text.

1
outlook.getOlMailManager().replyMail(mailItem, "This is a reply");
1
outlook.getOlMailManager().replyAllMail(mailItem, "This is a reply");

Forward

It is also possible to forward an email as Outlook does. In this case, it is necessary to complete the destination addresses before executing the method forward().

1
2
3
4
5
6
IOlMailItem mailItem = mailList.get(0);
 
mailItem.setTo("forwardAccount@myCompany.com");
mailItem.setCc("");
         
outlook.getOlMailManager().forward(mailItem, "This is a Forward");
Open in Github Built: Fri, Nov 12, 2021 (02:39:09 PM)

On This Page

FEEDBACK