Arrays of strings searching in emails

Hi,
I have several arrays of strings (~20). Each string should be checked if it is used in an email.
Some arrays are only for the subject, others for the email body and the rest for both.
Every array leads to another task (e.g. set category, forward mail or move to a folder).

How would a good solution look like?

I use the “Get Outlook Mail Messages” activity and check for every email like this:

arrKeywords_Test.FirstOrDefault(Function(s) strMailSubject.Contains(s))

Q1: how can I check this statement in an if-activity if it has any value?
Q2: would it be good to work with a switch-activity and handle every string separately?
Q3: if I would use the “for each email”-activity, would it be possible to solve this is one activity? I want to go through every email only once.

Thanks for your help!
Regards

Hey,

For each email would be the good approach for this task.
For each email
{
if(arrKeywords_Test.Any(Function(s) mail.item.subject.contains(s))
{
True — Add your logic
}
if you want to add more logics, add more conditions as per your requirement.
}

Hope this would help you.

Hi @TWG-NEF ,
1.The “get mail message” activity can help you get list mail
You can use “For each” mail in list mail to and add “If” activity to check subject
2.Swith I think ok
3.I think ok, but I usually use " for each"


regards,

Hi @TWG-NEF

If only one array will match, you can use Else Activity passing your expressions with Any Method:

arrKeywords_Test.Any(Function(s) strMailSubject.Contains(s))

Something like this

Thank you all for your advices.

I was on the right track, but the condition doesn’t work, because it returns a string and not a boolean. Should I set the option strict to off?
image

With “Else If” I need a condition?! How do I check if a value is returned?
image

Regards

Assign the result to a variable and then check its value:

Use Any method to return a Boolean like this

arrKeywords_Test.Any(Function(s) strMailSubject.Contains(s))

Hey,

if condition, use Any Function.
inside the if condition, use FirstOrDefault Function

Hi @TWG-NEF

Q1

If arrKeywords_Subject.Any(Function(s) email.Subject.Contains(s)) Then
’ Keyword found in subject
End If

If arrKeywords_Body.Any(Function(s) email.Body.Contains(s)) Then
’ Keyword found in body
End If

Q2

A Switch activity can be used if you have a clear value to switch on, but in this case, you’re checking multiple conditions, so a series of If or Else If statements would be more appropriate to handle different keywords and their corresponding actions.

Q3

For Each email In emails
’ Check subject keywords
subjectAction = arrKeywords_Subject.Select(Function(k) New With {k, .action = dictActions(k)}).FirstOrDefault(Function(x) email.Subject.Contains(x.k))

If subjectAction IsNot Nothing Then
    ' Execute the action for the subject keyword
    ExecuteAction(subjectAction.action, email)
End If

' Check body keywords
bodyAction = arrKeywords_Body.Select(Function(k) New With {k, .action = dictActions(k)}).FirstOrDefault(Function(x) email.Body.Contains(x.k))

If bodyAction IsNot Nothing Then
    ' Execute the action for the body keyword
    ExecuteAction(bodyAction.action, email)
End If

' Check keywords for both subject and body
' ... Similar logic ...

Next

Thank you all for your support!

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