Download email attachment with UiPath

The article presents an easy and fast way on how to build an email automation that downloads all attachments into a specific folder. Of course, this example can be extended to an even more complex degree. To give you an idea you can filter the emails or download only files with a specific extension. But this is all up to you. Be creative. I’ll give you some hints along the way.

We will start with a new automation process. Let’s call it Email Automation.

Email Automation UiPath process

Add a new Flowchart and inside drag and drop a new Sequence. Rename the Sequence to Download Email Attachment.

UiPath Flowchart

Open the sequence and create 3 new variables:

  • EmailAddress – String type
  • EmailPassword – String type
  • MailMessages – List<MailMessage> type (System.Net.Mail.MailMessage)
UiPath Sequence: 3 new variables

Drag and drop a new Assign activity and set the EmailAddress to your email address.

Drag and drop a new Get Password activity and set the EmailPassword variable. On Misc section set the Password and on Result select EmailPassword variabe.

Next, drag and drop Get IMAP Mail Message. Make the following changes:

  • Set MailFolder to “Inbox”
  • Port to 993
  • Server to “imap.gmail.com”
  • Email to EmailAddress variable
  • Password to EmailPassword variable
  • Top to 10 (get the first 10 emails)
  • Save the Messages to MailMessages variable

Drag and drop a new For Each activity and inside of it drop an If activity. It should look like this:

For each item in the MailMessages check if the subject contains a specific word. For example, get emails with a request word in the subject. Use Contains method.

item.Subject.ToLower.Contains(“request”)

https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage?view=netframework-4.8

Inside Then branch drag and drop the Save Attachment activity. Set the Message as item variable and select the path where to download the email attachment.

If you want to download only specific files use the Filter option on Save Attachment activity. For example, if you want to download only .jpg and .png files use this regular expression as a filter:

“^.+.(([pP][nN][gG])|([jJ][pP][gG]))$”

Before running the email automation please configure your Gmail account for IMAP/POP3 protocols and generate and use an App password from Google. For more details follow this link (source: UiPath)

https://docs.uipath.com/studio/docs/enabling-gmail-for-email-activities

Download the solution

Click on the button below to download the source of the demo (Download Email Attachment).

About Valeriu B

RPA Developer (UiPath certified)

Leave a Reply

Your email address will not be published. Required fields are marked *