How to filter messages in Get outlook mail messages (with variables)


I’ve read a similar topic here on forum but it didn’t provide some information.
In my case I need to filter all emails from mailbox that contains a variable (and it can have extra data around the variable). It can be on the attachments, subject, or mailbody…

How can I filter using a SQL query on this case?

Thank you,

can i have an example if possible
Cheers @iwishikn

Have you searched and tried some of the solutions presented here: Search results for 'get emails filter' - UiPath Community Forum

I think there are some examples of syntax needed for the Filter property.

You can also opt to use .Where() on the mailmessages variable which is pretty easy to look at all the different parts of each mail and only take in the ones that meet your criteria. However, this will not be as fast as using the Filter property since using .Where() you must read all the emails first, rather than just the filtered amount.

Yes. If you have an attachment you can have the name as “something 208937something something.pdf” and same case for body or subject. I cannot give a real test case.

Yes, I’ve tried solutions, but I can review it again. I was using the SQL query with textdescription and subject and It always return 0 mails. Can you explain the .Where()?

Thank you

If you take a single mailmessage type value, you can access all of its parts like subjects, attachments, body, etc.
For example, mails(0).Subject would look at subject of the first item in the mailmessages. Or you can run it through a loop like For each m in mails, then use m.Subject

So, to use .Where(), it will essentially query your mailmessages based on .net conditions.
For example,
mails.Where(Function(m) m.Subject.ToUpper.Contains("ABC") ).ToArray

So, that will give you a filtered mails where subject contains certain text.

To look at attachments, it might be more complex since it is an enumerable of attachments. Off the top of my head, I don’t have an example of this.


But for that I would have to get all the email first, and then filter correct? It’s seems not the best solution since we are talking about a shared mail account with thousand of emails. Wouldn’t it be better a query?

You are correct, which is why I said it would be slower.

It’s still useful to know though, so you can quickly manipulate the emails around as you process them. For example, maybe you need to read the emails for certain attachments, but process them by sender address — then, you could use .Where() to filter by each address as you process the email… rather than reading the emails again over and over.

If you can show how you were doing it that returned 0 mails, maybe we can identify a problem.

“@SQL=” + “”"" + “urn:schemas:httpmail:textdescription” + “”"" + " like ‘" + strMailFilter + "%’" @ClaytonM
Any way to solve this?

1 Like