MailBee.NET Objects 3.1

Smtp.MaxThreadCount Property

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

public int MaxThreadCount {get; set;}

Property Value

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).

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   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.

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.

Exceptions

Exception Type Condition
MailBeeInvalidArgumentException value is zero.

Example

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.

[C#]
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:\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();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.SmtpMail
Imports MailBee.Mime

Class Sample
    ' MessageSubmittedToServer event handler.
    Private Shared Sub OnMessageSubmittedToServer( _
        ByVal sender As Object, ByVal e As SmtpMessageSubmittedToServerEventArgs)
        ' 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.
        For Each address As EmailAddress In e.AcceptedRecipients
            Console.WriteLine("Message sent to " & address.Email)
        Next
    End Sub

    ' The actual code.
    Shared Sub Main()
        Dim mailer As 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:\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.
        AddHandler mailer.MessageSubmittedToServer, AddressOf OnMessageSubmittedToServer

        ' Send it!
        mailer.Send()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace | SendMailMerge