SmtpMaxThreadCount Property |
Namespace: MailBee.SmtpMail
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | value is zero. |
Caution |
---|
Some methods and properties of Smtp component cannot be used in multi-thread mode. For instance, if there are 3 simultaneous SMTP connections opened, it's not possible to determine for which SMTP connection GetServerResponse method should return the response. In multi-thread mode, the developer should use Smtp class events in order to track progress of operations. In multi-thread mode, IsSmtpContext always returns false. |
The maximum number of threads MailBee can utilize is 60. Threads are taken from ThreadPool. In .NET Core and UWP, Tasks are used instead.
Note |
---|
If logging SMTP session into a file is enabled (Enabled property of Log object is true), it's recommended to include context information into the log when multi-thread mode is used. The developer can use AddContextInfo flag for this purpose. The sample code in the bottom of this page demonstrates how this option can be used. If context information was not added, the log file would be more difficult to read and analyze. |
This sample sends message using MX lookup (no SMTP relay server is used), spawning as many threads as required (MaxThreadCount is -1). The message contains the following recipients: user1@domain-a.com, user2@domain-a.com, user1@domain-b.com, user1@domain-c.com. Thus, the message must be sent to SMTP servers of 3 domains: domain-a.com, domain-b.com, domain-c.com Since the maximum number of threads is not limited, the component will create 3 threads in this case.
The MessageSubmittedToServer event is used to track submission of the message to each of 3 domains. If the message is successfully delivered to SMTP servers of all 3 domains, this event will occur 3 times total.
using System; using MailBee; using MailBee.SmtpMail; using MailBee.Mime; class Sample { // MessageSubmittedToServer event handler. private static void OnMessageSubmittedToServer( object sender, SmtpMessageSubmittedToServerEventArgs e) { // Show all recipients which were accepted by SMTP server. This event // is raised for every recipient domain which accepted at least one // recipient. As for the current sample, this event will occur 3 times // if each domain's SMTP server accepted at least one recipient. foreach (EmailAddress address in e.AcceptedRecipients) { Console.WriteLine("Message sent to " + address.Email); } } // The actual code. static void Main(string[] args) { Smtp mailer = new Smtp(); // Enable logging of send mail session into a file. // Context information will be added into the log as well. mailer.Log.Enabled = true; mailer.Log.Filename = @"C:\Temp\log.txt"; mailer.Log.Format = LogFormatOptions.AddContextInfo; mailer.Log.Clear(); // Get DNS servers for MX lookup from the config file // or operating system settings. mailer.DnsServers.Autodetect(); // Set sender. mailer.From.AsString = "John Doe <jdoe@domain.com>"; // Set recipients (we specify multiple recipient domains // to demonstrate effect of multi-threading). mailer.To.AsString = "C.J. Smith <user1@domain-a.com>, " + "\"Kathy Long\" <user2@domain-a.com>, " + "Bob <user1@domain-b.com>, user1@domain-c.com"; // Set message body and subject. mailer.Subject = "Annual report"; mailer.BodyHtmlText = "<html>That's it.</html>"; // Allow MailBee to create as many threads as required // to send mail to all recipient domains simultaneously. mailer.MaxThreadCount = -1; // Subscribe to the MessageSubmittedToServer event. mailer.MessageSubmittedToServer += new SmtpMessageSubmittedToServerEventHandler(OnMessageSubmittedToServer); // Send it! mailer.Send(); } }