MailBee. NET Objects Tutorials

Receiving a simple e-mail

Introduction

To receive a simple e-mail, the developer should use POP3 object. A new instance of this object can be created as follows:

C#

Pop3 pop = new Pop3();

VB.NET

Dim pop As Pop3 =  New Pop3()

Essential Properties and Methods

To receive e-mail message MailBee.NET Objects communicates with POP3 server. In fact, it does not matter where POP3 server is actually running: on the current PC, somewhere in local network, or in Internet. To connect to POP3 server the developer should just specify the host name of this POP3 server (or the equal IP address) as follows:

C#

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

VB.NET

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

or

C#

pop.Connect("127.0.0.1");

VB.NET

pop.Connect("127.0.0.1")

Since all POP3 servers always require authentication, the developer should specify the login and password to log in the mailbox as shown below:

C#

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

VB.NET

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

When the developer has successfully logged in the mailbox, any message this mailbox contains can be easily downloaded by calling the DownloadEntireMessage method of POP3 object. The developer should specify the index of a message when calling this method. For instance, the following line of code downloads the last message from the inbox:

C#

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

VB.NET

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

where:

* pop.InboxMessageCount is a property, containing the total number of messages stored in the mailbox;

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

NOTE! 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 error.

If the new messages should be downloaded from the mailbox, the developer has to define the special helper function. This function should look up the UID of the message in the existing database of already downloaded messages. If the specified UID was not found, the message is considered as a new one. Thus, the developer has to use the database engine to store UIDs of all received messages.

When a message was received, the developer can display the message body using BodyHtmlText or BodyPlainText property of MailMessage object for HTML-formatted or plain text body respectively.

Finally, when all necessary operations with mailboxes are finished, and the connection with POP3 server is useless, the developer should call the Disconnect method of POP3 object to disconnect from the POP3 server as follows:

C#

pop.Disconnect();

VB.NET

pop.Disconnect()

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 newest message from the specified mailbox and displays a 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).

C#

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

namespace EmailApp
{
    class Class1
    {
        [STAThread]

        static bool IsNewMessage(string UID)
        {
            return true;
        }
        static void Main(string[] args)
        {
            Pop3 pop = new Pop3();

            try
            {
                pop.Connect("mail.domain.com");
                pop.Login("login", "password");
                Console.WriteLine("Successfully logged in.");
            }
            catch(MailBeePop3LoginNegativeResponseException)
            {
                Console.WriteLine("POP3 server replied with a negative response at login.");
            }

            string[] arrIDs = pop.GetMessageUids();
            int n = pop.InboxMessageCount;
            if (IsNewMessage(arrIDs[n])) 
            {
                MailMessage msg = pop.DownloadEntireMessage(n);

                if (msg.BodyHtmlText != "") 
                     Console.WriteLine(msg.BodyHtmlText);
                else 
                    if (msg.BodyPlainText != "") 
                      Console.WriteLine(msg.BodyPlainText);
                    else 
                      Console.WriteLine("The body of this message is empty.");
            }

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

            catch
            {
                Console.WriteLine("Disconnection failed.");
            }
        }
    }
}

VB.NET

Imports System
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime
 
Namespace EmailApp
    Class Class1
        <STAThread> _ 
 
        Shared Function IsNewMessage(ByVal UID As String) As Boolean
            Return True
        End Function
        Shared  Sub Main(ByVal args() As String)
            Dim pop As Pop3 =  New Pop3() 
 
            Try
                pop.Connect("mail.domain.com")
                pop.Login("login", "password")
                Console.WriteLine("Successfully logged in.")
            Catch
                Console.WriteLine("POP3 server replied with a negative response at login.")
            End Try
 
            Dim arrIDs() As String =  pop.GetMessageUids() 
            Dim n As Integer =  pop.InboxMessageCount 
            If IsNewMessage(arrIDs(n)) Then
                Dim msg As MailMessage =  pop.DownloadEntireMessage(n) 
 
                If msg.BodyHtmlText <> "" Then
                     Console.WriteLine(msg.BodyHtmlText)
                Else 
                  If msg.BodyPlainText <> "" Then
                    Console.WriteLine(msg.BodyPlainText)
                  Else 
                    Console.WriteLine("The body of this message is empty.")
                  End If
                End If
            End If
 
            Try
                pop.Disconnect()
                Console.WriteLine("Disconnected successfully.")
            Catch
                Console.WriteLine("Disconnection failed.")
            End Try
        End Sub
    End Class
End Namespace