Commons-Email provides an API for sending email. It is built on top of the Java Mail API, which it aims to simplify. Some of the mail classes that are provided are as follows:
- SimpleEmail – This class is used to send basic text based emails.
- MultiPartEmail – This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
- HtmlEmail – This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
- EmailAttachment – This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
- Download commons-email-1.0.jar from http://jakarta.apache.org/site/downloads/downloads_commons-email.cgi
- Add this to the CLASSPATH of your system if you want to use a standalone java program to send mails
- If you want to integrate commons mailing into your own J2EE project, then add this to the “lib” folder of your application and also add this to the build path.
- Also, make sure that j2ee.jar is present in your build path.
Once the paths have been properly set, we simply have to import the appropriate commons Class (Refer to http://jakarta.apache.org/commons/email/api-release/index.html ) into your own class and start using it just like a standard API, for eg, to send a simple email without an attachment, the following code would suffice
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
The call to setHostName("mail.myserver.com") sets the address of the outgoing SMTP server that will be used to send the message. If this is not set, the system property "mail.host" will be used.
Ease of customization/enhancements
The source code is made available as this is an Open source project. We can download the same and customize it according to our requirements although, the API is already built on top of generic Java Mailing API, thus there will not be many scenarios which would require customization apart from changing sever names and the recipients/sender’s ID!
Related Posts
- Java Class for Sending Email using Java API
- Java API for Voice Based Solutions
- Capitalization Errors while Java Programming
- UTL_MAIL package in Oracle 10g
- Java Developers Forgetting that Java is zero-indexed
Tags: Java API, Java Commons API, Java Email API, Java Opensource Email API





Wow. Great post! Are there any downfalls of using this library (performance/functionality issues?)?
I would be also interested in seeing this done with Java Mail API, if you ever have the time to blog about it
I am working on posting “Java Class for sending email using Java API” Hope it helps.
Thanks
look very much like email object in ColdFusion 9.
It’d be nice if they also had stuff for receiving emails. Sending isn’t that hard in Java Mail, but reading one is fairly tricky. Not as tricky as I’ve seen other languages make it, but not trivial.
Why reinvent wheel? Spring already provide such things with email api http://static.springsource.org...../mail.html. it very simple and muhc powerufl than build your own. Use spring instead build again your own.