ComponentPro UltimateMail

      Downloading Mail Message Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      Downloading mail messages from a IMAP server in UltimateMail component is so simple, you just call the DownloadMailMessage method and you retrieve an instance of the MailMessage class. You can now read, modify, save to disk the message. If you just need to download the message to disk or write it into a stream, just call the convenient DownloadMessage method instead.

      The example below shows you how to get a list of messages from a IMAP server and download and save all messages to disk:

      Downloading a mail 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 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 with DownloadMailMessage and display some information about these messages. 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 | ImapEnvelopeParts.Size);
        
        // Get messages. 
        for (int i = 0; i < list.Count; i++)
        {
            ImapMessage imapMessage = list[i];
        
            // Download the message to an instance of the MailMessage class. 
            MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
        
            // It can be downloaded with the sequence number. 
            // MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber); 
         
            // Display some information about it. 
            Console.WriteLine("Size: " + imapMessage.Size);
            Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
            Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
        }
        
      6. After completing your work, call the Disconnect method to close the IMAP session.

      Final example code

      using System;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      // 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 | ImapEnvelopeParts.Size);
      
      // Get messages. 
      for (int i = 0; i < list.Count; i++)
      {
          ImapMessage imapMessage = list[i];
      
          // Download the message to an instance of the MailMessage class. 
          MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
      
          // It can be downloaded with the sequence number. 
          // MailMessage msg = client.DownloadMailMessage(imapMessage.SequenceNumber); 
       
          // Display some information about it. 
          Console.WriteLine("Size: " + imapMessage.Size);
          Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
          Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
      }
      
      // Close the connection.
      client.Disconnect();