How to save attachment from the list of downloaded emails in the folder

Hello all,

I have a situation where I download a lot of emails in the input folder.
Once downloaded, I am trying to save attachment for each downloaded email.

Does anyone have idea how could I achieve that?

Thank you in advance.

Hi @bp777,

Follow these steps:

  1. Create a New Sequence in UiPath.
  2. Get Email Files from Folder
  • Use the Assign activity to get all email files from the input folder.
    • emailFiles = Directory.GetFiles("C:\path\to\your\input\folder", "*.eml")
    • emailFiles is a variable of type String[].
  1. For Each Email File
  • Drag and drop the For Each activity.
  • Set the TypeArgument to String.
  • In the Values field, set it to emailFiles.
  1. Inside the For Each Loop
  • Load and Parse Email
    • Use the Invoke Code activity to load and parse the email using the MailMessage class.
Dim mail As New System.Net.Mail.MailMessage
Dim client As New System.Net.Mail.SmtpClient
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory
client.PickupDirectoryLocation = Path.GetDirectoryName(emailFile)
client.Send(mail)
mail = client.SendMailAsync(emailFile).Result
* `emailFile` is the current file in the loop.
  • Check for Attachments
    • Use an If activity to check if the email has attachments: mail.Attachments.Any.
  • Save Attachments
    • If true, drag and drop the Save Attachments activity inside the Then block of the If activity.
    • Set the properties:
      • MailMessage: Set to mail.
      • FolderPath: The folder path where you want to save the attachments, e.g., Path.Combine(Environment.CurrentDirectory, "Attachments").

LLM helped me to write this answer but it’s cross checked by me.

Thanks,
Ashok :slight_smile:

Hello,

I get error "error BC30451: ‘emailFile’ is not declared. It may be inaccessible due to its protection level.

@bp777,

You missed to pass the arguments to Invoke code.

Click on Edit Argument and pass like this.

emailFile is the current file in the loop

Thanks,
Ashok :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.