SmtpThread::SMTPConnectionPool Class Reference

#include <SMTPConnectionPool.h>

List of all members.

Public Member Functions

 SMTPConnectionPool ()
 SMTPConnectionPool (unsigned long s)
 SMTPConnectionPool (unsigned long s, unsigned long min, unsigned long max)
 ~SMTPConnectionPool ()
int sendMail (string &from, string &to, string &msg)
void setPoolSize (unsigned long)
unsigned long getPoolSize ()
void setSMTPHost (string host)
void setSMTPPort (int port)
void setMyHostname (string myname)
void setOptimizePool (bool)
void setMinPoolSize (long)
void setMaxPoolSize (long)

Detailed Description


The SMTPConnectionPool sets up an amount of SMTPConnections. These connections are threaded objects and run each in a seperate thread. To send a message, just utilize the sendMail-method. The SMTPConnectionPool then tries to find a ready connection and delegates the message to this connection.

Typical usage:

 #include <iostream>
 #include <sstream>
 #include <stdlib.h>
 #include <string>
 #include <stdio.h>
 #include <stdlib.h>
 #include <smtpthread/SMTPConnectionPool.h>

 using namespace std;
 using namespace SmtpThread;

 int main(int argc, char** argv) {

     string ip = "";      // SMTP-Server
     string host = "localhost";    // for HELO-command
     string to = "you@localhost";  // sender-email
     string from = "me@localhost"; // recipient-email
     string port = "25";           // SMTP-port

     using namespace std;

     mailbody  = "From: \"me\"\n";
     mailbody += "To: \"you\"\n";
     mailbody += "Subject: This is a test\n";
     mailbody += "\n";
     mailbody += "This is the first line of the body.\n";
     mailbody += "This is the second line of the body.\n";

     SMTPConnectionPool pool;

     pool.setMyHostname( host );
     pool.setSMTPHost( ip );
     pool.setSMTPPort( portNumber );
     pool.setMaxPoolSize( 100 );
     pool.setMinPoolSize( 1 );
     pool.setPoolSize( 10 );
     pool.setOptimizePool( true );

     int success = 0;
     int error   = 0;
     int result  = 0;

     for( int idx = 0; idx < 1000; idx++ )
         result = pool.sendMail( from, to, mailbody  );
         if( result == 0 ) ++success;
         else ++error;

     cout << "Success: " << success << endl;
     cout << "Errors:  " << error   << endl;


Constructor & Destructor Documentation

SmtpThread::SMTPConnectionPool::SMTPConnectionPool (  ) 

Default Constructor.

SmtpThread::SMTPConnectionPool::SMTPConnectionPool ( unsigned long  s  ) 

Constructor. Initializes a connection-pool with a pool-size of s connections.

s pool-size.

SmtpThread::SMTPConnectionPool::SMTPConnectionPool ( unsigned long  s,
unsigned long  min,
unsigned long  max 

Constructor. Initializes a connection-pool with a pool-size of s connections, a minimum of min and a maximum of max connection.

s pool-size.

SmtpThread::SMTPConnectionPool::~SMTPConnectionPool (  ) 

Default Destructor.

Member Function Documentation

int SmtpThread::SMTPConnectionPool::sendMail ( string &  from,
string &  to,
string &  msg 

Send an email to the connection-pool.

from sender-email, used in the SMTP-dialog.
to receiver-email, used in the SMTP-dialog.
msg email-body to send, including additional headers.

void SmtpThread::SMTPConnectionPool::setPoolSize ( unsigned  long  ) 

Sets the pool-size. The pool-size is the number of SMTP-connectons, which are connected to the SMTP-Server.

poolSize number of SMTP-connections.

unsigned long SmtpThread::SMTPConnectionPool::getPoolSize (  ) 

Returns the pool-size.


void SmtpThread::SMTPConnectionPool::setSMTPHost ( string  host  ) 

Sets the IP-address of the SMTP-host.

NOTE: host must be an IP-address and not a domain-name.

host IP-address of the SMTP-server.

void SmtpThread::SMTPConnectionPool::setSMTPPort ( int  port  ) 

Sets the port-number to connect to. Defaults to 25.

int port port-number of the remote SMTP-server.

void SmtpThread::SMTPConnectionPool::setMyHostname ( string  myname  ) 

Sets the hostname, which is send in the HELO-commend.

myname hostname of the mail-sending-host.

void SmtpThread::SMTPConnectionPool::setOptimizePool ( bool   ) 

Tells the pool wether or not to optimize the pool-size.

Optimizing tries to find a pool-size, which has enough connections so that a new sendmail-command does not have to wait for previous connection to finish.

The optimal pool-size is calculated and set, if within the upper and lower limits set with "setMinPoolSize" and "setMaxPoolSize".

Please remember, that changing the pool-size also affects allocating or deallocating of new/existing objects. This results in small performanc-impacts. The bast way is to already know the optimal pool-size, initialize the pool with that size and disable optimizing the pool.

NOTE: Optimizing the pool-size is not a trivial task. The algorithm used in this implementation, is a rather simple approach. Works for me, but might not be smart enough for you.

opt true / false = optimize / do not optimize the pool-size.
See also:


void SmtpThread::SMTPConnectionPool::setMinPoolSize ( long   ) 

Sets the minimum size of the connection-pool. Takes effect only, when pool-optimization is active.

minimum minimal pool-size.

void SmtpThread::SMTPConnectionPool::setMaxPoolSize ( long   ) 

Sets the maximum size of the connection-pool. Takes effect only, when pool-optimization is active.

maximum maximal pool-size.

Generated on Sun Dec 2 13:20:05 2007 for libSmtpThread by  doxygen 1.5.1