Scraping the body of an email as text

Hey there,

I’m trying to extract the body text of an email but can’t seem to get it working. I have filtered the email I want as part of an ‘if’ activity based on sender/subject but cannot get the body of the email to appear.

Could someone please help by explaining which steps I’ll need to take? (Apologies, I’m new to this).

Thank you!

Hi @dr1992,

Could you please get a screenshot of the code that you’re using, and the bit that isn’t working as expected?

Hey @william.coulson, I won’t add a screenshot as it contains some sensitive info.

I have a ‘get outlook mail’ activity with a variable of UnreadEmails. Then a ffor each loop which gets each item in the inread emails with the criteria of:

item.Subject.ToString.ToLower.Contains(“EMAIL SUBJECT”) and item.Sender.ToString.Equals(“EXAMPLE@EMAIL.co.uk”)

I then have a message box for item.Body.ToString. When I run the bot, it marks my email as ‘read’ as inteded but then doesn’t show the message box.

Hi @dr1992,

Something like this?

If the message box isn’t going off, I would try running the code in debug and see the values of the various email parts you are referring to (subject, sender, body). You can specify these in a log message before the If statement or see them in the locals panel

The code you have written is in correct
item.Subject.ToString.ToLower.Contains(“EMAIL SUBJECT”)

You are converting the text to ‘ToLower’ and then comparing it with Capital characters.

Either use toupper or use non caps email subject

That was just an example to preserve sensitive info, the letters are lowered.

Is the type argument set to System.Net.Mail.MailMessage?!
IMG_20211101_204309|690x310

It is indeed.

Hmm so I debugged it and every field on that part is ‘The following errors were encountered while processing the workflow tree:‘VisualBasicValue’: Compiler error(s) encountered processing expression “item.To”.Option Strict On disallows late binding.’

Any ideas?

Hi @dr1992,

You’ll need to write item.ToString before you do any manipulation like .ToLower

So I changed it to this :

item.ToString.Sender.Equals(“example@email.com”) and item.ToString.Subject.ToLower.Contains(“test”)

Now I’m getting an error that ToString doesn’t belong with Sender or Subject lol.

Apologies, should’ve explained better. Do item.Sender.ToString.Equals and item.Subject.ToString.ToLower.Contains instead of item.ToString.Sender.Equals and item.ToString.Subject

Hmm, so it looks like the email is showing, though still no message box popping up.

Message box contains item.body.ToString.

Edit:
It looks like it can’t find the sender, which would be a condition for it.

Error: The following errors were encountered while processing the workflow tree:
‘VisualBasicValue’: Compiler error(s) encountered processing expression “item.Sender”.
Option Strict On disallows late binding.

Item.sender.address.tostring.equals( )

This should work. Since address has other sub function as well.

I have found some old code where I used Mail.Headers(“HTMLBody”) to get the body in a HTML format. I will look into using this same thing to get the body in a normal format, this may work for you

Edit: In that case of your edit, try item.From.ToString.ToLower.Equals to get the sender

Still getting the below (I think UiPath doesn’t like me haha)

The following errors were encountered while processing the workflow tree:
‘VisualBasicValue’: Compiler error(s) encountered processing expression “item.From”.
Option Strict On disallows late binding.

Please try this

Didn’t work, but thanks.

Can you check that your for each TypeArgument is “System.Net.Mail.MailMessage”?
image

I ran and logged some values from a test email I sent myself, not sure why it’s not working for you aside from the TypeArgument being incorrect…
image

Edit: Maybe try assigning Item.From.ToString to a variable before your If statement, and referring to the variable? Doubtful it’ll make a change but it’s worth a try

So I commented out the if activity and replicated your log messages and it got the information. The variable types were indeed set to System.Net.Mail.MailMessage as required.

Therefore, something is wrong with this ‘if’ condition, as it doesn’t register it as meeting the criteria.

item.From.ToString.ToLower.Equals(“example@email.com”) and item.Subject.ToString.ToLower.Contains(“test”)