Begins an asynchronous request to start TLS/SSL negotiation with the server.
An IAsyncResult that references the asynchronous TLS/SSL negotiation.
This method is an asynchronous version of StartTls.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidStateException | There is already an operation in progress. |
Asynchronous TLS/SSL negotiation with the SMTP server in WinForms application. This sample also handles TlsStarted event. Wait method is used to wait for the asynchronous method completion, since .NET's standard WaitOne cannot process events.
[C#] // To use the code below, import MailBee namespaces at the top of your code. using MailBee; using MailBee.SmtpMail; // Put the code below inside your class. // TlsStarted event handler. private void OnTlsStarted(object sender, TlsStartedEventArgs e) { MessageBox.Show("TLS/SSL negotiation complete. Secure connection is ready."); } // The actual code. private void Form1_Load(object sender, System.EventArgs e) { Smtp mailer = new Smtp(); // Let MailBee process events. mailer.RaiseEventsViaMessageLoop = false; mailer.SmtpServers.Add("smtp.domain.com"); mailer.TlsStarted += new TlsStartedEventHandler(OnTlsStarted); mailer.Connect(); mailer.Hello(); // Initiate an asynchronous TLS/SSL negotiation. mailer.BeginStartTls(null, null); // Simulate some lengthy work here... for (int i = 0; i < 100; i++) { // Make a portion of the work. System.Threading.Thread.Sleep(10); // Process events which were raised during execution of the work above. mailer.Wait(0); } // If the connection was not established during execution of the lengthy // work, wait until it's established. mailer.Wait(); // End the connection request. mailer.EndStartTls(); // The connection is now under TLS/SSL layer! mailer.Hello(); // Disconnect from the server. mailer.Disconnect(); }
[Visual Basic] ' To use the code below, import MailBee namespaces at the top of your code. Imports MailBee Imports MailBee.SmtpMail ' Put the code below inside your class. ' TlsStarted event handler. Private Sub OnTlsStarted(ByVal sender As Object, ByVal e As TlsStartedEventArgs) MsgBox("TLS/SSL negotiation complete. Secure connection is ready.") End Sub ' The actual code. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim mailer As New Smtp ' Let MailBee process events. mailer.RaiseEventsViaMessageLoop = False mailer.SmtpServers.Add("smtp.domain.com") AddHandler mailer.TlsStarted, AddressOf OnTlsStarted mailer.Connect() mailer.Hello() ' Initiate an asynchronous TLS/SSL negotiation. mailer.BeginStartTls(Nothing, Nothing) ' Simulate some lengthy work here... Dim i As Integer For i = 1 To 100 ' Make a portion of the work. System.Threading.Thread.Sleep(10) ' Process events which were raised during execution of the work above. mailer.Wait(0) Next ' If the connection was not established during execution of the lengthy ' work, wait until it's established. mailer.Wait() ' End the connection request. mailer.EndStartTls() ' The connection is now under TLS/SSL layer! mailer.Hello() ' Disconnect from the server. mailer.Disconnect() End Sub
Smtp Class | MailBee.SmtpMail Namespace | StartTls