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 Imap class.
      3. Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
        // IMAP server information. 
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "mytestpassword";
        const int port = 993;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        // Create a new instance of the Imap class.
        Imap client = new Imap();
        
        // 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 select a working mailbox, list all message and download them to local disk with DownloadMessage. The code looks similar to the following:
        // Select 'INBOX' mailbox
        client.Select("INBOX");
        
        // Get the message list.
        Console.WriteLine("Getting message list...");
        ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.UniqueId);
        
        // Get messages. 
        for (int i = 0; i < list.Count; i++)
        {
            ImapMessage 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.UniqueId);
                client.DownloadMessage(message.UniqueId, filename);
            }
        }
        
      6. After completing your work, call the Disconnect method to close the IMAP session. 

      Final example code

      using System;
      using System.Text;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      static void Main()
      {
          // IMAP server information. 
          const string serverName = "myserver";
          const string user = "name@domain.com";
          const string password = "mytestpassword";
          const int port = 993;
          const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
          // Create a new instance of the Imap class. 
          Imap client = new Imap();
      
          // Connect to the server. 
          client.Connect(serverName, port, securityMode);
      
          // Login to the server. 
          client.Authenticate(user, password);
      
          // Select 'INBOX' mailbox 
          client.Select("INBOX");
      
          // Get the message list. 
          Console.WriteLine("Getting message list...");
          ImapMessageCollection list = client.ListMessages(ImapEnvelopeParts.UniqueId);
      
          // Get messages. 
          for (int i = 0; i < list.Count; i++)
          {
              ImapMessage 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.UniqueId);
                  client.DownloadMessage(message.UniqueId, 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();
      }