Skip to content

tstavinoha/smtp-connection-pool

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SMTP Connection Pool

This library implements a SMTP connection pool using Java Mail for the SMTP code and the Apache Commons Pool for the pool code.

The pool, thanks to the Apache library, supports most common pool features:

  • Max total
  • Min idle
  • Eviction
  • Test on borrow
  • ...

Requirements

Java 1.7

Maven dependency

Maven configuration:

<dependency>
    <groupId>com.github.nithril</groupId>
    <artifactId>smtp-connection-pool</artifactId>
    <version>1.2.1</version>
</dependency>

How to use the connection pool?

The SmtpConnectionPool creates a JavaMail Transport using a SmtpConnectionFactory.

Smtp Connection Factory

The SmtpConnectionFactory can be created using different ways.

If you already have a configured Session

SmtpConnectionFactory factory = SmtpConnectionFactories.newSmtpFactory(aSession);

JavaMail will retrieve the protocol, host, username... from the Session.

You can build the factory using a builder

SmtpConnectionFactory factory = SmtpConnectionFactoryBuilder.newSmtpBuilder()
                .session(aSession)
                .protocol("smtp") 
                .host("mailer")
                .port(2525)
                .username("foo")
                .password("bar").build();

All builder parameters are optionals. JavaMail will fallback to the default configuration (smtp, port 25...)

You can instanciate directly the factory

new SmtpConnectionFactory(aSession, aTransportStrategy, aConnectionStrategy);

Where:

  • TransportStrategy allows to configure how the transport is got (default, protocol, url, provider)
  • ConnectionStrategy allows to configure how to connect (default, username/password...)

Smtp Connection Pool

Java code:

//Declare the factory and the connection pool, usually at the application startup
SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(SmtpConnectionFactoryBuilder.newSmtpBuilder().build());

//borrow an object in a try-with-resource statement or call `close` by yourself
try (ClosableSmtpConnection transport = smtpConnectionPool.borrowObject()) {
    MimeMessage mimeMessage = new MimeMessage(transport.getSession());
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false);
    mimeMessageHelper.addTo("[email protected]");
    mimeMessageHelper.setFrom("[email protected]");
    mimeMessageHelper.setSubject("Hi!");
    mimeMessageHelper.setText("Hello World!", false);
    transport.sendMessage(mimeMessage);
}

//Close the pool, usually when the application shutdown
smtpConnectionPool.close();

How to configure the pool?

Configuration is held by the Pool code, see the Commons Pool Javadoc.

Example:

//Create the configuration
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(2);

//Declare the factory and the connection pool, usually at application startup
SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(SmtpConnectionFactoryBuilder.newSmtpBuilder().build(), config);

About

SMTP Connection Pool

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%