JAVAMAIL邮件接收开发

邮件读取连接时报
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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值