RSS RSS feed
November 21, 2009
Hot Topics:

Java meets e-mail

  • November 24, 1998
  • By Benoît Marchal
  • Send Email »
  • More Articles »

Listing 4 is the

reverse()
method. It suppress CR characters ('\r') from the string, because some mail servers choke at the CR/LF in reverse order.

Listing 4:

reverse()

private static String reverse(String toReverse)
{
   String noCR = toReverse.replace('\r',' ');
   StringBuffer reversed = new StringBuffer(noCR);
   reversed.reverse();
   return reversed.toString();
}

We are almost there. Listing 5 is the

main()
method, which simply takes a URL from the command line.

Listing 5:

main()

static public void main(String[] args)
   throws MessagingException, IOException
{
   if(args.length < 1)
   {
      System.out.println("usage is: " +
         "FunnyMail store://name:password@host");
      System.out.println("   e.g. FunnyMail " +
         "imap://fun:pwd@joker.psol.com");
   }
   else
   {
      FunnyMail mailer = new FunnyMail(args[0]);
      mailer.readAndReply();
   }
}

Monitoring the mailbox

FunnyMail illustrates how to automatically process mail from Java. However, FunnyMail must be run at regular intervals. We can further improve it by using the JavaMail notification mechanism, which fires a JavaBeans event when new mail arrives in the mailbox.

Listing 6 is the

monitorAndReply()
method, which registers a message handler for the
messagesAdded
event. For every new message, it calls
replyTo()
.

There are several practical limitations to the notification mechanism. For one thing, POP3 does not support it due to limitations in the underlying protocol. You need an IMAP server. Furthermore, your application has to periodically check on the IMAP server, for example, by calling

getMessageCount()
at regular intervals. That's the purpose of the final loop, which calls
getMessageCount()
every minute until the user presses a key.

I'm missing a simple method to test if a key has been pressed in Java. Obviously, AWT fires a

keyPressed
event, but using AWT is too much trouble for programs of this size.
System.in.available()
is helpful but is not very reliable. If you know of a better solution, I'd like to hear from you.

If you are using an IMAP server, modify

readAndReply()
to call
monitorAndReply()
before calling
expurge()
.

Listing 6:

monitorAndReply()

private void monitorAndReply(Folder folder)
   throws MessagingException, IOException,
          InterruptedException
{
   folder.addMessageCountListener(new MessageCountAdapter()
      {
         public void messagesAdded(MessageCountEvent ev)
         {
            Message[] msgs = ev.getMessages();
            for(int i = 0;i < msgs.length;i++)
               try
               {
                  replyTo(msgs[i]);
                  msgs[i].setFlag(Flags.Flag.DELETED,true);
               }
               catch(Exception ex)
               {
                  ex.printStackTrace();
               }
         }
      });

   while(System.in.available() == 0)
   {
      Thread.sleep(60000);
      folder.getMessageCount();
   }
}

E-mail from Java

JavaMail offers a simple yet powerful interface to messaging systems. It supports all the popular Internet protocols, either directly or through third parties. Now, it's easy to mail-enable your Java applications. Obviously, JavaMail also works with applets and servlets.

About the author

Benoît Marchal runs his own consulting company, Pineapplesoft sprl. His interests include distributed applications, object-oriented programming and design, system programming, hand-helds, and computer languages (notably, Java). He also enjoys teaching and writing. You can contact him at bmarchal@pineapplesoft.com.



1 2




Networking Solutions





Partners

  • Partner With Us














More for Developers

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs