JavaMail
The JavaMail add-on integrates JavaMail API (JSR 919) with SeedStack.
Dependency #
<dependency>
<groupId>org.seedstack.addons.javamail</groupId>
<artifactId>javamail</artifactId>
</dependency>
Show version
dependencies {
compile("org.seedstack.addons.javamail:javamail:3.0.0")
}
You also need to add the JavaMail API itself:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.8</version>
</dependency>
dependencies {
compile("javax.mail:mail:1.4.8")
}
Note that you need to specify the scope as provided
if your runtime environment already provides the JavaMail API
(such as JEE servers).
Configuration #
Configuration is done by declaring one or more mail providers:
javamail: # Configured mail providers with the name of the cache as key providers: provider1: # Providers are fully configured with JavaMail session properties property1: value1
To dump the javamail
configuration options:
mvn -q -Dargs="javamail" seedstack:config
Note that providers are fully configured by specifying JavaMail session properties.
SMTP #
To configure an SMTP provider, use the following configuration:
javamail: providers: smtpProvider: mail.transport.protocol: smtp mail.smtp.host: ... mail.smtp.port: ...
IMAP #
To configure an IMAP provider, use the following configuration:
javamail: providers: imapProvider: mail.store.protocol: imap mail.imap.user: ... mail.imap.host: ... mail.imap.port: ...
POP3 #
To configure a POP3 provider, use the following configuration:
javamail: providers: imapProvider: mail.store.protocol: pop3 mail.pop3.user: ... mail.pop3.host: ... mail.pop3.port: ...
Usage #
The configured providers will be injectable as a fully-configured JavaMail session:
public class SomeClass { @Inject @Named("provider1") private Session session; }
Use the JavaMail API documentation to know more about usage.
Testing #
JavaMail add-on provides testing fixtures which enable to emulate an SMTP server and easily assert that your sent mails are valid. Consider the following test configuration:
javamail: providers: smtpTest: mail.transport.protocol: smtp mail.smtp.host: localhost mail.smtp.port: 6457
You can then use the @WithMailServer
annotation and the
MessageRetriever
class in your tests:
@WithMailServer(host = "localhost", port = 6457) public class SmtpSendingIT extends AbstractSeedIT { @Inject @Named("smtpTest") private Session smtpSession; @Inject private MessageRetriever retriever; @Test public void testSend() throws MessagingException { Transport transport = null; try { Message message = new MimeMessage(smtpSession); message.setRecipient(Message.RecipientType.TO, new InternetAddress("...")); message.setFrom(new InternetAddress("...")); message.setSubject("..."); message.setText("..."); message.setSentDate(new Date()); transport = smtpSession.getTransport(); transport.connect(); transport.sendMessage(message, message.getAllRecipients()); } finally { if (transport != null) { transport.close(); } } for (Message message : retriever.getSentMessages()) { MockMailServerAssertions.assertThat(message) .hasRecipients(Message.RecipientType.TO); MockMailServerAssertions.assertThat(message) .recipientEqualsTo(Message.RecipientType.TO, InternetAddress.parse(TestConstantsValues.DEFAULT_RECIPIENT)); } } }