Use SSL with SMTP
Mail.dll SMTP client supports Secure Socket Layer (SSL) and Transport Layer Security (TLS) protocols to authenticate the server and secure client-server communication.
There are two modes in which Mail.dll can work:
- Implicit - where Mail.dll SMTP client immediately connects using secure channel,
- Explicit - where Mail.dll SMTP client connects on unsecured channel first and then secures the communication by issuing STARTTLS command.
In both cases, by default, either Secure Sockets Layer (SSL) 3.0 or Transport Layer Security (TLS) 1.0 are acceptable for secure communication. You can change the defaults by using Smtp.SSLConfiguration property.
Smtp client may decide to secure the channel, if SMTP server explicitly forbids logging-in on unsecured channel and you are using UseBestLogin method.
Implicit mode:
// C# version
using Limilabs.Mail;
using Limilabs.Client.POP3;
class Program
{
static void Main(string[] args)
{
using (Smtp smtp = new Smtp())
{
smtp.ConnectSSL("server.example.com");
smtp.Login("user", "password");
MailBuilder builder = new MailBuilder();
builder.Text = "text";
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("to@example.com"));
IMail email = builder.Create();
smtp.SendMessage(email);
smtp.Close();
}
}
};
' VB.NET version
Imports Limilabs.Mail
Imports Limilabs.Client.SMTP
Public Module Module1
Public Sub Main(ByVal args As String())
Using smtp As New Smtp()
smtp.ConnectSSL("server.example.com")
smtp.Login("user", "password")
Dim builder As New MailBuilder()
builder.Text = "text"
builder.From.Add(New MailBox("from@example.com"))
builder.[To].Add(New MailBox("to@example.com"))
Dim email As IMail = builder.Create()
smtp.SendMessage(email)
smtp.Close()
End Using
End Sub
End Module
Explicit mode:
// C# version
using Limilabs.Mail;
using Limilabs.Mail.Headers;
using Limilabs.Client.SMTP;
class Program
{
static void Main(string[] args)
{
using (Smtp smtp = new Smtp())
{
smtp.Connect("server.example.com");
smtp.StartTLS();
smtp.Login("user", "password");
MailBuilder builder = new MailBuilder();
builder.Text = "text";
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("to@example.com"));
IMail email = builder.Create();
smtp.SendMessage(email);
smtp.Close();
}
}
};
' VB.NET version
Imports Limilabs.Mail
Imports Limilabs.Mail.Headers
Imports Limilabs.Client.SMTP
Public Module Module1
Public Sub Main(ByVal args As String())
Using smtp As New Smtp()
smtp.Connect("server.example.com")
smtp.StartTLS()
smtp.Login("user", "password")
Dim builder As New MailBuilder()
builder.Text = "text"
builder.From.Add(New MailBox("from@example.com"))
builder.[To].Add(New MailBox("to@example.com"))
Dim email As IMail = builder.Create()
smtp.SendMessage(email)
smtp.Close()
End Using
End Sub
End Module
If you are using self-signed certificates you may encounter this error: The remote certificate is invalid according to the validation procedure.
March 4th, 2011 at 17:53
How i can get this class named Limilabs,
without this it is not usefull for any body
March 6th, 2011 at 10:58
@leo
You can download Mail.dll here:
http://www.limilabs.com/mail
October 3rd, 2011 at 13:20
something is missing: ERROR: Send hello first
means do a hello before sending STARTTLS
October 4th, 2011 at 17:28
@Hendrik
You are right smtp.Ehlo() is missing from the examples (after Connect and after StartTLS methods).The latest version will not require those.
Edit:
Helo/Ehlo commands are no longer needed in the latest version.
October 25th, 2011 at 18:31
I recently purchased your product and have successfully used it to read emails. Today, I started SMTP tests and am getting an error when I attempt to use the ConnectSSL method that accepts a port #. Also, I get an error if I try to call the StartTLS method. All my test use a Gmail account.
ConnectSSL error:
The handshake failed due to an unexpected packet format.
StartTLS error:
5.5.1 Unrecognized command. u1sm39033912yhu.11
Thanks
October 25th, 2011 at 19:40
@Tim
You need to specify correct port to use SSL. From SMTP protocol it’s usually 465. You can also use Smtp.DefaultSSLPort property.
using (Smtp client = new Smtp()) { client.Connect("smtp.gmail.com", 465, true); // which is equivalent to: // client.ConnectSSL("smtp.gmail.com");To use StartTLS method first connect without SSL (otherwise you’ll get “Unrecognized command” error).
using (Smtp client = new Smtp()) { client.Connect("smtp.gmail.com", 25, false); // which is equivalent to: // client.Connect("smtp.example.org"); client.StartTLS();You can check if server supports StartTLS command with following code:
November 5th, 2011 at 12:12
[...] Use SSL with SMTP [...]