Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.nithril:smtp-connection-pool:2.0.0'
}
dependencies {
implementation("com.github.nithril:smtp-connection-pool:2.0.0")
}
<dependency>
<groupId>com.github.nithril</groupId>
<artifactId>smtp-connection-pool</artifactId>
<version>2.0.0</version>
</dependency>
libraryDependencies += "com.github.nithril" % "smtp-connection-pool" % "2.0.0"
:dependencies [[com.github.nithril/smtp-connection-pool "2.0.0"]]
This library implements a SMTP connection pool using Jakarta Mail formerly known as 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:
Java 1.8
Search for the latest version on Maven central:
eg.:
<dependency>
<groupId>com.github.nithril</groupId>
<artifactId>smtp-connection-pool</artifactId>
<version>1.4.0</version>
</dependency>
The SmtpConnectionPool
creates a JavaMail Transport
using a SmtpConnectionFactory
.
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...)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());
mimeMessage.addRecipients(Message.RecipientType.TO, to);
mimeMessage.setFrom("from@example.com");
mimeMessage.setSubject("Hi!");
mimeMessage.setText("Hello World!");
transport.sendMessage(mimeMessage);
}
//Close the pool, usually when the application shutdown
smtpConnectionPool.close();
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);