ComponentPro UltimateMail

      Uploading a message Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      IMAP also allows you to upload message from a client to a folder on a IMAP server, and Imap class provides the UploadMessage method for this purpose. By using the UploadMessage method you can upload message from a various source such as from file, stream, or MailMessage object. Unlike other message-related methods, the UploadMessage method uploads the message into a specified folder, so that the current folder is not used for the operation, and you do not need to set the current folder before using the UploadMessage method.

      The following steps will help you to do that:

      Uploading a 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:
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "password";
        const int port = 993;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        Imap client = new Imap();
        try
        {
            Console.WriteLine("Connecting IMAP 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 create a new MailMessage object and upload it into INBOX mailbox. The code looks similar to the following:
        // Create a new MailMessage object.
        MailMessage message = new MailMessage();
        message.From.Add("from@thedomain.com");
        message.To.Add("name@domain.com");
        message.Subject = "Test Subject";
        message.BodyText = "Test Content";
        
        // Upload message.
        Console.WriteLine("Uploading message...");
        // Upload the message to the 'Inbox' mailbox.
        client.UploadMessage("Inbox", message);
        
      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;
      
      ...
      
      const string serverName = "myserver";
      const string user = "name@domain.com";
      const string password = "password";
      const int port = 993;
      const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
      Imap client = new Imap();
      try
      {
          Console.WriteLine("Connecting IMAP 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);
      
          // Create a new MailMessage object. 
          MailMessage message = new MailMessage();
          message.From.Add("from@thedomain.com");
          message.To.Add("name@domain.com");
          message.Subject = "Test Subject";
          message.BodyText = "Test Content";
      
          // Upload message. 
          Console.WriteLine("Uploading message...");
          // Upload the message to the 'Inbox' mailbox. 
          client.UploadMessage("Inbox", message);
      
          // Disconnect. 
          Console.WriteLine("Disconnecting...");
          client.Disconnect();
      
      }
      catch (ImapException imapExc)
      {
          Console.WriteLine(string.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status));
      }
      catch (Exception exc)
      {
          Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
      }