Namespaces and Assemblies: Contact
Namespaces and Assemblies: Contact
Namespaces and Assemblies: Contact
aspx
Login
Contact
About Rebex
Contact
Our customers
News
Jobs
Table of content
Namespaces and assemblies
POP3 basics - connecting, logging in and disconnecting
Authenticating to a POP3 server
Authentication using NTLM
Obtaining info about the mailbox
Sequence numbers and unique IDs
Retrieving the message list
Downloading message headers
Downloading messages
Deleting and undeleting messages
Using events and logging communication
C#
using Rebex.Net;
using Rebex.Mail;
using Rebex.Mime.Headers;
VB.NET
Imports Rebex.Net
Imports Rebex.Mail
Imports Rebex.Mime.Headers
back to top...
POP3 is basically a single-session and single-folder protocol. While you are connected and authenticated, you are not
going to be informed when any new message arrives, and you only have access to messages that were available at the
time of authentication. Because of this, many POP3 servers only allow a single session for a single account at a time, so
don't forget to call Disconnect method when you are done with the mailbox, or you might experience problems
connecting again until the previous session either expires or the Ftp object is claimed by .NET's garbage collector. Also,
due to the nature of the POP3 protocol, you will need to connect periodically if you wish to check for new messages.
And now let's look at some sample code.
C#
using Rebex.Net;
...
// authenticate
client.Login("username", "password");
// disconnect
client.Disconnect();
VB.NET
Imports Rebex.Net
...
'authenticate
client.Login("username", "password")
'disconnect
client.Disconnect()
back to top...
GetSupportedAuthenticationMethods method, and specify which one is to be used for login. (Or specify
Pop3Authentication.Auto that will do this automatically.)
C#
using Rebex.Net;
...
VB.NET
Imports Rebex.Net
...
back to top...
C#
...
VB.NET
...
NTLM also makes it possible to authenticate as the user under whose context your application is running. This makes it
possible for the user to log in without the need to specify his or her password.
C#
...
VB.NET
...
back to top...
C#
client.Disconnect();
VB.NET
client.Disconnect()
back to top...
ID type Description
A number between 1..[number of messages in the POP3 mailbox]. This number does
not change during POP3 session - even when one or more messages are deleted, the
int
remaining messages retaing their sequence number. However, these numbers are
sequenceNumber
specific to a single session - when you disconnect and log in again later, the same
sequence numbers will be assigned to different messages.
string Unlike sequence number, the message unique ID is permanent and does not change
uniqueID between sessions. A message will retain its unique ID until it is deleted.
Most POP3 methods that deal with messages only accept sequenceNumber as a parameter for message
identification. However, it is quite simple to map a given unique ID to a corresponding sequence number and vice-versa.
Just retrieve the message list using use GetMessageList method, use its Find method to get info about the given
message and read its UniqueId or SequenceNumber properties to get the required ID.
C#
client.Disconnect();
VB.NET
client.Disconnect()
back to top...
These fields are bit flags, which means that combinations of SequenceNumber|Length and UniqueId|Length are
also possible - use bitwise or operator for this.
The following code shows how to download a message list and show a unique ID and From/Subject headers of each
message:
C#
client.Disconnect();
VB.NET
client.Disconnect()
back to top...
C#
client.Disconnect();
VB.NET
client.Disconnect()
back to top...
# Downloading messages
Finally, let's start downloading some messages! It is very simple - just call the GetMailMessage method and you
retrieve an instance of the MailMessage class that contains the message corresponding to the supplied sequence
number. You can read message headers, body and attachments from it, modify it, save it to disk or send it again using
our Smtp class. For more info about this, check out our MailMessage class tutorial.
Alternatively, if all you need is to download the message data and save it to disk or write it into a stream, and you don't
need to access the message content in any way at the moment, just call the GetMessage method instead. This won't
return an instance of MailMessage and just writes the message data into the supplied local path or stream.
C#
if (list.Count == 0)
{
Console.WriteLine("There are no messages in the mailbox.");
}
else
{
// download the first message
MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
//...
}
client.Disconnect();
VB.NET
If list.Count = 0 Then
Console.WriteLine("There are no messages in the mailbox.")
Else
'download the first message
Dim message As MailMessage = client.GetMailMessage(list(0).SequenceNumber)
'...
End If
client.Disconnect()
back to top...
Calling Undelete method. This recovers messages marked as deleted. It will appear in message lists again and
won't be removed when the session ends.
Calling Disconnect(true). This will undelete all deleted messages and disconnect from the server. No deleted
messages will be removed, and the next time you connect to this mailbox, they will be there again. Calling
Disconnect() without parameter will commit all changes and remove all messages marked as deleted.
C#
client.Connect("pop3.example.org");
client.Login("username", "password");
int sequenceNumber = 1;
VB.NET
back to top...
Occurs when the session state changes, such as from Disconnected to Ready, from
StateChanged
Ready to Sending or from Sending to Reading.
TransferProgress Occurs when a block of message data is received from the server.
The CommandSent or ResponseRead events are particularly useful for the purpose of generating a communication log
and the TransferProgress event can be used by a GUI application to display amount of transfered data or a progress
bar.
C#
...
VB.NET
...
back to top...