ComponentPro UltimateMail

Connecting and authenticating to an SMTP server Synchronously

Language Filter: AllSend comments on this topic to ComponentPro

To authenticate to an SMTP server, you can simply perform the following steps

  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 one of Authenticate methods. The code looks similar to the following:
    // Login to the server.
    client.Authenticate("user", "password");
    
  5. Do your work like creating a new mail message and send it, etc. 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();