MailBee.NET Objects 4.0

Smtp.TlsStarted Event

Occurs when the connection with the server becomes secure.

public event TlsStartedEventHandler TlsStarted;

Event Data

The event handler receives an argument of type TlsStartedEventArgs containing data related to this event. The following TlsStartedEventArgs properties provide information specific to this event.

Property Description
Protocol Gets application-level protocol of the current connection.
RemoteEndPoint Gets a reference to the end point of the server host.
RemoteHostName Gets the host name of the server.
State Gets a reference to the object which was supplied by the developer in state parameter of asynchronous methods of the mailer components.

Remarks

This event is raised when TLS/SSL negotiation completes and TLS session successfully starts. Usually, this happens as a result of successful completion of StartTls method or if automatic TLS/SSL negotiation was requested via setting SslMode property of the active SmtpServer object to non-Manual value.

Example

This console sample demonstrates that TlsStarted event is raised during executing Hello method when SslMode property of the active SmtpServer object is set to UseStartTls value.

[C#]
using System;
using MailBee;
using MailBee.SmtpMail;
using MailBee.Security;

class Sample
{
    // TlsStarted event handler.
    private static void OnTlsStarted(object sender, TlsStartedEventArgs e)
    {
        // This will happen during Hello method execution.
        Console.WriteLine("TLS/SSL session started.");
    }

    // The actual code.
    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Subscribe to TlsStarted event.
        mailer.TlsStarted += new TlsStartedEventHandler(OnTlsStarted);

        // Notify MailBee it should start TLS/SSL session when appropriate.
        // The connection is made to the regular port so that STLS command
        // will be used to start TLS/SSL session.
        SmtpServer server = new SmtpServer("smtp.company.com");
        server.SslMode = SslStartupMode.UseStartTls;
        mailer.SmtpServers.Add(server);

        mailer.Connect();

        Console.WriteLine("Connected to the server. Will send greeting now...");

        // TLS/SSL negotiation will take place here. Thus, any subseqeunt commands
        // such as sending user credentials or mail message submission will take
        // place under secure TLS/SSL layer.
        // Note: EHLO command will be sent twice: first, before TLS/SSL negotiation;
        // second, after. This is because capability list presented by EHLO response
        // can change after TLS negotiation.
        mailer.Hello();

        Console.WriteLine("Hello completed successfully.");

        // Can send mail, perform login or whatever here.

        mailer.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.SmtpMail
Imports MailBee.Security

Public Class Sample
    ' TlsStarted event handler.
    Private Shared Sub OnTlsStarted(ByVal sender As Object, ByVal e As TlsStartedEventArgs)
        ' This will happen during Hello method execution.
        Console.WriteLine("TLS/SSL session started.")
    End Sub

    ' The actual code.
    Shared Sub Main(ByVal args As String())
        Dim mailer As New Smtp

        ' Subscribe to TlsStarted event.
        AddHandler mailer.TlsStarted, AddressOf OnTlsStarted

        ' Notify MailBee it should start TLS/SSL session when appropriate.
        ' The connection is made to the regular port so that STLS command
        ' will be used to start TLS/SSL session.
        Dim server As SmtpServer = New SmtpServer("smtp.company.com")
        server.SslMode = SslStartupMode.UseStartTls
        mailer.SmtpServers.Add(server)

        mailer.Connect()

        Console.WriteLine("Connected to the server. Will send greeting now...")

        ' TLS/SSL negotiation will take place here. Thus, any subseqeunt commands
        ' such as sending user credentials or mail message submission will take
        ' place under secure TLS/SSL layer.
        ' Note: EHLO command will be sent twice: first, before TLS/SSL negotiation;
        ' second, after. This is because capability list presented by EHLO response
        ' can change after TLS negotiation.
        mailer.Hello()

        Console.WriteLine("Hello completed successfully.")

        ' Can send mail, perform login or whatever here.

        mailer.Disconnect()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace | SslMode | StartTls