WEB环境javamail取不到pop3邮件,急!!!
最近项目要使用javamail,邮件服务器使用james,是部署在内网的,没有开外网域名。
使用javamail获取邮件信息时总是取不到,而且有一个怪问题,就是同一段代码在main方法里执行是可以取到邮件的,放到其他方法里一调用就不行,我都快疯了。
哪位高人解决,高分!
------解决方案--------------------该回复于2012-11-21 15:25:53被管理员删除
------解决方案--------------------代码如下:
public static List<Map> getMail(String connect,final String user,final String password) throws
MessagingException{
List<Map> list = new ArrayList<Map>();
List<Map> list2 = new ArrayList<Map>();
Properties props = new Properties();
//首页portal,接受邮箱服务器配置.
props.put("mail.pop3.host", OAConstConfig.EMAIL_CONNECT);
props.put("mail.pop3.port", OAConstConfig.EMAIL_CONNECT_PORT);
Session recesession = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user,password);
}
});
recesession.setDebug(true);
Store store = recesession.getStore("pop3");
store.connect(connect, user, password);
Folder folder = store.getDefaultFolder().getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// folder.open(Folder.HOLDS_MESSAGES);
Message[] unreadMessages = folder.search(
new FlagTerm(new Flags(Flags.Flag.SEEN), false));
//int newCount=folder.getUnreadMessageCount();//得到新邮件数
System.out.println("未读:" + unreadMessages.length);
Message[] msgs = folder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++) {
//System.out.println(msgs[msgNum].getSubject());
//Object object = msgs[msgNum].getReceivedDate();
InternetAddress[] internetAddress = (InternetAddress[]) msgs[msgNum]
.getFrom();
Map<String, String> map = new HashMap<String, String> () ;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (msgs[msgNum].getSentDate()!=null)
map.put("Time", sdf.format(msgs[msgNum].getSentDate()));
if(internetAddress!=null)
map.put("Address", internetAddress[0].getAddress());
if(internetAddress!=null) {
String personal = internetAddress[0].getPersonal();
map.put("Personal", personal==null?internetAddress[0].getAddress():personal);
}
map.put("Subject", msgs[msgNum].getSubject());
list.add(map);
}
for (Map map : list) {
list2.add(map);
}
folder.close(false);
store.close();
return list2;
}
------解决方案--------------------我本人试验了,这段代码在具备外网ip的james邮件服务器上试验没问题.
------解决方案--------------------问题找到了,是别的包和javamail的包冲突。