Connection Pool Refactored

With the release of Aspose.Email 19.3, the connection pool has been refactored. New EmailClient class has been introduced which will eventually replace the CredentialsByHostClient class. The EmailClient class provides a ConnectionAsgmtMode property which defines the mode of connection allocation in a multithreaded environment. EmailClient.ConnectionAsgmtMode is set using the ConnectionAsgmtType enumerable.

Connections Types

There are following three connection types:

  • The main connection: This is the connection created and disposed together with the mail client. It can’t be created or disposed manually.
  • Default connection: User can create default connections for threads with CreateConnection method. If the default connection exists, all methods of email client executed in a thread will be implicitly using this connection. Only one default connection can exist per thread. It can be created manually or automatically. It depends on EmailClient.ConnectionAsgmtMode property. These connections can be created manually with EmailClient.CreateConnection(createAsDefaultConnection = true) method. If the default connection is not used (depends on connection allocation mode), the main connection used implicitly instead of it.
  • Independent connections: These are connections that are not linked to threads. They can be created manually and have to be used explicitly as a method parameter. These connections can be created manually with EmailClient.CreateConnection() method or EmailClient.CreateConnection(createAsDefaultConnection = false) method.

Connection Allocation Types

To set up the EmailClient.ConnectionAsgmtMode property, ConnectionAsgmtType enumerable is used. The allocation types provided by ConnectionAsgmtType are listed below.

  • ConnectionAsgmtType.UseMainOrDefault This mode uses by default in email clients. Email client uses the main connection for all operations from multiple threads if default connection hasn’t been created, or if a connection has not been passed as method parameter explicitly. The main connection is a connection which is created at the same time as an email client. User can create default connections for threads with CreateConnection method. If default connection for a thread is created, it’s used implicitly for all methods of email client which are invoked in this thread. If default connection for a thread is not created, the main connection is used for all methods of email client which are invoked in this thread. The user also can create connections not linked to threads (not default connections) with the CreateConnection method. If the user wants to use other connections (not main and not default) he has to pass this connection explicitly as a parameter of a method which he wants to use. User can additionally create any number of connections. Default connection can be only one per thread. Please note default connections work correctly if the user uses Thread objects for multitasking programming. If the user uses ConnectionPool or uses Task objects for multitasking programming this mode may lead to the wrong behavior of the application. To avoid this problem user has to manually dispose default connection (if he uses it) at the end of the code execution.
  • ConnectionAsgmtType.UseMain Email client uses the main connection for all operations from multiple threads. The main connection is a connection which is created at the same time as the email client. The user can not create default connections. User can create connections not linked to threads (not default connections) with the CreateConnection method. If the user wants to use other connections (not main and not default) he has to pass this connection explicitly as a parameter of a method which he wants to use. 
  • ConnectionAsgmtType.UseDefault Email client uses implicitly only default connections for all operations from multiple threads. The main connection is not used in this mode. If default connection hasn’t been created for some thread (first invocation of email client method), the email client creates default connection implicitly for the thread before the first operation is executed. User can’t create default connections for threads with CreateConnectionmethod because they are created automatically. When default connection for a thread is created, it’s used implicitly for all methods of email client which are invoked in this thread. The user can also create connections that are not linked to threads (not default connections) with the CreateConnectionmethod. If the user wants to use other connections (not main and not default) he has to pass this connection explicitly as a parameter of a method which he wants to use. User can additionally create any number of connections. Only one default connection can be used per thread. Please note default connections work correctly if the user uses Thread objects for multitasking programming. If the user uses ConnectionPool or uses Task objects for multitasking programming this mode may lead to wrong behavior of the application. To avoid this problem user has to manually dispose the default connection at the end of the code execution.