Open Source Java Mailing Library for Complex Emails
Java Mailing API allows developers to add attachments to Email messages, embedded images, add custom headers, CLI & S/MIME support inside Java apps.
Simple Java Mail is an open source lightweight Java mailing library that provides functionality for working with emails inside their Java applications. The library is very simple and easy to use enabling users to send emails via SMTP. Another great feature is that it allows users to convert between outlook MSG, EML, MimeMessage, and Email. The library has included Spring support and users can easily read properties from the Spring context.
The library is very lightweight but still very powerful and is the only java mailing library that can send through an authenticated SOCKS proxy or let users configure a cluster of connection pools. Simple Java Mail gets matured with the passage of time and has included support for some important features, such as adding attachments to email messages, embedded images, adding custom headers and related properties, CLI support, S/MIME support, advanced batch processing, user interfaces for email validation and sending, Spring support, Email conversion tools and many more.
The Simple Java Mail library always performs some basic validation such as CRLF injection attacks, verifies email addresses, checks connection and security properties, and many more. One other great aspect of the API is that it has included alternative ways for doing things for almost everything, for example, you can add your own Recipient instances or can add comma/semicolon separated addresses.
Getting Started with Simple Java Mail
First of all, you need to install JDK 1.6 or higher. You need to add the following maven dependency in pom.xml.
Maven Dependency
<dependency>
<groupId>org.simplejavamail</groupId>s;
<artifactId>simple-java-mail</artifactId>
<version>6.4.3</version>
</dependency>
Email Conversion b/t MimeMessage, EML and Outlook MSG
The open source Simple Java Mail library enables software developers to convert email between different email types. It also includes reading S/MIME-protected emails from the file. You can easily convert email objects, EML data, and even Outlook MSG files to MimeMessage. It is also very easy to build a mass Outlook MSG to EML converter.
Add Attachments to Email using Java
The open source Simple Java Mail library allows software developers to add Attachments to their email messages with just a couple of lines of Java code. It is very easy to add an attachment but you have to provide data yourself. Don’t worry it can be anything, a PDF document, A word processing document, an image, an Excel CSV spreadsheet, or anything else.
Add Attachments to Email via Java
currentEmailBuilder
.withAttachment("dresscode.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"))
.withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain")
// ofcourse it can be anything: a pdf, doc, image, csv or anything else
.withAttachment("invitation.pdf", new FileDataSource("invitation_v8.3.pdf"))
// you can provide your own list of attachments as well
.withAttachments(yourAttachmentResourceCollection))
Adding Custom Headers to Your Emails
The Simple Java Mail library makes it easy for Java developers to add custom headers and it’s relevant properties to their email messages inside their own applications. It is often required to add extra headers inside your email messages because the email server, recipient server, or your email client needs it. Whatsoever can be the reason, it is very easy to ad headers inside your email messages using The Simple Java Mail library.
Add Custom Headers to Emails via Java
currentEmailBuilder
.withHeader("X-Priority", 2);
.withHeader("X-MC-GoogleAnalyticsCampaign", "halloween_sale");
.withHeader("X-MEETUP-RECIP-ID", "71415272");
.withHeader("X-my-custom-header", "foo");
// or
.withHeaders(yourHeadersMap);
Email Addresses Validation
It is very important for companies to have a valid email address to communicate with their customers via email. Many times the email address is the sole means to interconnect with a particular individual. The Simple Java Mail library can easily validate your email addresses and makes your hard job easy for you. The library automatically performs address validation when sending emails. It also allows users to directly perform validations inside their Java apps. The library validation is not a simple regex check, but it provides a complete and robust full validation.
Validating Email Addresses via Java
currentMailerBuilder
.withEmailValidator(
JMail.strictValidator()
.requireOnlyTopLevelDomains(TopLevelDomain.DOT_COM)
.withRule(email -> email.localPart().startsWith("allowed"))
)
// or
.clearEmailValidator() // turn off validation
.resetEmailValidator() // reset to default (strict)
// you can also directly perform validations:
mailer.validate(email); // does all checks including address validation
// or just do the address validation
JMail.isValid("your_address@domain.com");
// or, fine-tuned to be stricter
JMail.strictValidator()
.isValid("your_address@domain.com");