MailBee. NET Objects Tutorials

Receiving and displaying HTML e-mail

You can tell MailBee to automatically process the message body upon parsing. For instance, to automatically create an HTML-formatted body from the plain text body when a message does not have HTML body (useful if you want to load it into an HTML container):

C#

msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml;

VB.NET

msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml

Also, you can specify the options which affect how the message is parsed. For instance, the code below makes MailBee convert all URLs contained in the plain text body of the message into A HREF links when building the HTML body from the plain-text body:

C#

msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink;

VB.NET

msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink

It's recommended to set these properties before the message gets parsed. MailBee parses the message whenever you access any of its properties which require parsing (lazy model).

However, in case if you already accessed any property of the message and then changed the parser settings, you need to re-parse the message. For that, call Apply method to apply the changes:

C#

msg.Parser.Apply();

VB.NET

msg.Parser.Apply()

To display the HTML body of the message, you can use a lot of tools and controls, such as WebBrowser control (in desktop apps):

C#

webBrowser1.DocumentText = msg.BodyHtmlText;

VB.NET

webBrowser1.DocumentText = msg.BodyHtmlText

Sample Code:

Summary: the following example downloads the last message from the specified mailbox, generates HTML message body from the plain-text body (assuming the message does not have its own HTML body), and displays the generated HTML body. Otherwise, the message's own HTML body is displayed.

By the way, you can also set PlainToHtmlMode = PlainToHtmlAutoConvert.IfPlain to make MailBee ignore the message's own HTML body and overwrite it with the auto-generated one. This can be useful if you don't want to display the message's native HTML body (for instance, for security reasons).

Before using MailBee.NET Objects library, please make sure it is unlocked (see "Sales, Licensing, and Support" and "Using MailBee.NET Objects in Your Projects" sections).

Create a WinForms or WPF app. Add a reference to MailBee.NET and add usings/imports:

C#

using System;
using MailBee;
using MailBee.Pop3Mail;

VB.NET

Imports System
Imports MailBee
Imports MailBee.Pop3Mail

Create a new form in and place a Button and WebBrowser control on it. Set the following code to be run on the button click event:

C#

Pop3 pop = new Pop3();
                        
pop.Connect("mail.domain.com");
pop.Login("login", "password");

MailMessage msg = pop.DownloadEntireMessage(pop.InboxMessageCount);

msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml;
msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink;

WebBrowser1.Text = msg.BodyHtmlText;

pop.Disconnect();



VB.NET

Dim pop As Pop3 =  New Pop3() 
 
pop.Connect("mail.domain.com")
pop.Login("login", "password")
 
Dim msg As MailMessage = pop.DownloadEntireMessage(pop.InboxMessageCount) 
 
msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml
msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink
 
WebBrowser1.Text = msg.BodyHtmlText

pop.Disconnect()

Should you experience any problems with the sample, check Troubleshooting page.