SmtpBeginSendJobs Method |
Note: This API is now obsolete.
Namespace: MailBee.SmtpMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use SendJobsAsync instead.")] public IAsyncResult BeginSendJobs( AsyncCallback callback, Object state )
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:\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 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:\Temp\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."); } }