ComponentPro UltimateMail

      Downloading Message Headers Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      You can retrieve all message headers by simply calling the ListMessages with parameter ImapEnvelopeParts.FullHeaders. However, this might take a very long time if the mailbox contains a lot of messages or the bandwidth is limited. We will take advantage of the DownloadMessageHeaders method by the following example:

      Getting Mailbox Info

      1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
        using System;
        using System.IO;
        using ComponentPro.Net;
        using ComponentPro.Net.Mail;
        
      2. Create a new instance of the Imap class.
      3. Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "password";
        const int port = 993;
        const string folder = "Inbox";
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        Imap client = new Imap();
        try
        {
            Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
            // Connect to the server. 
            client.Connect(serverName, port, securityMode);
        
      4. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
        // Login to the server.
        Console.WriteLine("Logging in as {0}...", user);
        client.Authenticate(user, password);
        
      5. Now download message headers with both ListMessages and DownloadMessageHeaders method. The code looks similar to the following:
        // Select working folder.
        Console.WriteLine("Selecting folder '{0}'...", folder);
        client.Select(folder);
        Folder workingFolder = client.WorkingFolder;
        
        // Show the number of messages in the selected folder.
        Console.WriteLine("{0} messages found.", workingFolder.TotalMessages);
        
        
        // Get the message list.
        Console.WriteLine("Getting message list...");
        ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.FullHeaders);
        
        ImapMessage message;
        for (int i = 0; i < list.Count; i++)
        {
            message = list[i];
        
            // Print out message uniqueid and sequence id     
            Console.WriteLine("UniqueId: {0}, Sequence Num: {1}", message.UniqueId, message.MessageInboxIndex);
            Console.WriteLine("From: {0}, To: {1}, Subject: '{2}'", message.From, message.To, message.Subject);
            Console.WriteLine();
        }
        
        
        
        // Get the message list.
        Console.WriteLine("Getting message list...");
        list = client.ListMessages(ImapEnvelopeParts.MessageInboxIndex);
        
        for (int i = 0; i < list.Count; i++)
        {
            message = list[i];
        
            // Download message headers to a stream. 
            Stream s = new MemoryStream();
            client.DownloadMessageHeaders(message.MessageInboxIndex, s);
        
            // Load message from the Stream object. 
            MailMessage msg = new MailMessage(s);
        
            // Print out message uniqueid and sequence id     
            Console.WriteLine("Sequence Num: {0}", message.MessageInboxIndex);
            Console.WriteLine("From: {0}, To: {1}, Subject: '{2}'", msg.From, msg.To, msg.Subject);
            Console.WriteLine();
        }
        
        
      6. After completing your work, call the Disconnect method to close the IMAP session.

      Final example code

      // Example Title : Download headers of messages 
      // Example Description : Shows how to connect to an IMAP server and download headers of messages. 
      // Example Group : Imap Message 
      using System;
      using System.IO;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      namespace Samples.ImapExample
      {
          class DownloadMessageHeaders
          {
              [STAThread]
              static void Main()
              {
                  const string serverName = "myserver";
                  const string user = "name@domain.com";
                  const string password = "password";
                  const int port = 993;
                  const string folder = "Inbox";
                  const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
                  Imap client = new Imap();
                  try 
                  {
                      Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
                      // Connect to the server. 
                      client.Connect(serverName, port, securityMode);
      
                      // Login to the server. 
                      Console.WriteLine("Logging in as {0}...", user);
                      client.Authenticate(user, password);
      
                      // Select working folder. 
                      Console.WriteLine("Selecting folder '{0}'...", folder);
                      client.Select(folder);
                      Folder workingFolder = client.WorkingFolder;
      
                      // Show the number of messages in the selected folder. 
                      Console.WriteLine("{0} messages found.", workingFolder.TotalMessages);
      
      
                      // Get the message list. 
                      Console.WriteLine("Getting message list...");
                      ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.FullHeaders);
      
                      ImapMessage message;
                      for (int i = 0; i < list.Count; i++)
                      {
                          message = list[i];
      
                          // Print out message uniqueid and sequence id     
                          Console.WriteLine("UniqueId: {0}, Sequence Num: {1}", message.UniqueId, message.MessageInboxIndex);
                          Console.WriteLine("From: {0}, To: {1}, Subject: '{2}'", message.From, message.To, message.Subject);
                          Console.WriteLine();
                      }
      
      
      
                      // Get the message list. 
                      Console.WriteLine("Getting message list...");
                      list = client.ListMessages(ImapEnvelopeParts.MessageInboxIndex);
      
                      for (int i = 0; i < list.Count; i++)
                      {
                          message = list[i];
      
                          // Download message headers to a stream. 
                          Stream s = new MemoryStream();
                          client.DownloadMessageHeaders(message.MessageInboxIndex, s);
      
                          // Load message from the Stream object. 
                          MailMessage msg = new MailMessage(s);
      
                          // Print out message uniqueid and sequence id     
                          Console.WriteLine("Sequence Num: {0}", message.MessageInboxIndex);
                          Console.WriteLine("From: {0}, To: {1}, Subject: '{2}'", msg.From, msg.To, msg.Subject);
                          Console.WriteLine();
                      }
      
      
                      // Disconnect. 
                      Console.WriteLine("Disconnecting...");
                      client.Disconnect();
      
                  }
                  catch (ImapException imapExc)
                  {
                      Console.WriteLine(string.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status));
                  }
                  catch (Exception exc)
                  {
                      Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
                  }
              }
          }
      }