SmtpStartTls Method |
Namespace: MailBee.SmtpMail
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
This method lets the developer to switch the existing, already opened connection (usually, on port 25 or 587), to SSL/TLS mode. If the connection has been opened on a dedicated SSL port from the start (usually, port 465), the developer doesn't need to call this method.
The developer must call Hello method prior to calling StartTls. Also, Hello method must be called again after StartTls because TLS/SSL negotiation resets the connection.
As alternative to calling StartTls method, the developer can ask MailBee to start TLS/SSL negotiation automatically by setting SslMode property value to OnConnect or UseStartTls.
AutodetectPortAndSslMode property may cause MailBee to automatically enable TLS/SSL for some well-known hosts or ports even if SslMode property of the current connection (specified by SmtpServer object) has its default value. Be sure to always check IsSslConnection value prior to calling StartTls.
Note |
---|
Not all mail servers support STARTTLS functionality. |
// To use the code below, import MailBee namespaces at the top of your code. using MailBee; using MailBee.SmtpMail; // The actual code (put it into a method of your class) Smtp mailer = new Smtp(); mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret"); mailer.Connect(); mailer.Hello(); if (!mailer.IsSslConnection) { mailer.StartTls(); mailer.Hello(); } mailer.Login(); mailer.From.Email = "jdoe@domain.com"; mailer.To.Add("kathy@company.com"); mailer.Subject = "Report"; mailer.BodyPlainText = "The report contents"; mailer.Send(); mailer.Disconnect();