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:
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();
Use this maven dependency:
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>subethasmtp</artifactId>
<version>VERSION_HERE</version>
</dependency>
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!).
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...
/lib
directory. Maven correctly handles pulling in all the dependencies and best practices discourage keeping binary artifacts inside version control.checked
. This is possibly controversial, but we thought about it a lot and prefer to have these exceptions show up in the throws
clause rather than have them potentially pop-up unexpectedly at run-time. Dave Moten came across the Engine821 fork and
com.github.davidmoten:subethasmtp
artifact Base64
class at the moment, Java 7 required now because of use in unit test of X509TrustManager
)SMTPServer
to be largely immutable and is created with a builder patternWiser
API to cope with an immutable SMTPServer
SMTPServer
(now final)Wiser
now created with builder pattern (disallowed inheritance and added accepter
builder method)SmartClient
SmartClient
so that references don't escape the constructor (connect
was called from the constructor)InputStreamReader
(US_ASCII) in SMTPClient
and SmartClient
java.util.Optional
and Preconditions
in SmartClient
, SMTPClient
and SMTPServer
@Override
annotationsEmailUtils
testsSMTPServer
builder and a roundtrip unit testBDAT
supportPROXY protocol
, built on the HAProxy specification found at https://www.haproxy.org/download/2.3/doc/proxy-protocol.txtIan 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.
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.
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.