SmtpSendJobs Method |
Namespace: MailBee.SmtpMail
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
This method takes JobsPending collection as an input and processes all the jobs in it. For jobs consisting of sending a single e-mail message, this e-mail gets sent. For mail merge jobs, mail merge is performed so that the original mail merge job is split into smaller jobs (for mail merge of N data rows, N jobs generated) and the resulting e-mail messages are sent.
To place mail merge job into the queue, use AddJob(string, string, EmailAddressCollection, DataTable) or AddJob(string, string, EmailAddressCollection, IDataReader) overloads. To queue sending of a single e-mail message, use AddJob(String, String, EmailAddressCollection) overload.
To sign each outgoing e-mail with DomainKeys/DKIM signature, subscribe to SendingMessage event and call e.MailMessage.DomainKeysSign in the event handler (assuming e is SmtpSendingMessageEventArgs parameter of the event handler).
While SendJobs method is running, it takes the jobs one by one from JobsPending collection into JobsRunning. Once certain job completes successfully, it's moved into JobsSuccessful. If, however, it failed, it's moved into JobsFailed. For more information on how jobs are processed, see AddJob(String, String, EmailAddressCollection, DataTable, Object, Boolean, Boolean) overload and SendMailMerge(String, EmailAddressCollection, DataTable) method topics.
RetryFailedJobs method can be used to put failed jobs back into the pending queue (except of the jobs created from IDataReader). This can be used to try to re-send failed messages again.
By default, jobs are processed in a single-thread mode. The developer may achieve better performance enabling multi-thread mode (see MaxThreadCount property for details).
MaxThreadCount must be set BEFORE SendJobs gets called.
If large amount of e-mails is sent to an SMTP relay server, it might be efficient to tune some properties of SmtpServer object representing this server in SmtpServers collection. For instance, tuning MaxConnectionCount, MaxSendPerSessionCount, and PauseInterval property values can improve sending performance or overcome issues caused by various restictions implied by the SMTP server (such as limitation of maximum number of e-mails per second from a single IP address).
If you need to submit e-mails directly to the pickup folder for subsequent delivery with MailBee.NET Queue or IIS SMTP service, use SubmitJobsToPickupFolder(String, Boolean) method instead.
Note |
---|
SendJobs will never stop on failures and always succeed if StopJobsOnError is false. |
using System; using System.Data; using System.Data.OleDb; using MailBee; using MailBee.Mime; using MailBee.SmtpMail; class Sample { // Reports successful attempt of sending mail merge e-mail. static void mailer_MessageSent(object sender, SmtpMessageSentEventArgs e) { // Ignore notification e-mail (it's not mail merge). if (e.MergeTable != null) { // Display e-mail address of the successful e-mail. Console.WriteLine(e.MergeTable.Rows[e.MergeRowIndex]["Email"] + " SUCCEEDED"); } } // Reports failed attempt of sending mail merge e-mail. static void mailer_MessageNotSent(object sender, SmtpMessageNotSentEventArgs e) { // Ignore notification e-mail (it's not mail merge). if (e.MergeTable != null) { // Display e-mail address of the failed e-mail. Console.WriteLine(e.MergeTable.Rows[e.MergeRowIndex]["Email"] + " FAILED"); } } 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; // 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", "secret"); // Setup e-mail message header template for mail merge. mailer.Message.From.AsString = "John Doe <john.doe@domain.com>"; mailer.Message.To.AsString = "##Email##"; mailer.Message.Subject = "Our Jan/2007 newsletter"; // Setup plain-text body template. mailer.Message.BodyPlainText = "##Body##"; // 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(); OleDbDataAdapter adapter = new OleDbDataAdapter(); DataTable table = new DataTable(); adapter.SelectCommand = command; adapter.Fill(table); // Create a job which is the following task for MailBee: perform mail merge over // the specified data table and then send out each resulting e-mail to // the recipients which appear in the resulting messages. "bounce@domain.com" // address will be used as Return-Path (i.e. sender e-mail address). mailer.AddJob("1", "bounce@domain.com", null, table); } // Create a job which is a task of sending a notification e-mail to the originator // of the newsletter regarding completion of bulk mailing. mailer.Message.To.AsString = "John Doe <john.doe@domain.com>"; mailer.Message.Subject = "Status: Our Jan/2007 newsletter"; mailer.Message.BodyPlainText = "Newsletter sent out"; mailer.AddJob("2", null, null); // In case of timeouts, uncomment the next line. ESMTP CHUNKING sometimes causes problems (usually, with MS Exchange). // mailer.SmtpServers[0].SmtpOptions = ExtendedSmtpOptions.NoChunking; // Run both jobs. The actual mail merge (job #1) and // sending a single e-mail (job #2) takes place here. mailer.SendJobs(); Console.WriteLine(); // Report results to the console. if (mailer.JobsFailed.Count == 0) { Console.WriteLine("All e-mails have been sent."); } else { if (mailer.JobsSuccessful.Count == 0) { Console.WriteLine("No e-mails have been sent."); } else { Console.WriteLine("Some e-mails have not been sent."); Console.WriteLine(); // Report mail merge results (job #1). Console.WriteLine("Successful mail merge data rows: "); Console.WriteLine(mailer.JobsSuccessful.GetIndicesAsString(null, "1")); Console.WriteLine(); Console.WriteLine("Failed mail merge data rows: "); Console.WriteLine(mailer.JobsFailed.GetIndicesAsString(null, "1")); Console.WriteLine(); // Report notification e-mail results (job #2). if (mailer.JobsFailed.GetIndicesAsString(null, "2") == string.Empty) { Console.WriteLine("Notification e-mail has been sent."); } else { Console.WriteLine("Notification e-mail has not been sent."); } } } } }