mbox4j is an object-oriented primitives to simplify the manipulations with emails for Java-based applications.
Design principles behind mbox4j.
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.