ComponentPro UltimateMail

      Sending HTML mail

      Language Filter: AllSend comments on this topic to ComponentPro

      UltimateMail makes it easy to create a complete HTML email message with only a few lines of code. To accomplish this, tou can either set the property BodyHtml or call the Load method of the MailMessage class.

      The following example illustrates how to load a HTML file from disk as the body of a mail message and send it:

      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 msg = new MailMessage();
          msg.Load("mycontent.html");
          msg.From.Add("from@thedomain.com");
          msg.To.Add("name@domain.com");
          msg.Subject = "Test Subject";                
      
          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 message...");
          client.Send(msg);
          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));
      }