ComponentPro UltimateMail

      Downloading messages headers Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      You can either download message headers as a raw EML message to disk or write it into a data stream with DownloadMessageHeaders method and then construct a MailMessage from the downloaded data and get your desired information.

      The following steps will help you to do that:

      Downloading Message Headers

      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 Pop3 class.
      3. Now you can connect to the POP3 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 = 995;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        Pop3 client = new Pop3();
        try
        {
            Console.WriteLine("Connecting POP3 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 list all messages and download the message headers to local disk with DownloadMessageHeaders. The code looks similar to the following:
        // Get mailbox info.
        Pop3MailboxStat info = client.GetMailboxStat();
        
        // Show the number of messages in the selected folder.
        Console.WriteLine("{0} messages found.", info.MessageCount);
        
        
        // Get the message list.
        Console.WriteLine("Getting message list...");
        Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.FullHeaders);
        
        Pop3Message 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(Pop3EnvelopeParts.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("Inbox Index: {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 POP3 session.

      Final example code

      using System;
      using System.IO;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      const string serverName = "myserver";
      const string user = "name@domain.com";
      const string password = "password";
      const int port = 995;
      const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
      Pop3 client = new Pop3();
      try
      {
          Console.WriteLine("Connecting POP3 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);
      
          // Get mailbox info. 
          Pop3MailboxStat info = client.GetMailboxStat();
      
          // Show the number of messages in the selected folder. 
          Console.WriteLine("{0} messages found.", info.MessageCount);
      
      
          // Get the message list. 
          Console.WriteLine("Getting message list...");
          Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.FullHeaders);
      
          Pop3Message 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(Pop3EnvelopeParts.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("Inbox Index: {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 (Pop3Exception pop3Exc)
      {
          Console.WriteLine(string.Format("An POP3 error occurred: {0}, ErrorStatus: {1}", pop3Exc.Message, pop3Exc.Status));
      }
      catch (Exception exc)
      {
          Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
      }