Introduction

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.

Installation and Intergration

  1. Download commons-email-1.0.jar from http://jakarta.apache.org/site/downloads/downloads_commons-email.cgi
  2. Add this to the CLASSPATH of your system if you want to use a standalone java program to send mails
  3. 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.
  4. Also, make sure that j2ee.jar is present in your build path.

Ease of Use

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

  1. Java Class for Sending Email using Java API
  2. Java API for Voice Based Solutions
  3. Capitalization Errors while Java Programming
  4. UTL_MAIL package in Oracle 10g
  5. Java Developers Forgetting that Java is zero-indexed

Tags: , , ,

5 Comments to “Java Commons-Email API”

  1. ppow says:

    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 :)

  2. Tech Guy says:

    I am working on posting “Java Class for sending email using Java API” Hope it helps.

    Thanks

  3. Henry Ho says:

    look very much like email object in ColdFusion 9. :)

  4. kjordan says:

    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.

  5. Ivan says:

    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.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>