SubEtha SMTP

repository·master·Indexed 41 days ago

https://github.com/davidmoten/subethasmtp

A lightweight Java library that allows applications to act as an SMTP server to receive mail. It is RFC 2821 compliant and supports STARTTLS, SMTP AUTH, and SMTP over SSL/TLS. The library provides a builder-based API for configuring the SMTPServer, including options for port binding, connection timeouts, and message size limits. It offers both a low-level MessageHandler API and high-level adapters like SimpleMessageListenerAdapter for simplified email processing.

Tokens
4.3K
Snippets
12
Records
20
Agent score
46%

What's inside SubEtha SMTP

  1. SubEtha SMTP Features

    master

    SubEtha SMTP is a Java library designed for receiving SMTP mail with a simple API. Key features include:

    • RFC 2821 Compliance: Supports the minimum SMTP specification.
    • Security: Supports STARTTLS, SMTP AUTH, and SMTP over SSL/TLS (via server socket factories).
    • Developer Experience: Uses builders for a concise and discoverable API.
  2. Understand SubEthaSMTP threading model

    master

    SubEthaSMTP is a multithreaded server. Each incoming SMTP connection is serviced by a new thread.

    Note: You can modify settings related to the number of simultaneous connections on a stopped server, but these settings cannot be changed after start() has been called.

  3. Install SubEtha SMTP via Maven

    master

    Add the following dependency to your pom.xml to use SubEtha SMTP in your Java project. Replace VERSION_HERE with the desired version.

    <dependency>
        <groupId>com.github.davidmoten</groupId>
        <artifactId>subethasmtp</artifactId>
        <version>VERSION_HERE</version>
    </dependency>
  4. Add SubEtha SMTP dependency via Maven

    master

    To use SubEtha SMTP in your Java project, add the following dependency to your pom.xml file. The library is published to the Maven Central Repository, which is active by default in Maven and requires no additional configuration. Ensure you replace VERSION_HERE with the specific version you wish to use (e.g., 7.2.2).

    <dependency>
      <groupId>com.github.davidmoten</groupId>
      <artifactId>subethasmtp</artifactId>
      <version>VERSION_HERE</version>
    </dependency>
  5. Set up the project for development in Eclipse

    master

    To develop on SubethaSMTP using Eclipse, follow these steps:

    1. Clone the repository from GitHub to your local machine.
    2. In Eclipse, select Import -> Maven -> Existing Maven Projects.
    3. Right-click the POM.XML file and select Run As -> Maven Install to build the project.
  6. Enable logging via SLF4J

    master

    SubethaSMTP uses the SLF4J logging framework. By default, if no compatible logging connector is found in the classpath, the program will print a warning and discard all log messages. To enable logging, you must include a compatible SLF4J connector in your classpath. The Simple connector is a lightweight option that outputs log messages to stderr.

    Note: The version of the connector must match the version that SubethaSMTP was built against.

  7. Run a demo SubethaSMTP Server

    master

    To run a demonstration SMTP server, download the latest jar-with-dependencies.jar from the GitHub releases section. Execute the BasicSMTPServer example class using the java -cp command. Once running, you can test the server using TELNET or any SMTP client by connecting to TCP port 25000.

    java -cp subethasmtp-j3.1.8-jar-with-dependencies.jar org.subethamail.examples.BasicSMTPServer
  8. Handle incoming SMTP messages

    master

    To process received emails, use the .messageHandler() method in the SMTPServer builder. The handler provides the context, the sender (from), the recipient (to), and the message content as a byte array (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();
  9. Start a basic SMTP server

    master

    You can start a simple SMTP server on a specific port using the SMTPServer builder. Calling .start() begins the server execution asynchronously.

    SMTPServer server = SMTPServer 
      .port(25000) 
      .build();
      
    //start server asynchronously
    server.start();