SmtpMaxThreadCount Property
Gets or sets the maximum number of threads MailBee may create during send mail operations.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public int MaxThreadCount { get; set; }

Property Value

Type: Int32
An integer value specifying the maximum number of threads MailBee may create during send mail operations, or -1 which specifies the number of threads is not limited. The default value is 1 (i.e. multi-threading is off).
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionvalue is zero.
Remarks
Multi-threading may greatly increase performance of send bulk mail (mail merge) and direct send operations (sending directly to recipients' SMTP servers through MX lookup). For instance, if the message is addressed to 5 recipients on the different domains (and therefore 5 SMTP connections must be made), the component can submit the message to all 5 SMTP servers simultaneously.
Caution note 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 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.
Examples

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();
    }
}
See Also