javamail读取邮件报:javax.mail.AuthenticationFailedException: EOF on socket解决办法:
如果连接邮件服务器、端口号、用户名、密码没有问题那就可能是一些认证没有被通过,我的这个问题是没有设置SSL认证所导致,在连接时添加如下:
如果连接邮件服务器、端口号、用户名、密码没有问题那就可能是一些认证没有被通过,我的这个问题是没有设置SSL认证所导致,在连接时添加如下:
Properties pop = new Properties();
//SSL认证
pop.setProperty("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory");
pop.setProperty("mail.pop3.socketFactory.port",port);
//还有其它一些认证或其它的:
pop.put("mail.smtp.auth", "true");
pop.put("mail.smtp.starttls.enable",Config.getConfig("mail-server","starttls"));
pop.put("mail.smtp.host", host);
pop.put("mail.mime.address.strict", "false");
pop.put("mail.smtp.connectiontimeout", Config.getConfig("mail-server", "connect-timeout"));
pop.put("mail.smtp.timeout", Config.getConfig("mail-server", "send-timeout"));
PopAuthenticator authenticatior = null;
if (Config.getConfig("mail-server", "authenticate").equalsIgnoreCase("TRUE")) {
authenticatior = new PopAuthenticator();
authenticatior.performCheck(userName, passWord);
}
this.session = Session.getDefaultInstance(pop, authenticatior);
Folder folder = null;
Store store = null;
try {
//这种方法也可以连接
/* URLName urln = new URLName("pop3",host, Integer.parseInt(port), null,userName, passWord);
store = session.getStore(urln);
store.connect();*/
store = session.getStore("pop3");
if(port != null && !port.equals("")) {
store.connect(host,Integer.parseInt(port), userName, passWord);
}else {
store.connect(host, userName, passWord);
}
folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
log.info("all mail size = " + message.length);
} catch (MessagingException e) {
log.error("邮件服务器连接错误!"+e.getMessage());
e.printStackTrace();
} catch(Exception e){
log.error(e.getMessage());
e.printStackTrace();
}finally {
try {
if (folder != null)
folder.close(true);
if (store != null)
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}