MailBee.NET Objects 3.1

Smtp.BeginSend Method 

Begins an asynchronous request for sending a mail message to recipients.

public IAsyncResult BeginSend(
   string senderEmail,
   EmailAddressCollection recipients,
   AsyncCallback callback,
   object state
);

Parameters

senderEmail
The e-mail address of the sender.
recipients
The list of the message recipients.
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 sending the message.

Remarks

This method is an asynchronous version of Send.

Exceptions

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

Example

This WinForms sample sends a mail message asynchronously. The callback function is used to receive notification on asynchronous method completion. Events are used to track send mail progress.

[C#]
// To use the code below, import MailBee namespaces at the top of your code.
using MailBee;
using MailBee.SmtpMail;
using MailBee.DnsMX;
using MailBee.Mime;

// Put the code below inside your class.

// MessageSubmittedToServer event handler.
private void OnMessageSubmittedToServer(object sender,
    SmtpMessageSubmittedToServerEventArgs e)
{
    // If no recipients received the message, this event won't be raised.
    // It's safe to assume AcceptedRecipients collection is not empty.
    string domain = e.AcceptedRecipients[0].GetDomain();
    MessageBox.Show("Message was sent to recipients at " + domain + " domain");
}

// MessageRecipientSubmitted event handler.
private void OnMessageRecipientSubmitted(object sender,
    SmtpMessageRecipientSubmittedEventArgs e)
{
    // If a particular recipient was refused, let the user know.
    if (!e.Result)
    {
        MessageBox.Show("For e-mail address " + e.RecipientEmail +
            ", server responded: " + e.ServerStatusMessage);
    }
}

// Send callback function.
private void SendCallback(IAsyncResult result)
{
    Smtp mailer = (Smtp)result.AsyncState;
    mailer.EndSend();
    MessageBox.Show("The e-mail was sent to: " +
        mailer.GetAcceptedRecipients().ToString());
}

// The actual code.
private void Form1_Load(object sender, System.EventArgs e)
{
    Smtp mailer = new Smtp();

    // Enable logging SMTP session into a file.
    mailer.Log.Enabled = true;
    mailer.Log.Filename = @"C:\log.txt";
    mailer.Log.Clear();
    mailer.Log.Format = LogFormatOptions.AddContextInfo;

    // Get the list of DNS servers for MX lookup from OS settings 
    // (app.config/web.config/machine.config files are ignored).
    mailer.DnsServers.Autodetect(
        DnsAutodetectOptions.Registry | DnsAutodetectOptions.Wmi);

    // Subscribe to events.
    mailer.MessageSubmittedToServer +=
        new SmtpMessageSubmittedToServerEventHandler(OnMessageSubmittedToServer);
    mailer.MessageRecipientSubmitted +=
        new SmtpMessageRecipientSubmittedEventHandler(OnMessageRecipientSubmitted);

    mailer.From.Email = "jdoe@domain.com";
    mailer.Subject = "Important details";

    // Demonstrate different methods of adding To, CC, and BCC recipients. 
    mailer.To.AsString = "kathy@domain.com, \"Bill Smith, Jr.\" <b.smith@company.com>";
    mailer.Cc.Add("Peter", "peter@domain.com");
    mailer.Cc.Add("john@domain.com");
    mailer.Bcc.AsString = "echelon@secret.com";

    // Specify HTML body.
    mailer.BodyHtmlText = "<html>Here are the details.</html>";

    // Create plain-text version.
    mailer.Message.MakePlainBodyFromHtmlBody();

    // Initiate an asynchronous send mail attempt.
    mailer.BeginSend(null, null, new AsyncCallback(SendCallback), mailer);
}
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code.
Imports MailBee
Imports MailBee.SmtpMail
Imports MailBee.DnsMX
Imports MailBee.Mime

' Put the code below inside your class.

' MessageSubmittedToServer event handler.
Private Sub OnMessageSubmittedToServer(ByVal sender As Object, _
    ByVal e As SmtpMessageSubmittedToServerEventArgs)
    ' If no recipients received the message, this event won't be raised.
    ' It's safe to assume AcceptedRecipients collection is not empty.
    Dim domain As String = e.AcceptedRecipients(0).GetDomain()
    MsgBox("Message was sent to recipients at " & domain & " domain")
End Sub

' MessageRecipientSubmitted event handler.
Private Sub OnMessageRecipientSubmitted(ByVal sender As Object, _
    ByVal e As SmtpMessageRecipientSubmittedEventArgs)
    ' If a particular recipient was refused, let the user know.
    If (Not e.Result) Then
        MsgBox("For e-mail address " & e.RecipientEmail & _
            ", server responded: " & e.ServerStatusMessage)
    End If
End Sub

' Send callback function.
Private Sub SendCallback(ByVal result As IAsyncResult)
    Dim mailer As Smtp = result.AsyncState
    mailer.EndSend()
    MsgBox("The e-mail was sent to: " & _
        mailer.GetAcceptedRecipients().ToString())
End Sub

' The actual code.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim mailer As New Smtp

    ' Enable logging SMTP session into a file.
    mailer.Log.Enabled = True
    mailer.Log.Filename = "C:\log.txt"
    mailer.Log.Clear()
    mailer.Log.Format = LogFormatOptions.AddContextInfo

    ' Get the list of DNS servers for MX lookup from OS settings 
    ' (app.config/web.config/machine.config files are ignored).
    mailer.DnsServers.Autodetect( _
    DnsAutodetectOptions.Registry Or DnsAutodetectOptions.Wmi)

    ' Subscribe to events.
    AddHandler mailer.MessageSubmittedToServer, AddressOf OnMessageSubmittedToServer
    AddHandler mailer.MessageRecipientSubmitted, AddressOf OnMessageRecipientSubmitted

    mailer.From.Email = "jdoe@domain.com"
    mailer.Subject = "Important details"

    ' Demonstrate different methods of adding To, CC, and BCC recipients. 
    mailer.To.AsString = "kathy@domain.com, ""Bill Smith, Jr."" <b.smith@company.com>"
    mailer.Cc.Add("Peter", "peter@domain.com")
    mailer.Cc.Add("john@domain.com")
    mailer.Bcc.AsString = "echelon@secret.com"

    ' Specify HTML body.
    mailer.BodyHtmlText = "<html>Here are the details.</html>"

    ' Create plain-text version.
    mailer.Message.MakePlainBodyFromHtmlBody()

    ' Initiate an asynchronous send mail attempt.
    mailer.BeginSend(Nothing, Nothing, New AsyncCallback(AddressOf SendCallback), mailer)
End Sub

See Also

Smtp Class | MailBee.SmtpMail Namespace | Send