HELP!- Email Attachments counting and file type

Dear community. I have hit a brick wall. I am trying to find away of counting the number of attachments to an email with a mixture of .pdf, word and excel files attached to an email. I want to report the number back into excel.

Does any one know where, I can start or a link to resources to show me how etc… Which library, is there a tutorial, can any one help!

Best regards,

@bally
To count the number of email attachments and get their file types in UiPath, you can use the following steps:

  1. Use the “Get IMAP Mail Messages” activity to retrieve the emails from your email account. Specify the folder from which you want to retrieve the emails and any filter criteria such as date range or subject line.

  2. Use a “For Each” loop to iterate through the list of emails retrieved in the previous step.

  3. Within the loop, use the “Save Attachments” activity to save the attachments from each email to a specified folder on your computer. Use the “Attachments” property of the email message to retrieve the list of attachments.

  4. Use the “Directory.GetFiles” method to get a list of files in the folder where the attachments were saved.

  5. Use another “For Each” loop to iterate through the list of files and count the number of attachments.

  6. Use the “Path.GetExtension” method to get the file extension of each file and check if it matches the desired file type(s). You can store the file type(s) in an array or list variable and compare it against the file extension using the “String.Equals” method.

  7. If the file type matches the desired type, increment a counter variable to keep track of the number of attachments of that file type.

  8. Once the loops have finished, you can use the counter variable to display the total number of attachments of a specific file type.

Here’s an example of the code you can use within the loops:

For Each mail In listMails
   ' Save attachments
   attachments = mail.Attachments
   For Each attachment In attachments
      attachment.Save(attachmentPath)
   Next
   
   ' Count attachments by file type
   files = Directory.GetFiles(attachmentPath)
   For Each file In files
      If String.Equals(Path.GetExtension(file), ".pdf") Then
         pdfCount = pdfCount + 1
      ElseIf String.Equals(Path.GetExtension(file), ".docx") Then
         docxCount = docxCount + 1
      ' Add other file types as needed
      End If
   Next
Next

' Display counts
messageBox("PDF count: " + pdfCount.ToString)
messageBox("DOCX count: " + docxCount.ToString)

Note: Replace the file types “.pdf” and “.docx” with the desired file type(s) you want to count.

WOW A Big THANKYOU @ [Manju_Reddy_Kanughula]

lets assume a mail - Variablename mail
get returned a dictionary with the count of the different file type extensions and its count

Assign Activity
dictReport =

mail.Attachments.GroupBy(Function (x) Path.GetExtension(x.Name).toUpper.Replace(".","")).toDictionary(Function (x) x.Key, Function (x) x.Count)

So we can get it without saving the attachments and are dynamic

1 Like