No mail message body in IMAP

Hello Expertise
I am not able to get the body message in IMAP as I have used System.Text.RegularExpressions.Regex.Replace(Mail.Body, “<[^>]>", “”).
My output gets blanks.
I have seen the solution as system.Text.RegularExpressions.Regex.Replace(mail.Headers(“HTMLBody”),"(<.
?>)|({.*})”,""), But this one get all the signature strings too, I would like to have just a body mail.
Your help in this matter will be great thankful.

Hi @Kuenzang

Could you give an example of the outputs? In case the email is HTML, you will need to use the second method with some additional post-processing.

Hi @Kuenzang,

Please try mail.Headers("PlainText") instead of mail.Body ,
or use the regex, System.Text.RegularExpressions.Regex.Replace(mail.Headers(“HTMLBody”),"(<.*?>)|({.*})|(.*[}\)--{;]\s?\r\n)|(\r?\n@.*)|(.*[}{;]$)",String.Empty).Trim

Warm regards,
Nimin

1 Like

Hi @nimin
I didn’t get the output,

Hi @loginerror
I just need the body message of the mail.

Hi @nimin
I tried with PlainText too but the output is same.

Hi @Kuenzang,

I think something wrong with with your variable assignment. Please try the following.
1.Use a get IMAP message activity; it’s result will be a list of mail messages.
2. Use a for each loop with type argument System.Net.Mail.MailMessage
3.In the above regex, ‘mail’ is each MailMessage item in the list of mailmessages.
4.Assign string, mail_body= System.Text.RegularExpressions.Regex.Replace(mail.Headers(“HTMLBody”),"(<.*?>)|({.*})|(.*[}\)--{;]\s?\r\n)|(\r?\n@.*)|(.*[}{;]$)",String.Empty).Trim
5. Now 'mail_body' holds the captured plain text from the HTML mail.:slightly_smiling_face:

Warm regards,
Nimin

1 Like

Hi sir @nimin
Can you help me to match and split this String.
Name:Darwish FrancoEmail Address:darwish.franco.ccc@gmail.com [darwish.franco.ccc@gmail.com]Company name:ShopeeSubscription:I want to create a client account and hire a robotComments (optional):TestSent from Caipable [https://caipable.com]
Where I need to match Company name: and get only “ShopeeSubscription”, not the other rests one.

Hi @Kuenzang,

Do you want to extract “Shopee” or “ShopeeSubscription”? Please update.

Hi @nimin
I want “ShopeeSubscription”

Hi @Kuenzang,

Please use the regex System.Text.RegularExpressions.Regex.Match(your_string,"(?<=Company name:)\w+").ToString to get the required string.

Warm regards,
Nimin

Hi @nimin
Thank you for your help, can you plesae explain me how its done?

Hi @nimin
Sir what if I want to extract “Shopee” only instead of “ShopeeSubscription”? Will the code be change in regex match?

Hi bro,

In the regex, (?<=Company name:)\w+ looks for any number of digits of alphabets after the word “Company name”. Here (?<=) is a look behind group and ‘/w+’ is one or more combinations of alphabets or digits. “:” is a special character, so it doesn’t comes under \w and only the string before ‘:’ will be capture.

If you only want “Shopee”, you can use regex.Match(you_string,"(?<=Company name:)[A-Z][a-z]+").ToString
It will looks for a word starting with uppercase followed by lowercase letters after the string “Company Name:”
I hope you got it now… :grinning:

Warm regards,
Nimin

1 Like

Hi @nimin
How can we get the mail message if the mail you recieve is in form format, will the same code be able to extract the mail body message with mail.Body?

Hi @Kuenzang,

If your mail is plain text, you can get the body with mail.Body. If it is in html format, you have to operate it with regex or using mail.Headers("PlainText") .

Warm regards,
Nimin

Hi @nimin
Even with using mail.Headers(“PlainText”) I still get some HTML format of header of mail and body message at last as an output.

Hi @Kuenzang,

Then it’s better to proceed with regular expression.:slightly_smiling_face:

Regards,
Nimin

Hi @nimin
Its still same result with regex. :frowning:

Hi @Kuenzang,

System.Text.RegularExpressions.Regex.Replace(mail.Headers(“HTMLBody”),"(<.*?>)|({.*})|(.*[}\)--{;]\s?\r\n)|(\r?\n@.*)|(.*[}{;]$)",String.Empty).Trim

This regex can remove all those unwanted strings shown in your screenshot.
Regards,
Nimin

3 Likes