More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Connecting
On this page:
Connecting to servers using SMTP protocol
To connect to an SMTP server running on default port (25), call the single-parameter Connect
method.
A plain (unencrypted) connection is established.
// create SMTP client instance
var client = new Rebex.Net.Smtp();
// connect to a server
client.Connect(hostname);
' create SMTP client instance
Dim client = New Rebex.Net.Smtp()
' connect to a server
client.Connect(hostname)
Connect
method as an additional argument.
Note: Historically, port 25 was the default for SMTP. Recently, another default port of 587 gained traction.
- Port 25 is used for communication between two SMTP servers. The sender's SMTP server uses it for sending e-mail to recipient's SMTP server. In this case, authentication is not required, but the target SMTP server only accepts emails for it's recipients.
- Port 587 is used for communication between an e-mail client and the SMTP server used to send its outgoing mail messages. Mail clients use it for submitting e-mail to their SMTP server for delivery. In this case, authentication is usually required, and any valid recipients are accepted. The client's SMTP server then sends the e-mail to the recipients's SMTP server (port 25).
Connecting to servers using explicit or implicit TLS/SSL
To establish a secure TLS/SSL connection to an SMTP server, use the Connect
method with SslMode
argument.
Tip: Learn about the difference between explicit and implicit TLS/SSL modes.
// create SMTP client instance
var client = new Rebex.Net.Smtp();
// connect to a server using explicit TLS/SSL
client.Connect(hostname, SslMode.Explicit);
// connect to a server using implicit TLS/SSL
//client.Connect(hostname, SslMode.Implicit);
' create SMTP client instance
Dim client = New Rebex.Net.Smtp()
' connect to a server using explicit TLS/SSL
client.Connect(hostname, SslMode.Explicit)
' connect to a server using implicit TLS/SSL
'client.Connect(hostname, SslMode.Implicit);
Server certificate is validated automatically by Rebex SMTP, but you can customize the validation process as well.
Note: For Rebex.Net.Smtp
, if port number is not specified, the 587 is used as a default port number with explicit TLS/SSL mode.
Tip: For advanced security options, see TLS/SSL core section.
Switching to encrypted communication
It's also possible to establish a plain (unencrypted) connection first and upgrade to TLS/SSL later using the Secure
method.
This is not available in EWS.
// create SMTP client instance
var client = new Rebex.Net.Smtp();
// connect to a server on default (not-secured) port 25
client.Connect(hostname);
// secure the connection with TLS/SSL
client.Secure();
// authenticate securely
client.Login(username, password);
' create SMTP client instance
Dim client = New Rebex.Net.Smtp()
' connect to a server on default (not-secured) port 25
client.Connect(hostname)
' secure the connection with TLS/SSL
client.Secure()
' authenticate securely
client.Login(username, password)
Setting connection options
There are various connection options such as character sets, proxies, SMTP extensions, and more.
// create SMTP client instance
// ...
// use SOCKS5 proxy
client.Proxy.ProxyType = ProxyType.Socks5;
// ...
// set UTF-8 encoding for commands and responses
client.Encoding = Encoding.UTF8;
// allow only TLS 1.1
client.Settings.SslAllowedVersions = TlsVersion.TLS11;
// disable Pipelining extension
client.EnabledExtensions &= ~SmtpExtensions.Pipelining;
// connect, log in, send emails
// ...
' create SMTP client instance
' ...
' use SOCKS5 proxy
smtp.Proxy.ProxyType = ProxyType.Socks5
' ...
' set UTF-8 encoding for commands and responses
smtp.Encoding = Encoding.UTF8
' allow only TLS 1.1
smtp.Settings.SslAllowedVersions = TlsVersion.TLS11
' disable Pipelining extension
smtp.EnabledExtensions = smtp.EnabledExtensions And Not SmtpExtensions.Pipelining
' connect, log in, send emails
' ...
Getting info about the connection
Once connected (or authenticated), you can get useful information about the current connection such as server name, server port or user name. Don't be afraid - user's password is not stored with the connection.
// create SMTP client instance
var client = new Rebex.Net.Smtp();
// connect to a server
client.Connect(hostname, SslMode.Explicit);
// display info
Console.WriteLine("Connected to {0}:{1}.", client.ServerName, client.ServerPort);
// authenticate
client.Login(username, password);
// display info to the user
Console.WriteLine("Logged on as {0}.", client.UserName);
' create SMTP client instance
Dim client = New Rebex.Net.Smtp()
' connect to a server
client.Connect(hostname, SslMode.Explicit)
' display info
Console.WriteLine("Connected to {0}:{1}.", client.ServerName, client.ServerPort)
' authenticate
client.Login(username, password)
' display info to the user
Console.WriteLine("Logged on as {0}.", client.UserName)
Getting info about TLS/SSL connection
To get information about SSL connection, use TlsSocket
property.
// create SMTP client instance
var client = new Rebex.Net.Smtp();
// connect to a server
client.Connect(hostname, SslMode.Explicit);
// Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", client.TlsSocket.Cipher);
// ServerCertificate property provides access to server certificate
// the first certificate in the chain is the server's certificate
CertificateChain chain = client.TlsSocket.ServerCertificate;
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
chain[0].GetSubject(), chain[0].GetIssuer());
' create SMTP client instance
Dim client = New Rebex.Net.Smtp()
' connect to a server
client.Connect(hostname, SslMode.Explicit)
' Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", client.TlsSocket.Cipher)
' ServerCertificate property provides access to server certificate
' the first certificate in the chain is the server's certificate
Dim chain As CertificateChain = client.TlsSocket.ServerCertificate
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
chain(0).GetSubject(), chain(0).GetIssuer())
Back to feature list...