Skip to content

dgroup/mbox4j

Repository files navigation

Maven Javadocs License: MIT Commit activity Hits-of-Code

Build Status 0pdd Dependency Status Known Vulnerabilities

DevOps By Rultor.com EO badge We recommend IntelliJ IDEA

Qulice SQ maintainability Codebeat Codacy Badge Codecov

What it is

mbox4j is an object-oriented primitives to simplify the manipulations with emails for Java-based applications.

Principles

Design principles behind mbox4j.

How to use

Get the latest version here:

<dependency>
    <groupId>io.github.dgroup</groupId>
    <artifactId>mbox4j</artifactId>
    <version>${version}</version>
</dependency>

Java version required: 1.8+.

Interface Purpose Implementations / Related
Inbox Allows to read the emails from the server JavaxMailInbox, InboxEnvelope, UncheckedInbox, FakeInbox, etc
Outbox Allows to send the emails to the target recipients JavaxMailOutbox, OutboxEnvelope, UncheckedOutbox, FakeOutbox, etc
Msg The email message to be read/sent MsgOf, MsgEnvelope, FakeMsg, etc
Query Email search query to the server QueryOf, Mode, ModeOf, All, etc

All examples below are using the following frameworks/libs:

  • Hamcrest - Library of matchers, which can be combined in to create flexible expressions of intent in tests.
  • cactoos - Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons.
  • cactoos-matchers - Object-Oriented Hamcrest matchers

Fetch the emails from the server using javax.mail framework:

public static void main(final String[] args) {
    final Properties smtp = new Properties();
    smtp.setProperty("mail.smtp.host", host);
    smtp.setProperty("mail.smtp.port", port);
    smtp.setProperty("username", user);
    smtp.setProperty("password", password);
    ...
    final Inbox inbox = new JavaxMailInbox(smtp);
    final Iterable<Msg> msgs = inbox.read(
        new Query("pop3s", "INBOX", new All())
    );
    for(final Msg msg : msgs) {
        System.out.println(msg);
    }
}

See Gmail SMTP connection properties as an example of a configuration for reading procedure.

Send an email to the target recipients using javax.mail framework:

public static void main(final String[] args) {
    final Properties smtp = new Properties();
    smtp.setProperty("mail.smtp.host", host);
    smtp.setProperty("mail.smtp.port", port);
    smtp.setProperty("username", user);
    smtp.setProperty("password", password);
    ...
    final Outbox outbox = new JavaxMailOutbox(smtp);
    outbox.send(
        new MsgOf(
            "from@server.com", 
            "to@server.com", 
            "Testing subj", 
            "I'm simple and i know it."
        )
    );
}

See Gmail SMTP connection properties as an example of a configuration for sending procedure.