How to fetch the email body of a mail message that contains text/html or text/plain content?
While fetching emails using the Get IMAP Mail Message activity, by default, it can only read mails that contain only one text mime-part (text/html or text/plain). When received emails contain more than one text/... mime-part, it might return a null string.
Solution\Workaround:
- Use a Get IMAP Mail Message activity that gives an output of List of MailMessage.
- Use a For each loop with the type argument System.Net.Mail.MailMessage
- Assign string with one of the following,
-
mail_body =
System.Text.RegularExpressions.Regex.Replace(item.Headers(“HTMLBody”),"(<.*?>)|({.*})|(.*[}\)--{;]\s?\r\n)|(\r?\n@.*)|(.*[}{;]$)",String.Empty).Trim
-
mail_body =
System.Text.RegularExpressions.Regex.Replace(item.Headers(“HTMLBody”),"(<.*?>)|({.*})|(.*[}\)--{;]\s?\r\n)|(\r?\n@.*)|(.*[}{;]$)",String.Empty)
-
- In the regex above, ‘item’ is each MailMessage item in the List of MailMessage
- 'mail_body' holds the captured plain text from the HTML mail