MailBee.NET Objects 4.0

Smtp.BeginSendJobs Method 

Begins an asynchronous request to process the pending jobs and send out the resulting e-mails.

public IAsyncResult BeginSendJobs(
   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 asynchronous sending e-mails accordingly the assigned pending jobs.

Remarks

This method is an asynchronous version of SendJobs method.

Example

This console sample performs mail merge asynchronously. The sample gets notified of mail merge completion by using a callback function.

[C#]
using System;
using System.Data;
using System.Data.OleDb;
using System.Threading;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;

class Sample
{
    private static bool finished = false;

    // SendJobs callback function.
    private static void SendJobsCallback(IAsyncResult result)
    {
        Smtp mailer = (Smtp)result.AsyncState;

        mailer.EndSendJobs();

        // Report results (row indices in the data table) to the console.
        if (mailer.JobsFailed.Count == 0)
        {
            Console.WriteLine("Newsletter has been sent out without any errors.");
        }
        else
        {
            if (mailer.JobsSuccessful.Count == 0)
            {
                Console.WriteLine("All newsletter e-mails failed to be sent.");
            }
            else if (mailer.JobsFailed.Count > 0)
            {
                Console.WriteLine("Not all newsletter e-mails have been sent.");
                Console.WriteLine();

                // Obtain a reference to the DataTable used for mail merge.
                DataTable table = mailer.JobsFailed[0].MergeTable;

                Console.WriteLine("Successful rows: ");
                Console.WriteLine(mailer.JobsSuccessful.GetIndicesAsString(table, null));
                Console.WriteLine();

                Console.WriteLine("Failed rows: ");
                Console.WriteLine(mailer.JobsFailed.GetIndicesAsString(table, null));
            }
            else
            {
                Console.WriteLine("There was nothing to send.");
            }
        }

        Console.WriteLine();

        finished = true;
    }

    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Logging into a file is useful for troubleshooting.
        mailer.Log.Filename = @"C:\log.txt";
        mailer.Log.Enabled = true;
        mailer.Log.Format = LogFormatOptions.AddContextInfo;
        mailer.Log.Clear();

        // Uncomment the line below to use unlimited number of worker threads (up to 60)
        // and increase performance. Note that not all SMTP servers support this.

        // mailer.MaxThreadCount = -1;

        // Setup SMTP server parameters.
        mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");

        // Setup e-mail message header template for mail merge.
        mailer.Message.From.AsString = "John Doe <john.doe@domain.com>";
        mailer.Message.To.AsString = "##Name## <##Email##>";
        mailer.Message.Subject = "Our newsletter";

        // Setup HTML body template.
        mailer.Message.BodyHtmlText = "<html>##Body##</html>";

        // Specify database connection string (it may be different in your case).
        string connParams = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Newsletter.mdb;";

        // Connect to the database and populate mail merge job to-do list with
        // the data from "mailing_list" table.
        using (OleDbConnection conn = new OleDbConnection(connParams))
        {
            // Open the connection and get the data.
            OleDbCommand command = new OleDbCommand("SELECT * FROM mailing_list", conn);
            conn.Open();
            DataTable table = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(table);

            // Create a job which is the following task for MailBee: perform mail merge
            // of a newsletter template with data rows of the specified data table and
            // send out each resulting e-mail to its intended recipients.
            mailer.AddJob(null, null, null, table);
        }

        // Start processing jobs. The actual mail merge takes place here.
        mailer.BeginSendJobs(new AsyncCallback(SendJobsCallback), mailer);

        // Can do anything else while the mail merge takes place in the background.
        while (!finished)
        {
            Thread.Sleep(1000);
        }

        Console.WriteLine("Processing jobs done.");
    }
}
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Threading
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail

Class Sample
    Private Shared finished As Boolean = False

    ' SendJobs callback function.
    Private Shared Sub SendJobsCallback(ByVal result As IAsyncResult)
        Dim mailer As Smtp = CType(result.AsyncState, Smtp)

        mailer.EndSendJobs()

        ' Report results (row indices in the data table) to the console.
        If mailer.JobsFailed.Count = 0 Then
            Console.WriteLine("Newsletter has been sent out without any errors.")
        Else
            If mailer.JobsSuccessful.Count = 0 Then
                Console.WriteLine("All newsletter e-mails failed to be sent.")
            ElseIf mailer.JobsFailed.Count > 0 Then
                Console.WriteLine("Not all newsletter e-mails have been sent.")
                Console.WriteLine()

                ' Obtain a reference to the DataTable used for mail merge.
                Dim table As DataTable = mailer.JobsFailed(0).MergeTable

                Console.WriteLine("Successful rows: ")
                Console.WriteLine(mailer.JobsSuccessful.GetIndicesAsString(table, Nothing))
                Console.WriteLine()

                Console.WriteLine("Failed rows: ")
                Console.WriteLine(mailer.JobsFailed.GetIndicesAsString(table, Nothing))
            Else
                Console.WriteLine("There was nothing to send.")
            End If
        End If

        Console.WriteLine()

        finished = True
    End Sub

    Shared Sub Main(ByVal args() As String)
        Dim mailer As Smtp = New Smtp

        ' Logging into a file is useful for troubleshooting.
        mailer.Log.Filename = "C:\log.txt"
        mailer.Log.Enabled = True
        mailer.Log.Format = LogFormatOptions.AddContextInfo
        mailer.Log.Clear()

        ' Uncomment the line below to use unlimited number of worker threads (up to 60)
        ' and increase performance. Note that not all SMTP servers support this.

        ' mailer.MaxThreadCount = -1;

        ' Setup SMTP server parameters.
        mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret")

        ' Setup e-mail message header template for mail merge.
        mailer.Message.From.AsString = "John Doe <john.doe@domain.com>"
        mailer.Message.To.AsString = "##Name## <##Email##>"
        mailer.Message.Subject = "Our newsletter"

        ' Setup HTML body template.
        mailer.Message.BodyHtmlText = "<html>##Body##</html>"

        ' Specify database connection string (it may be different in your case).
        Dim connParams As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Newsletter.mdb;"

        ' Connect to the database and populate mail merge job to-do list with
        ' the data from "mailing_list" table.
        Dim conn As OleDbConnection
        Try
            conn = New OleDbConnection(connParams)

            ' Open the connection and get the data.
            Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM mailing_list", conn)
            conn.Open()
            Dim table As DataTable = New DataTable
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter
            adapter.SelectCommand = command
            adapter.Fill(table)

            ' Create a job which is the following task for MailBee: perform mail merge
            ' of a newsletter template with data rows of the specified data table and
            ' send out each resulting e-mail to its intended recipients.
            mailer.AddJob(Nothing, Nothing, Nothing, table)
        Finally
            If Not conn Is Nothing Then
                conn.Close()
            End If
        End Try

        ' Start processing jobs. The actual mail merge takes place here.
        mailer.BeginSendJobs(New AsyncCallback(AddressOf SendJobsCallback), mailer)

        ' Can do anything else while the mail merge takes place in the background.
        While Not finished
            Thread.Sleep(1000)
        End While

        Console.WriteLine("Processing jobs done.")
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace | SendJobs | SendMailMerge