using Rebex.Net;

Welcome to using Rebex.Net; Sign in | Help
in Search

Using Rebex.Net

News and announcements about Rebex.NET components

HOWTO: Download emails from a GMail account in C#

Summary

The article shows how to connect to a Google Mail (aka GMail) mailbox, retrieve the message list and download the messages from your C# code using Rebex Secure Mail for .NET component.

Setting-up GMail

First, you have to enable POP3 access to your GMail mailbox:

  1. Go to Settings.
  2. Select "Forwarding and POP" tab.
  3. Enable POP for all mail or for new mail only (as seen on the following image).

Connect, login and get message count

GMail supports POP3 over TLS/SSL secured channel only. There are two variants of this - the Implicit and Explicit mode. The difference between these two modes is out of the scope of this article and is discussed in the connecting to the mail server using SSL tutorial. For now, we only have to keep in mind that GMail uses Implicit SSL mode.

Now, let's write some code:

using Rebex.Mail;
using Rebex.Net;
...
// -----------------
// create the POP3 client
// -----------------
Pop3 client = new Pop3();
try
{
   // -----------------
   // connect and login
   // -----------------
   
   // Connect securely using explicit SSL. 
   // Use the third argument to specify additional SSL parameters. 
   Console.WriteLine("Connecting to the POP3 server...");
   client.Connect("pop.gmail.com", 995, null, Pop3Security.Implicit);
   
   // login and password
   client.Login(email, password);
   
   // get the number of messages
   Console.WriteLine("{0} messages found.", client.GetMessageCount());
   
   // -----------------
   // list messages
   // -----------------
   
   // list all messages
   ListPop3MessagesFast(client); // unique IDs and size only   
   //ListPop3MessagesFullHeaders(client); // full headers
}
finally
{
   // leave the server alone
   client.Disconnect();      
}

Downloading the message list

Once we are connected, we can download the messages or their headers. The POP3 protocol only enables us to download the message list with sequence numbers, unique IDs and messages sizes. When we want to get more interesting info such as 'who is the message sender', or 'what about message subject' we are forced to download the message. Well, not the whole message really, but at least the message beginning with all the message headers. As you can imagine, retrieving message headers from the mailbox full of messages over a slow network connection can take a lot of time. Let's implement two variants of the code - one is fast, the other is verbose:

public static void ListPop3MessagesFast(Pop3 client)
{
   Console.WriteLine("Fetching message list...");
   
   // let's download only what we can get fast
   Pop3MessageCollection messages = 
      client.GetMessageList(Pop3ListFields.Fast);

   // display basic info about each message
   Console.WriteLine("UID | Sequence number | Length");
   foreach (Pop3MessageInfo message in messages)
   {
      Console.WriteLine
      (
         "{0} | {1} | {2} ",
         message.UniqueId,
         message.SequenceNumber,
         message.Length
      );
   }   
}

public static void ListPop3MessagesFullHeaders(Pop3 client)
{
   Console.WriteLine("Fetching message headers...");

   // Get the message list - full headers.
   // It could be quite slow on large mailboxes,
   // as full messages headers must be downloaded
   // for each message.
   Pop3MessageCollection messages = 
      client.GetMessageList(Pop3ListFields.FullHeaders);

   // display info about each message 
   Console.WriteLine("UID | From | To | Subject");
   foreach (Pop3MessageInfo message in messages)
   {
      Console.WriteLine
      (
         "{0} | {1} | {2} | {3}",
         message.UniqueId,
         message.From,
         message.To,
         message.Subject
      );
      
      // you can also download the full message with following code:
      // MailMessage downloadedMessage = client.GetMailMessage(message.SequenceNumber);
   }   
}

More info and links


Published Monday, May 14, 2007 12:00 PM by Martin Vobr
Filed under: , , ,

Comments

 

Weblog @ Rebex.cz :: Martin Vobr said:

Za těch zhruba dvacet let co programuju jsem si osedlal už ledacos - rozkoukával jsem se někdy v pravěku

November 27, 2007 6:19 PM
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems