HTML and text content within an email Most email sent today is HTML-based, however an email may also contain a plain text alternative for older mail clients. Learn how to work with text content of an email.
Before you begin
The examples shown below are based on two key assumptions:
Mailosaur also automatically extracts certain content for you, such as links and images.
Extracting from the HTML
You may wish to work with individual elements within your email’s HTML content. This is useful for extracting key pieces of data, such as account verification codes.
Install an HTML library
To extract content from the body of an email, you will need to use an HTML library in your tests. Each language has alternative libraries to those shown here, but this should get you started.
First, install a library for the language you are working with:
var doc = new HtmlDocument();
doc.LoadHtml(message.Html.Body);
// Find element using xpathvar node = doc.DocumentNode.SelectSingleNode("//*[contains(@class, 'verify-code')]")
var verificationCode = node.InnerText;
Console.WriteLine(verificationCode); // "542163"