Mail.headers in if condition stop the robot execution

Hello all.
I need your help executing the below code.

It seems that i’m facing a problem with mail.Headers with plainText.
I want to check if specific keyword appear inside my mail text in body.

That’s the if statement i want to check

mail.Headers(“PlainText”).ToString.Contains(“mySting”)

uiPath_Exception_v2

In output console said : If: Object reference not set to an instance of an object.

Can anyone help me solve this? Is there any other way to find specific string in mail body?

Best Regards

@f2007pilot,

Warm welcome to the uipath community, instead of mail.headers you should be using mail.body

Here your requirement is to check for a keyword inside the mail body not the email subject or header.

mail.Body.Contains(“YOURSTRING”)

Hello mzahid,
Thank you for your answer.Replacing mail.Headers with mail.Body returns null(in test with message box).That’s the reason i’m using mail.Headers.
I print in message box mail.Headers(“PlainText”), it is correct and doing what exactly i want but it stuck at if statement with the exception i mentioned above.

@f2007pilot You would need to include the Body after ForEach into a try catch activity
and use a log message for any exceptions occurred.

Instead of message box use Log message activity. Let me know if this solution works

Same Result.Does not working either.

What is the assign activity inside the If loop doing ?
Are you counting the number of emails which has YOUR STRING in the body ?

i’m holding an int variable to count if any of mail messages found in the above if statement.If mail found with my string i assign this in as :

noOfMails =noOfMails+1

And then outside the loop i’ve got another if that is checking if noOfMails is 0 message box no mails found,else if noOfMails >0 message box the noOfMails found in your mail box

@f2007pilot

Solution is below,

Please note the variable type of MailMessages is List

and the TypeArgument in forEach should be System.Net.Mail.MailMessage

Τhank you for your replies.But as i mentioned 2 times the problem is with mail.Headers and not with mail.Body.I tried both with your solution and robot finished (but it’s not finding the string inside the mail) using the mail.Body and with mail.Headers (finds the specific string) but it’s a problem with object reference in if condition.The problem is that the only way to achieve the requested string to be found inside the body is to use mail.Headers.If you try your solution with mail.Headers you will get an error.
mail.Body returns null due to almost all mails are using html format encoding and thats why mail.Headers(“PlainText”) help us solve this problem, but its a strange exception occurred while processing inside if.

What is the output which you received after the robot finished?

Below is my output, which is working fine.

Can you send a screenshot of your error message ?

Hello,

I resolve it using a try catch block and add exception with log message activity.It throws 3 exceptions in log message and continues.It’s also find the correct email.
See the attach

.
And the execution:
solved_v2 .

Thanks for your help.
Tried your code in message box and you will see that returns null message box.It’s not reading html body messages as text.Probably your mail box not having html messages.

Can you send a screenshot of your for each loop , properties.

Include System.Net.Mail.MailMessage as the type argument in the For each loop (edit this is in the Properties)

Try the below solution

Already done that mate.
if you find the forum you will see that if you want to search for a specific string in mail body is better to use mail.Headers.

I’m on Yahoo client mail using imap.

Thank you again.

@f2007pilot

To fix your problem, Now I do have a HTML message in my inbox,

In order to read HTML body messages as text ,

First I converted the HTML body message into plain text and then checked the plain text for the required string in the if loop.

In my existing work flow , I have used two assign activities,

Where ,

  1. original_mailbody is of type string and
  2. also new_mailBody in assign activity is of type string and assigned the below,

Convert.ToString(Regex.Replace(original_mailbody, “<[^>]+>|&nbsp”, string.Empty)).Replace("&#8203",string.Empty).Replace(“v:* {behavior:url(#default#VML);}”,string.Empty).Replace(“o:* {behavior:url(#default#VML);}”,string.Empty).Replace(“w:* {behavior:url(#default#VML);}”,string.empty).Replace(".shape {behavior:url(#default#VML);}",string.Empty)

As you can see in the output panel this is working fine. :slight_smile:

Hello,

As i said problem solved in my previous post so i will test later on your method too.Thank you for your help.
Go on with your automations mate.

Regards

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