MailBee. NET Objects Tutorials

Receiving a simple e-mail

Introduction

POP3 protocol is considered outdated nowadays so it's recommended to use IMAP whenever possible. You should resort to POP3 only if IMAP is not supported by your mail server.

To receive a simple e-mail via POP3, use Pop3 class. Provided that the license key is already set (such as with MailBee.Global.LicenseKey property), create a new instance of this class:

C#

Pop3 pop = new Pop3();

VB.NET

Dim pop As Pop3 =  New Pop3()

Essential Properties and Methods

Now you can connect to the server by its host name or IP address and, optionally, port (usually 110 for regular POP3 port and 995 for dedicated SSL POP3 port):

C#

pop.Connect("mail.domain.com", 995);

VB.NET

pop.Connect("mail.domain.com", 995)

or

C#

pop.Connect("127.0.0.1", 110);

VB.NET

pop.Connect("127.0.0.1", 110)

By default, MailBee automatically starts SSL session (using TLS 1.2 protocol if possible) if port number is 995.

Note that all MailBee methods which perform network operations (including Connect) have async/await version (e.g. ConnectAsync). You can use them in .NET 4.5+ if you're using async I/O in your application.

To log in a mail account with the login and password:

C#

pop.Login("login", "password");

VB.NET

pop.Login("login", "password")

Login is usually the e-mail address of the account but can be different sometimes. With some mail services, you may need to enable POP3 access from a new location in the account settings or the server will reject credentials even if they are correct. Also, some servers may not allow login/password authentication and require OAuth 2.0 instead (although it's usually more common for IMAP servers rather than POP3 ones).

Once you're logged in, you can immediately start downloading messages. Unlike IMAP, you don't select a folder in POP3 (because POP3 supports accessing Inbox only).

To download an e-mail with its body and attachments, use DownloadEntireMessage method. For instance, this downloads the last message in the inbox:

C#

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

VB.NET

Dim msg As MailMessage = pop.DownloadEntireMessage(pop.InboxMessageCount)

where:

* pop.InboxMessageCount property returns the total number of messages in the mailbox;

* msg is a MailMessage object, representing a single e-mail message.

NOTE! Message indices are 1-based, not 0-based. The first message in the mailbox has index 1, not 0.

If there are no messages in a mailbox, the pop.InboxMessageCount property contains 0 and calling the pop.DownloadEntireMessage(pop.InboxMessageCount) method will cause an exception.

Once the message has been received, you can get HTML or plain-text message body using BodyHtmlText or BodyPlainText properties.

Finally, call Disconnect method to shut down the connection:

C#

pop.Disconnect();

VB.NET

pop.Disconnect()

Calling Disconnect is especially important if you deleted some messages during the POP3 session. If Disconnect is not called and the connection was aborted by other means, the messages marked for deletion may not actually get deleted by the server.

Sample Code:

Summary: the following example downloads the last message in a mailbox and displays its body.

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

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

C#

using System;
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;

...

Pop3 pop = new Pop3();
try
{
    pop.Connect("mail.domain.com");
    pop.Login("login", "password");
    Console.WriteLine("Successfully logged in.");
}
catch (MailBeePop3LoginNegativeResponseException ex)
{
    Console.WriteLine("POP3 server replied with a negative response at login:" + ex.ToString());
}
if (pop.IsLoggedIn && pop.InboxMessageCount > 0)
{
    MailMessage msg = pop.DownloadEntireMessage(pop.InboxMessageCount);

    if (!string.IsNullOrEmpty(msg.BodyHtmlText))
    {
        Console.WriteLine(msg.BodyHtmlText);
    }
    else if (!string.IsNullOrEmpty(msg.BodyPlainText))
    {
        Console.WriteLine(msg.BodyPlainText);
    }
    else
    {
        Console.WriteLine("The message body is empty.");
    }
}
pop.Disconnect();

VB.NET

Imports System
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime

...

Dim pop As Pop3 = New Pop3()

Try
    pop.Connect("mail.domain.com")
    pop.Login("login", "password")
    Console.WriteLine("Successfully logged in.")
Catch ex As MailBeePop3LoginNegativeResponseException
    Console.WriteLine("POP3 server replied with a negative response at login:" & ex.ToString())
End Try

If pop.IsLoggedIn AndAlso pop.InboxMessageCount > 0 Then
    Dim msg As MailMessage = pop.DownloadEntireMessage(pop.InboxMessageCount)

    If Not String.IsNullOrEmpty(msg.BodyHtmlText) Then
        Console.WriteLine(msg.BodyHtmlText)
    ElseIf Not String.IsNullOrEmpty(msg.BodyPlainText) Then
        Console.WriteLine(msg.BodyPlainText)
    Else
        Console.WriteLine("The message body is empty.")
    End If
End If

pop.Disconnect()