FastMail - an easy-to-use library for mail communication for Java and Android. All you need is a SMTP server and to import the library.
This project is deprecated. I recommend switching to JakartaMail.
Gradle:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.alexsgi:fastmail:VERSION'
}
Maven:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.alexsgi</groupId>
<artifactId>fastmail</artifactId>
<version>VERSION</version>
</dependency>
</dependencies>
Talk is cheap ... here is the code to send a mail:
Recommended config :
FastMail.init("smtp.example.com", "username", "password");
Send a mail :
FastMail.sendMail("This is the subject", "This is the content", "to@example.com");
Send mail to multiple people ?
FastMail.sendMail("This is the subject", "This is the content", "to@example.com", "tome@example.com", "andme@example.com"); // ...
Content should be in HTML ?
FastMail.sendMail("This is still the subject", "<h1>This is a Heading</h1> <p style=\"color=red;\">This is a RED paragraph.</p>", true, "hello@example.com");
FastMail.sendMail(String subject, String content, String ... recipients);
and
FastMail.sendMail(String subject, String content, boolean isHtml, String ... recipients);
are static and can be called from everywhere. Don't forget to init!
Sometimes sending a mail fails. Why? Get a list with all exceptions:
FastMail.getExceptionsList();
Last element is the newest exception.
Reset the ArrayList (Log) :
FastMail.resetExceptionList();
Get the initialized host, username and password :
String host = FastMail.getHost();
String username = FastMail.getUsername();
String password = FastMail.getPassword();
Check if sending email was successful:
boolean sendingSuccessful = FastMail.sendMail("This is the subject", "This is the content", "to@example.com");
For the lazy ones :
FastMail.init("smtp.example.com", "username", "password");
FastMail.sendMail("Subject", "Content", "recipient@example.com");
Now we support IMAP !
MailReader mailReader = new MailReader("imap.example.com", "username", "Password");
IMAPObject imapObject = mailReader.getMessages();
if (imapObject.getEmailFolders().size() == 0) {
System.err.println("No directories found on server!");
return;
}
ArrayList<EmailFolder> emailFolders = imapObject.getEmailFolders();
for (int i = 0; i < emailFolders.size(); i++) {
EmailFolder folder = emailFolders.get(i);
ArrayList<EmailObject> emailObjects = folder.getEmailObjects();
System.err.println(i + ". " + folder.getFolderName());
for (int j = 0; j < emailObjects.size(); j++) {
EmailObject emailObject = emailObjects.get(j);
System.out.println(String.format("\t%d.%d %s", i, j, emailObject.getSubject()));
System.out.println(String.format("\t\t%s%s%s", "[", emailObject.getContent(), "]"));
System.out.println(String.format("\t\t%s%s%s %s%s%s", "[", emailObject.getSendDate(), "]", "[", emailObject.getReceivedDate(), "]"));
}
}