MailBee.NET Objects 3.1

Smtp.BeginHello Method 

Begins an asynchronous request for sending initial greeting to the SMTP server.

public IAsyncResult BeginHello(
   AsyncCallback callback,
   object state
);

Parameters

callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous greeting.

Remarks

This method is an asynchronous version of Hello.

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This console sample connects to the SMTP server, sends greeting asynchronously, and handles ErrorOccurred event to trap the warning telling that EHLO is not supported, and HELO will be tried (this warning event will be raised only if the server is not ESMTP enabled). No callback function is used.

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

class Sample
{
    // ErrorOccurred event handler.
    private static void OnErrorOccurred(object sender, ErrorEventArgs e)
    {
        if (e.Reason is MailBeeSmtpOptionalCommandNotSupportedException)
        {
            Console.WriteLine(e.Reason.Message); // EHLO not supported.
        }
    }

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

        mailer.SmtpServers.Add("mail.domain.com");

        mailer.Connect();

        // Subscribe to the ErrorOccurred event. We subcribe only for the time 
        // of Hello() method execution to make sure the trapped warnings do not 
        // relate to execution of any other methods.
        mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred);

        // Initiate an asynchronous greeting.
        mailer.BeginHello(null, null);

        // Simulate some lengthy work here. At the same time, 
        // the greeting is performed on another thread.
        System.Threading.Thread.Sleep(1000);

        // End the connection request.
        mailer.EndHello();

        mailer.ErrorOccurred -= new ErrorEventHandler(OnErrorOccurred);

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

Class Sample
    ' ErrorOccurred event handler.
    Private Shared Sub OnErrorOccurred(ByVal sender As Object, ByVal e As ErrorEventArgs)
        If (TypeOf e.Reason Is MailBeeSmtpOptionalCommandNotSupportedException) Then
            Console.WriteLine(e.Reason.Message)' EHLO not supported.
        End If
    End Sub

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

        mailer.SmtpServers.Add("mail.domain.com")

        mailer.Connect()

        ' Subscribe to the ErrorOccurred event. We subcribe only for the time 
        ' of Hello() method execution to make sure the trapped warnings do not 
        ' relate to execution of any other methods.
        AddHandler mailer.ErrorOccurred, AddressOf OnErrorOccurred

        ' Initiate an asynchronous greeting.
        mailer.BeginHello(Nothing, Nothing)

        ' Simulate some lengthy work here. At the same time, 
        ' the greeting is performed on another thread.
        System.Threading.Thread.Sleep(1000)

        ' End the connection request.
        mailer.EndHello()

        RemoveHandler mailer.ErrorOccurred, AddressOf OnErrorOccurred

        mailer.Disconnect()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace | Hello