SmtpAddJob Method (String, String, EmailAddressCollection, IDataReader)
Puts a "mail merge over database" job onto waiting list for subsequent processing in bulk mode.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public void AddJob(
	string tag,
	string senderEmailPattern,
	EmailAddressCollection recipientsPattern,
	IDataReader mergeDataReader
)

Parameters

tag
Type: SystemString
Any string the developer wants to assign to Tag property of SendMailJob object created by this method. The developer can leave it a null reference (Nothing in Visual Basic).
senderEmailPattern
Type: SystemString
The e-mail address template of the sender. If it's a null reference (Nothing in Visual Basic), the e-mail address template will be taken from From property.
recipientsPattern
Type: MailBee.MimeEmailAddressCollection
The e-mail address template of the recipients list. If it's a null reference (Nothing in Visual Basic), the recipients list will be constructed via merge of To, Cc, and Bcc patterns with actual values from the data source.
mergeDataReader
Type: System.DataIDataReader
The data source for mail merge, such as SqlDataReader.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionmergeDataReader is a null reference (Nothing in Visual Basic).
Remarks
This overload is similar to AddJob(String, String, EmailAddressCollection, DataTable).
Note Note
This method is not available in .NET Core and UWP editions.
Examples
using System;
using System.Data;
using System.Data.SqlClient;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;

class Sample
{
    // Reports successful attempt of sending e-mail.
    static void mailer_MessageSent(object sender, SmtpMessageSentEventArgs e)
    {
        // Display e-mail address of the successful e-mail.
        // We assume field #0 is ID, field #1 is Email, etc.
        string email = e.MergeDataReaderRowValues[1].ToString();
        Console.WriteLine(string.Format("{0} SUCCEEDED", email));
    }

    // Reports failed attempt of sending e-mail.
    static void mailer_MessageNotSent(object sender, SmtpMessageNotSentEventArgs e)
    {
        // Display e-mail address of the failed e-mail.
        // We assume field #0 is ID, field #1 is Email, etc.
        string email = e.MergeDataReaderRowValues[1].ToString();
        Console.WriteLine(string.Format("{0} FAILED", email));
    }

    static void Main(string[] args)
    {
        // By the way, you can pass the license key directly in the constructor.
        Smtp mailer = new Smtp("MN100-0123456789ABCDEF-0123");

        // Logging into a file is useful for troubleshooting.
        mailer.Log.Filename = @"C:\Temp\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 will support so many incoming connections (each thread creates its own).

        // mailer.MaxThreadCount = -1;

        // Subscribe to events to track send bulk mail progress.
        mailer.MessageSent += new SmtpMessageSentEventHandler(mailer_MessageSent);
        mailer.MessageNotSent += new SmtpMessageNotSentEventHandler(mailer_MessageNotSent);

        // Setup SMTP server parameters.
        mailer.SmtpServers.Add("mail.domain.com", "jdoe@domain.com", "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 Oct/2013 newsletter";

        // Setup DSN template for mail merge. In particular, this can be useful
        // to track bounced messages which may come back from some addresses after
        // sending bulk mail out. If the SMTP server does not support DSN, this
        // setting will be ignored.
        mailer.DeliveryNotification.TrackingID = "Oct2013_##ID##";

        // Setup HTML body template.
        mailer.Message.BodyHtmlText = "##HTML_body##";

        // For a change, setup plain-text body template. In other samples, we set HTML body only
        // and let MailBee create plain-text version.
        mailer.Message.BodyPlainText = "##Plain_body##";

        // Make outgoing e-mails UTF-8 to allow content in any language.
        mailer.Message.Charset = "UTF-8";

        // The app will take care of plain-text version. MailBee shouldn't do it.
        mailer.Message.Builder.HtmlToPlainMode = HtmlToPlainAutoConvert.Never;

        // Specify database connection string (it may be different in your case).
        string connParams = @"server=(local)\SQLEXPRESS;Initial Catalog=afterlogic;Integrated Security=True";

        using (SqlConnection conn = new SqlConnection(connParams))
        {
            conn.Open();
            using (SqlCommand command = conn.CreateCommand())
            {
                // The 'newsletter' table or query must have these fields:
                // #0: ID (int)
                // #1: Email (nvarchar)
                // #2: Name (nvarchar)
                // #3: Plain_body (nvarchar)
                // #4; HTML_body (nvarchar)
                // Table row example: 15, "jdoe@domain.com", "John", "Some text", "<html>Some&nbsp;text</html>".
                command.CommandText = "SELECT * FROM newsletter";

                // Populate mail merge job to-do list with from the specified table.
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    // Submit the job to MailBee.
                    mailer.AddJob(null, null, null, reader);

                    // Run the job. The actual mail merge takes place here.
                    mailer.SendJobs();

                    Console.WriteLine();

                    // Report results (row indices in the data table) to the console.
                    if (mailer.JobsFailed.Count == 0)
                    {
                        Console.WriteLine(
                            "All of the rows of the table have been processed and sent as e-mails.");
                    }
                    else
                    {
                        if (mailer.JobsSuccessful.Count == 0)
                        {
                            Console.WriteLine(
                                "None of the rows of the table has been processed and sent as e-mail.");
                        }
                        else
                        {
                            Console.WriteLine(
                                "Not all rows of the table have been processed and sent as e-mails.");
                            Console.WriteLine();

                            Console.WriteLine("Successful rows count: " +
                                mailer.JobsSuccessful.Count.ToString());
                            Console.WriteLine();

                            Console.WriteLine("Failed rows count: " +
                                mailer.JobsFailed.Count.ToString());
                        }
                        Console.WriteLine();

                        mailer.JobsSuccessful.Clear();
                        mailer.JobsFailed.Clear();
                    }
                }
            }
        }
    }
}
See Also