SmtpAddJob Method (String, String, EmailAddressCollection, DataTable) |
Namespace: MailBee.SmtpMail
public void AddJob( string tag, string senderEmailPattern, EmailAddressCollection recipientsPattern, DataTable mergeTable )
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | mergeTable is a null reference (Nothing in Visual Basic). |
This method allows the application to schedule mail merge over database for subsequent processing with SendJobs, BeginSendJobs(AsyncCallback, Object), or SubmitJobsToPickupFolder(String, Boolean) method.
Note |
---|
To perform mail merge immediately rather than schedule it, use SendMailMerge(String, EmailAddressCollection, DataTable) method. |
Note |
---|
This method is not available in .NET Core and UWP editions. |
The mail merge requirements:
Calling this overload is equivalent to calling AddJob(String, String, EmailAddressCollection, DataTable, Object, Boolean, Boolean) with mergeRowIndices set to null, keepProducedJobs set to true, and keepMergedData set to false.
using System; using System.Data; using System.Data.OleDb; using MailBee; using MailBee.Mime; using MailBee.SmtpMail; class Sample { // Occurs when an e-mail message is ready to be sent. static void mailer_SendingMessage(object sender, SmtpSendingMessageEventArgs e) { // Display e-mail address of the e-mail to be sent. Console.WriteLine(e.MergeTable.Rows[e.MergeRowIndex]["Email"] + " of '" + e.Tag + "'"); // You can also subscribe to events like MergingMessage, MessageSent, MessageNotSent // to keep the detailed track of mail merge progress. } 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 event to track send bulk mail progress. mailer.SendingMessage += new SmtpSendingMessageEventHandler(mailer_SendingMessage); // 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 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 = "##ID##-N"; // Setup HTML body template. mailer.Message.BodyHtmlText = "<html>##Body##</html>"; // Setup template for adding file attachments upon the specified path. // In this sample, the path to attachment files will be constructed as // "C:\" + DatabaseRecordField("Doc_path"). mailer.Message.Merge.AddAttachmentPattern(@"C:\##Doc_path##"); // Make outgoing e-mails UTF-8 to allow content in any language. mailer.Message.Charset = "UTF-8"; // Tell MailBee to generate alternative plain-text version // of each e-mail automatically. mailer.Message.Builder.HtmlToPlainMode = HtmlToPlainAutoConvert.IfHtml; // 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;"; DataTable table = new DataTable(); // 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(); 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("Newsletter #1", null, null, table); // Change template parameters for another newsletter. mailer.Message.Subject = "Newsletter update"; // 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 = "##ID##-NU"; // Setup HTML body template. mailer.Message.BodyHtmlText = "<html>Please disregard the newsletter you just received.</html>"; // If we do not want attachments in second newsletter, remove the pattern. mailer.Message.Merge.ClearAttachmentPatterns(); // Create a job of sending another newsletter to the same recipients. mailer.AddJob("Newsletter #2", null, null, table); } // Run the jobs. 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("Both newsletters have been sent out without any errors."); } else { if (mailer.JobsSuccessful.Count == 0) { Console.WriteLine("All newsletter e-mails failed to be sent."); } else { Console.WriteLine("Not all newsletter e-mails have been sent."); Console.WriteLine(); Console.WriteLine("Successful rows (Our newsletter): "); Console.WriteLine(mailer.JobsSuccessful.GetIndicesAsString(table, "Newsletter #1")); Console.WriteLine(); Console.WriteLine("Failed rows (Our newsletter): "); Console.WriteLine(mailer.JobsFailed.GetIndicesAsString(table, "Newsletter #1")); Console.WriteLine(); Console.WriteLine("Successful rows (Newsletter update): "); Console.WriteLine(mailer.JobsSuccessful.GetIndicesAsString(table, "Newsletter #2")); Console.WriteLine(); Console.WriteLine("Failed rows (Newsletter update): "); Console.WriteLine(mailer.JobsFailed.GetIndicesAsString(table, "Newsletter #2")); } } } }