ComponentPro UltimateMail

      Downloading RAW(.EML) message Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      You can either retrieve a message as an instance of the MailMessage class or download it as a raw EML message to disk or write it into a data stream with DownloadMessage method.

      The following steps will help you to do that:

      Downloading a Raw(.EML) Message

      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.Text;
        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:
        // POP3 server information. 
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "mytestpassword";
        const int port = 995;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        // Create a new instance of the Pop3 class.
        Pop3 client = new Pop3();
        
        // 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.
        client.Authenticate(user, password);
        
      5. Now list all message and download them to local disk with DownloadMessage. The code looks similar to the following:
        // Get the message list.
        Console.WriteLine("Getting message list...");
        Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.UniqueId | Pop3EnvelopeParts.MessageInboxIndex);
        
        // Get messages. 
        for (int i = 0; i < list.Count; i++)
        {
            Pop3Message message = list[i];
        
            // Get file name. 
            string filename = GetFilename(message.UniqueId) + ".eml";
        
            // Get new message only. 
            if (!System.IO.File.Exists(filename))
            {
                Console.WriteLine("Downloading message {0}...", message.MessageInboxIndex);
                client.DownloadMessage(message.MessageInboxIndex, filename);
            }
        }
        
      6. After completing your work, call the Disconnect method to close the POP3 session.

      Final example code

      using System;
      using System.Text;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      static void Main()
      {
          // POP3 server information. 
          const string serverName = "myserver";
          const string user = "name@domain.com";
          const string password = "mytestpassword";
          const int port = 995;
          const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
          // Create a new instance of the Pop3 class. 
          Pop3 client = new Pop3();
      
          // Connect to the server. 
          client.Connect(serverName, port, securityMode);
      
          // Login to the server. 
          client.Authenticate(user, password);
      
          // Get the message list. 
          Console.WriteLine("Getting message list...");
          Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.UniqueId | Pop3EnvelopeParts.MessageInboxIndex);
      
          // Get messages. 
          for (int i = 0; i < list.Count; i++)
          {
              Pop3Message message = list[i];
      
              // Get file name. 
              string filename = GetFilename(message.UniqueId) + ".eml";
      
              // Get new message only. 
              if (!System.IO.File.Exists(filename))
              {
                  Console.WriteLine("Downloading message {0}...", message.MessageInboxIndex);
                  client.DownloadMessage(message.MessageInboxIndex, filename);
              }
          }
      
          // Close the connection. 
          client.Disconnect();
      }
      
      /// <summary> 
      /// Returns a uniquely correct file name from the specified unique message ID. 
      /// </summary> 
      /// <param name="uniqueId">The unique id.</param> 
      /// <returns>The corrected file name.</returns> 
      private static string GetFilename(string uniqueId)
      {
          // Characters allowed in the filename 
          const string allowed = " .-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
          // Replace invalid charactes with its hex representation 
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < uniqueId.Length; i++)
          {
              if (allowed.IndexOf(uniqueId[i]) < 0)
                  sb.AppendFormat("_{0:X2}", (int)uniqueId[i]);
              else 
                  sb.Append(uniqueId[i]);
          }
          return sb.ToString();
      }