How to browse and download the contents of a POP3 mailbox with Rebex Secure Mail for .Net
Introduction
Rebex Secure Mail for .Net provides a whole selection of ways that you can deal with email from within your program. Here we show how you can access a POP3 message box and retreive a list of the contents before downloading specific messages for viewing.
Detail
The C#.Net solution provided prompts for connection information relating to the POP3 server and mailbox to be accessed. It then displays a list of everything in the inbox and allows the user to view any message.
All of the UI controls are standard .Net controls and the work is done by the Rebex Secure Mail class libraries
Setup
Download the sample code using the above link and unzip it into a directory. Once the installation is complete you will find the C# sample code in the directory:
Rebex-SimplePOP3-C
and the VB.Net in the folder :
Rebex-SimplePOP3-VB
In the folder where you unpacked the ZIP file.
Configuration
The project makes 2 references Rebex.Mail and Rebex.Net.Pop3 and imports the namespaces Rebex.Mime, Rebex.Mail and Rebex.Net.
All the code for this solution is contained in class Form1. There are two main objects declared at class level _pop which is of type Rebex.Net.Pop3 and does most of the work, and _messages which is of type Pop3MessageCollection and stores the list of messages retreived.
These are instantiated in the constructor for Form1:
C#
private Pop3 _pop;
private Pop3MessageCollection _messages;
public Form1()
{
InitializeComponent();
this._pop = new Pop3();
}
VB
Private _pop As Pop3
Private _messages As Pop3MessageCollection
Public Sub New()
InitializeComponent()
Me._pop = New Pop3()
End Sub
Getting a message list
On clicking the "Get Mail" button, a connection is made to the server specified in txtServer on the standard POP3 port, 110 by the Connect() method of _pop3. A login is then initiated using the Login() method which passes username & password.
C#
private void btnGet_Click(object sender, EventArgs e)
{
try
{
this._pop.Connect(txtServer.Text, 110);
this._pop.Login(txtUsername.Text, txtPassword.Text);
.
.
VB
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
Try
Me._pop.Connect(Me.txtServer.Text, 110)
Me._pop.Login(Me.txtUsername.Text, Me.txtPassword.Text)
.
.
The messages can now be retreived from the mailbox using the GetMessageList() method of _pop3. This returns a MessageCollection object which is then iterated and a MessageInfo is retreived for each message via the GetMessageInfo() method of _pop3. Each MessageInfo object is then used to load the listView for display.
C#
this._messages = _pop.GetMessageList();
foreach (Pop3MessageInfo message in this._messages)
{
Pop3MessageInfo m = this._pop.GetMessageInfo(message.SequenceNumber, Pop3ListFields.FullHeaders);
if (m.HeadersParsed)
{
string subject = (m.Subject != null) ? m.Subject : "n/a";
string date = (m.Date != null) ? m.Date.LocalTime.ToString("yyyy-MM-dd HH:mm:ss") : "n/a";
ListViewItem item = new ListViewItem(m.From.ToString());
item.Tag = m;
item.SubItems.Add(subject);
item.SubItems.Add(m.Length.ToString());
item.SubItems.Add(date);
this.listView.Items.Add(item);
VB
Me._messages = _pop.GetMessageList()
For Each message As Pop3MessageInfo In Me._messages
Dim m As Pop3MessageInfo = Me._pop.GetMessageInfo(message.SequenceNumber, Pop3ListFields.FullHeaders)
If m.HeadersParsed Then
Dim subject As String = IIf((m.Subject IsNot Nothing), m.Subject, "n/a")
Dim [date] As String = IIf((m.[Date] IsNot Nothing), m.[Date].LocalTime.ToString("yyyy-MM-dd HH:mm:ss"), "n/a")
Dim item As New ListViewItem(m.From.ToString())
item.Tag = m
item.SubItems.Add(subject)
item.SubItems.Add(m.Length.ToString())
item.SubItems.Add([date])
Me.listView.Items.Add(item)
Note that each
Pop3MessageInfo object returned in the collection is stored in the
Tag property of the associated
ListViewItem. This is useful when selecting a message to view.
Viewing a message
A message is viewed by double clicking on a line in the listView. This causes the MessageInfo object to be retreived from the Tag property.
The SequenceNumber property of the message is then used by the GetMessage method of _pop3 to retreive the full message from the server into a MimeMessage object.
This is then cast as a MailMessage and the BodyText property is used to display the message body in the textbox txtMessage.
C#
Pop3MessageInfo m = (Pop3MessageInfo)selCol[0].Tag;
int sequenceNumber = this._messages.Find(m.UniqueId).SequenceNumber;
MemoryStream buffer = new MemoryStream();
this._pop.GetMessage(sequenceNumber, buffer);
buffer.Position = 0;
byte[] raw = buffer.ToArray();
MimeMessage mime = new MimeMessage();
mime.Load(buffer);
MailMessage message = (MailMessage)mime;
txtMessage.Text = message.BodyText.Replace("\n", "\r\n");
VB
Dim m As Pop3MessageInfo = DirectCast(selCol(0).Tag, Pop3MessageInfo)
Dim sequenceNumber As Integer = Me._messages.Find(m.UniqueId).SequenceNumber
Dim buffer As New MemoryStream()
Me._pop.GetMessage(sequenceNumber, buffer)
buffer.Position = 0
Dim raw As Byte() = buffer.ToArray()
Dim mime As New MimeMessage()
mime.Load(buffer)
Dim message As MailMessage = CType(mime, MailMessage)
Me.txtMessage.Text = message.BodyText.Replace("" & Chr(10) & "", "" & Chr(13) & "" & Chr(10) & "")
Conclusion
This simplistic solution demonstrates how Rebex Secure Mail for .Net can be used to retreive the contents of a mailbox from within a program. The capabilities of this component go much further and there are considerably more sophisticted samples available on the Rebex web site, just follow the link below.
Visit
Rebex
for more information and more samples.