Technology news and views
RSS icon Email icon Home icon
  • Apache JAMES Mailet: Processing Email Attachments

    Posted on November 1st, 2007 Sandeep 2 comments


    James Mailet


    The Apache JAMES project is a one-stop shop for your mail/news needs. At the core is the JAMES server, a Java SMTP/POP3 mail and NNTP news server. The project provides a Java API in form of the Apache Mailet API for “the rapid development and flexible deployment of email processing functionality.” While documentation is scarce, the Javadocs are pretty helpful, as the website says.

    One of the guys over at the JAMES server-user list posted a query on the ability to access/process a mail attachment from a mailet. I’m reproducing my response here, with a few more details. The JAMES server version assumed is 2.3.1 (latest and greatest at the time of this writing).

    Let’s assume you want to process incoming emails with pesky GIF images and modify their subject to include “[has GIF attachment]“, for whatever reason. It is assumed that the email is multipart MIME encoded. We will develop a matcher/mailet combo to do just this. The matcher is called “HasGifAttachment”, and the mailet is called “ChangeSubject”.


    public class HasGifAttachment extends GenericMatcher {

       * GIF attachment MIME type
      private static String m_gifAttachmentMimeType = “image/gif”;

       * Init method inherited from GenericMatcher
       * @param config matcher configuration, if any
      public void init(MatcherConfig config) throws MessagingException {

       * Implement the match method in the GenericMatcher interface.
       * See if the email has an attachment of type “image/gif”.
       * @param mail the email message
      public Collection match(Mail mail) throws MessagingException {
        boolean foundGifAttachment = false;

        try {
          if (mail.getMessage().getContent() instanceof MimeMultipart) {
            MimeMultipart mmp = (MimeMultipart) msg.getContent();

            for (int i = 0; i < mmp.getCount(); i++) {
              MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
              if( mbp.getContentType().equals(m_gifAttachmentMimeType) ) {
                foundGifAttachment = true;
        } catch ( Exception e ) {
          log(“Did not find a GIF attachment.”);

        if( foundGifAttachment ) {
          // this message needs further processing
          return mail.getRecipients();
        } else {
          return null;
    Once it is determined that the incoming email has a GIF attachment, the recipient list is sent to the mailet for further processing. In the mailet (for which you can extend the GenericMailet class shipped with JAMES), you can change the email subject as required. Part 2 of this post will have details on deploying this mailet under the JAMES server.


    2 responses to “Apache JAMES Mailet: Processing Email Attachments” RSS icon

    Leave a reply