ComponentPro UltimateMail

      Sending a message Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      Mail can be sent using Smtp with only a few lines of code. In fact, for the simplest method of sending, no knowledge of message format is required at all.

      The following steps will help you to do that:

      Sending 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 ComponentPro.Net.Mail;
        
      2. Create a new instance of the Smtp class.
      3. Now you can connect to the SMTP server with Connect methods. The code looks similar to the following:
        // Create a new instance of the Smtp class.
        Smtp client = new Smtp();
        
        // Connect to the server.
        client.Connect("myserver");
        
        // Or you can specify the SMTP port with 
        // client.Connect("myserver", 25);
        
      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 create a new message object and send it with the Send method. The code looks similar to the following:
        // Create a new mail message.
        MailMessage msg = new MailMessage();
        msg.Subject = "Test Subject";
        msg.BodyText = "Content";
        msg.From = "from@mydomain.com";
        msg.To = "to@somedomain.com";
        
        // And send it.
        client.Send(msg);
        
      6. After completing your work, call the Disconnect method to close the SMTP session.

      Final example code

      using ComponentPro.Net.Mail;
      
      ...
      
      // Create a new instance of the Smtp class.
      Smtp client = new Smtp();
      
      // Connect to the server.
      client.Connect("myserver");
      
      // Or you can specify the SMTP port with 
      // client.Connect("myserver", 25); 
       
      // Login to the server.
      client.Authenticate("user", "password");
      
      // Create a new mail message.
      MailMessage msg = new MailMessage();
      msg.Subject = "Test Subject";
      msg.BodyText = "Content";
      msg.From = "from@mydomain.com";
      msg.To = "to@somedomain.com";
      
      // And send it.
      client.Send(msg);
      
      // Close the connection.
      client.Disconnect();