MailBee. NET Objects Tutorials

Receiving and displaying HTML e-mail

After downloading the entire message from the POP3 server as shown in the paragraph 2.1 of this tutorial, the developer can manage the available message bodies. For instance, the developer can configure message parser to automatically create an HTML-formatted body from a plain text body when a message does not have HTML-formatted body:

C#

msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml;

VB.NET

msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml

Besides, the developer can specify the options which affect how the message is parsed. For instance, the code below specifies that all URIs contained in a plain text body of a message should be changed to the corresponding links, when the plain text body is automatically converted into the HTML-formatted body:

C#

msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink;

VB.NET

msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink

Since the message parser is configured, the developer should call the Apply method to apply the changes:

C#

msg.Parser.Apply();

VB.NET

msg.Parser.Apply()

The content of the HTML body of the parsed message can be easily displayed using Web Browser control as follows:

C#

webBrowser1.DocumentText = msg.BodyHtmlText;

VB.NET

webBrowser1.DocumentText = msg.BodyHtmlText

Please note, WebBrowser control is available in Visual Studio 2005 and higher only. Thus, the code above will not work roperly in earlier versions of Visual Studio.NET.

Should you experience any problems with the following sample, please see the corresponding topic of the Troubleshooting section.

Sample Code:

Summary: the following example downloads the last message from the specified mailbox, generates HTML mes-sage body from plain text body, and displays HTML body of this message.

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

Please add the reference to MailBee.NET.dll to your application and then add the following lines in the top of your code to allow the use of MailBee.NET Objects in your application:

C#

using System;
using MailBee;
using MailBee.Pop3Mail;

VB.NET

Imports System
Imports MailBee
Imports MailBee.Pop3Mail

Then create a new form in Visual Studio 2005 and place the Button and WebBrowser control on this form. Set the following code to be run on the button click event:

C#

Pop3 pop = new Pop3();
                        
try
{
        pop.Connect("mail.domain.com");
        pop.Login("login", "password");
        Console.WriteLine("Successfully logged in.");
}

catch(MailBeePop3LoginNegativeResponseException e)
{
        Console.WriteLine("POP3 server replied with a negative response at login.");
}

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

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

msg.Parser.Apply();
                        
WebBrowser1.Text = msg.BodyHtmlText;

try
{
        pop.Disconnect();
        Console.WriteLine("Disconnected successfully.");
}
catch
{
        Console.WriteLine("Disconnection failed.");
}

VB.NET

Dim pop As Pop3 =  New Pop3() 
 
Try
        pop.Connect("mail.domain.com")
        pop.Login("login", "password")
        Console.WriteLine("Successfully logged in.")
Catch e As MailBeePop3LoginNegativeResponseException
        Console.WriteLine("POP3 server replied with a negative response at login.")
End Try
 
Dim msg As MailMessage =  pop.DownloadEntireMessage(pop.InboxMessageCount) 
 
msg.Parser.PlainToHtmlMode = PlainToHtmlAutoConvert.IfNoHtml
msg.Parser.PlainToHtmlOptions = PlainToHtmlConvertOptions.UriToLink
 
msg.Parser.Apply()
 
WebBrowser1.Text = msg.BodyHtmlText
 
Try
        pop.Disconnect()
        Console.WriteLine("Disconnected successfully.")
Catch
        Console.WriteLine("Disconnection failed.")
End Try