ComponentPro UltimateMail

      Sending multiple messages

      Language Filter: AllSend comments on this topic to ComponentPro

      It's better and faster to send multiple messages in one connection than sending them in separate connections. The following example shows how to accomplish this:

      using System;
      using System.Windows.Forms;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      const string serverName = "myserver";
      const string user = "name@domain.com";
      const string password = "mytestpassword";
      const int port = 465;
      const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
      Smtp client = new Smtp();
      try
      {
          MailMessage mmMessage = new MailMessage();
          mmMessage.From.Add("from@thedomain.com");
          mmMessage.To.Add("name@domain.com");
          mmMessage.Subject = "Test Subject";
          mmMessage.BodyText = "Test Content";
      
          MailMessage mmMessage2 = new MailMessage();
          mmMessage.From.Add("from@thedomain.com");
          mmMessage.To.Add("someone@thedomain.com");
          mmMessage.Subject = "Test Subject";
          mmMessage.BodyText = "Test Content";
      
          Console.WriteLine("Connecting SMTP 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);
      
          Console.WriteLine("Sending the first message...");
          client.Send(mmMessage);
          Console.WriteLine("Message sent...");
      
          Console.WriteLine("Sending the second message...");
          client.Send(mmMessage2);
          Console.WriteLine("Message sent...");
      
          // Disconnect. 
          Console.WriteLine("Disconnecting...");
          client.Disconnect();
      }
      catch (SmtpException smtpExc)
      {
          MessageBox.Show(string.Format("An SMTP error occurred: {0}, ErrorStatus: {1}", smtpExc.Message, smtpExc.Status));
      }
      catch (Exception exc)
      {
          MessageBox.Show(string.Format("An error occurred: {0}", exc.Message));
      }