davidmoten / subethasmtp

SubEtha SMTP is a Java library for receiving SMTP mail
Other
151 stars 40 forks source link

subethasmtp


Maven Central
codecov

SubEtha SMTP is a Java library which allows your application to receive SMTP mail with a simple, easy-to-understand API.

This component can be used in almost any kind of email processing application. Hypothetical (and not-so hypothetical) uses include:

Features

Example

The code below starts an SMTP server and logs messages to the console:

SMTPServer server = SMTPServer 
  .port(25000) 
  .build();

 //start server asynchronously
 server.start();

Do your own thing with a message (from, to and a byte array of data):

SMTPServer server = SMTPServer //
  .port(PORT) //
  .messageHandler(
    (ctx, from, to, data) -> 
       System.out.println(
           "message from " + from 
           + " to " + to
           + ":\n" + new String(data, StandardCharsets.UTF_8)))
  .build();

server.start();

The builder has a lot of options. This fragment sets a bunch of them:

SMTPServer server = SMTPServer 
  .port(port) 
  .connectionTimeout(1, TimeUnit.MINUTES) 
  .authenticationHandlerFactory(ahf) 
  .backlog(100) 
  .bindAddress(address) 
  .requireTLS() 
  .hideTLS() 
  .hostName("us") 
  .maxMessageSize(10000)
  .maxConnections(20)
  .maxRecipients(20) 
  .messageHandlerFactory(mhf) 
  .executorService(executor)
  .startTlsSocketFactory(sslContext)
  .fromAddressValidator(emailValidator)
  .build();

Getting started

Use this maven dependency:

<dependency>
    <groupId>com.github.davidmoten</groupId>
    <artifactId>subethasmtp</artifactId>
    <version>VERSION_HERE</version>
</dependency>

A Little History

SubEthaSMTP was split out of the SubEthaMail mailing list manager because it is a useful standalone component. When we wrote SubEtha, the last thing we wanted to do was write our own SMTP server. In our search for a modular Java SMTP component, we examined:

Since you're reading this page you probably already know what we found: Seven different SMTP implementations without the slightest thought given to reusability. Even Jstmpd, which purports to be a "A Modular Java SMTP Daemon", isn't. Even though JBoss Mail/Meldware Mail is in active development, the team was unintersted in componentization of the SMTP processing portion of their server. GreenMail, which is based on the JAMES code base is best summarized with this blog posting.

During the development of SubEtha's testing harness, we tried out the Dumbster software and found that not only was the API difficult to use, it did it not work properly, the developer has not done any development on it in about a year and it does not work reliably on Mac OS X. With two simple classes we re-implemented it as an included project called Wiser.

We hate reinventing wheels. This should be the LAST FREAKING JAVA SMTP IMPLEMENTATION (Dave Moten: not including forks!).

A New Fork

Engine821.com too did a survey of existing Java SMTP implementations and were unsatisfied... until they found SubEthaSMTP! The code is clean and very well thought out. The changes they made were minor, including...

Fork of a Fork!

Dave Moten came across the Engine821 fork and

Project Authors

Ian McFarland contributed the first codebase to SubEtha Mail. Then, Jon Stevens and Jeff Schnitzer re-wrote most of Ian's code into what we have today. Edouard De Oliveira and Scott Hernandez have also made significant contributions. Dave Moten made changes as mentioned above.

Support

If you have any bug reports, questions or comments about this SubEtha SMTP fork, it's best that you use the GitHub issue tracker to get in touch. Please do not email the authors directly.

Spec Compliance

For now, we have just focused on implementing just the minimal required aspects of http://rfc.net/rfc2821.html#s4.5.1. We also return SMTP status responses that mimic what Postfix returns.

Thanks to a contribution from Mike Wildpaner, we support the StartTLS specification.

Thanks to a contribution from Marco Trevisan, we support the SMTP AUTH specification.