邮件读取连接时报
javax.mail.AuthenticationFailedException: EOF on socket错误解决方法:
当邮件读取需要SSL认证时连接需采用:
Properties pop = new Properties();
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"));
javax.mail.AuthenticationFailedException: EOF on socket错误解决方法:
当邮件读取需要SSL认证时连接需采用:
Properties pop = new Properties();
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();
}
}