Before you dash off to try the example code, a warning though. Talking to mail servers even with JavaMail has pitfalls even with the FAQ to hand. For example, when I was writing this article, I decided initially to use GMail, Google's mail service and its POP3 support. Strangely though, Google's POP3 service is not RFC compliant which means as soon as you read a message from it, not matter what you do, that message will disappear from the message queue. With another mail server, the version of the server interpreted the RFC standard on authentication slightly differently causing a login error. How do you track down problems like this? One big help is turning on JavaMail debugging; when you get the Session, you can call the setDebug method to turn on debugging which in turn dumps everything that goes up or down the wire to the console. Be warned, when you are getting attachments, this can be some rather large sections of base 64 encoded text. Another way to flip the debug on is to set the system property "mail.debug" to true.
JavaMail has a whole range of properties which can affect the behaviour of providers. For example, with POP3, the standard provider has properties which control timeouts, authentication and how the provider actually interacts with the mail server. This in turn may resolve those compatibility issues mentioned above. These properties are provider implementation specific. Take the standard POP3 provider though; some POP3 providers automatically delete read messages when you disconnect, but the POP3 RSET command can reset the mailbox to its original read state. The property mail.pop3.rsetbeforequit set to true can enable that behaviour in the POP3 provider; the "pop3" part of the property name reflecting the provider's name, so change it to "pop3s" if you are using secure POP3. Here's code from the example which enables that for both and turns debugging on:
Properties props=System.getProperties();
props.setProperty("mail.pop3s.rsetbeforequit","true");
props.setProperty("mail.pop3.rsetbeforequit","true");
session=Session.getInstance(props,null);
session.setDebug(true);
And if you are wondering, no, this has no effect on Google Mail's POP3 implementation; once you've read it, it's gone. Next month, we'll look at using the MailRetriever as a servlet, complete with displaying those attachments, and look at handling some other types of mail beyond simple attachments.
2010年9月28日
Gmail Pop3 implementation
订阅:
博文评论 (Atom)
没有评论:
发表评论