ComponentPro UltimateMail

Connecting through a proxy server

Language Filter: AllSend comments on this topic to ComponentPro

The Ultimate SMTP component fully supports many proxy servers (often referred to as "proxies"). If you need to connect to your SMTP server through a proxy, simply create a new instance of the WebProxyEx class, set the appropriate properties of the WebProxyEx object, assign it to the Proxy property of the Smtp class and the necessary proxy communication will take place.

Ultimate SMTP component supports the following Proxy servers:

SOCKS4 SOCKS4 proxy.
SOCKS4A SOCKS4A proxy (capable of resolving domain names).
SOCKS5 SOCKS5 proxy.
HTTP CONNECT (Basic and NTLM supported) HTTP proxy using CONNECT method.

The following steps will help you to connect to an SMTP server through a proxy server:

Connecting through a proxy server

  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;
    using ComponentPro.Net.Mail;
    
  2. Create a new instance of the Smtp class.
  3. Create a new instance of the WebProxyEx class, set the appropriate properties of the WebProxyEx object and assign it to the Proxy property of the Smtp class.
  4. Now you can connect to the SMTP server with Connect methods. The code looks similar to the following:
    // Create a new instance.
    Smtp client = new Smtp();
    
    // Create a new proxy object.
    WebProxyEx proxy = new WebProxyEx();
    proxy.Server = "proxyserver"; // Set proxy address here.
    proxy.Port = 1080; // Set proxy port here.
    proxy.UserName = "username"; // Proxy user name.
    proxy.Password = "password"; // Password.
    
    client.Proxy = proxy;
    
    // Connect to the SMTP server.
    client.Connect("myserver");
    
  5. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
    // Authenticate.
    client.Authenticate("userName", "password");
    
  6. 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);
    
  7. After completing your work, call the Disconnect method to close the SMTP session.

Final example code

using ComponentPro.Net;
using ComponentPro.Net.Mail;

...

// Create a new instance.
Smtp client = new Smtp();

// Create a new proxy object.
WebProxyEx proxy = new WebProxyEx();
proxy.Server = "proxyserver"; // Set proxy address here.
proxy.Port = 1080; // Set proxy port here.
proxy.UserName = "username"; // Proxy user name.
proxy.Password = "password"; // Password.

client.Proxy = proxy;

// Connect to the SMTP server.
client.Connect("myserver");

// Authenticate.
client.Authenticate("userName", "password");

// Do something here... 
// ... 
 
// 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);

// Disconnect.
client.Disconnect();