ComponentPro UltimateMail

      Downloading mail message Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      Downloading mail messages from a POP3 server in Pop3 class 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 POP3 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 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 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:
        // Get the message list.
        Console.WriteLine("Getting message list...");
        Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.MessageInboxIndex | Pop3EnvelopeParts.Size);
        
        // Get messages. 
        for (int i = 0; i < list.Count; i++)
        {
            Pop3Message pop3Message = list[i];
        
            // Download the message to an instance of the MailMessage class. 
            MailMessage msg = client.DownloadMailMessage(pop3Message.MessageInboxIndex);
        
            // Display some information about it. 
            Console.WriteLine("Size: " + pop3Message.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 POP3 session.

      Final example code

      using System;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      // 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.MessageInboxIndex | Pop3EnvelopeParts.Size);
      
      // Get messages. 
      for (int i = 0; i < list.Count; i++)
      {
          Pop3Message pop3Message = list[i];
      
          // Download the message to an instance of the MailMessage class. 
          MailMessage msg = client.DownloadMailMessage(pop3Message.MessageInboxIndex);
      
          // Display some information about it. 
          Console.WriteLine("Size: " + pop3Message.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();