| Listing 4 is the method. It suppress CR characters ('\r') from the string, because some mail servers choke at the CR/LF in reverse order. Listing 4:
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 method, which simply takes a URL from the command line. Listing 5:
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 method, which registers a message handler for the event. For every new message, it calls . 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 at regular intervals. That's the purpose of the final loop, which calls 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 event, but using AWT is too much trouble for programs of this size. 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 to call before calling . Listing 6:
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. |