Java 无法连接JDK 8上的电子邮件服务器
我无法连接IMAP邮件服务器以从jdk8获取邮件,但它可以与jdk7一起正常工作。 下面是我的代码Java 无法连接JDK 8上的电子邮件服务器,java,email,authentication,jakarta-mail,imap,Java,Email,Authentication,Jakarta Mail,Imap,我无法连接IMAP邮件服务器以从jdk8获取邮件,但它可以与jdk7一起正常工作。 下面是我的代码 private Properties getServerProperties(String protocol, String host, String port) { Properties properties = new Properties(); properties.put(String.format("mail.%s.host", protocol), host);
private Properties getServerProperties(String protocol, String host, String port) {
Properties properties = new Properties();
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), port);
properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
return properties;
}
public void getNewEmails(String protocol, String host, String port, String userName, String password) {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getDefaultInstance(properties);
session.setDebug(true);
try {
Store store = session.getStore(protocol);
store.connect(userName, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
int count = inbox.getMessageCount();
Message[] messages = inbox.getMessages(1, count);
for (Message message : messages) {
Address[] fromAddresses = message.getFrom();
System.out.println("...................");
System.out.println("\t From: " + fromAddresses[0].toString());
System.out.println("\t To: " + parseAddresses(message.getRecipients(RecipientType.TO)));
System.out.println("\t CC: " + parseAddresses(message.getRecipients(RecipientType.CC)));
System.out.println("\t Subject: " + message.getSubject());
System.out.println("\t Sent Date:" + message.getSentDate().toString());
System.out.println(message.getContent());
}
inbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for protocol: " + protocol);
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String parseAddresses(Address[] address) {
String listOfAddress = "";
if ((address == null) || (address.length < 1))
return null;
if (!(address[0] instanceof InternetAddress))
return null;
for (int i = 0; i < address.length; i++) {
InternetAddress internetAddress = (InternetAddress) address[0];
listOfAddress += internetAddress.getAddress() + ",";
}
return listOfAddress;
}
JDK 8日志:
DEBUG:setDebug:
JavaMail version 1.5.0-
b01
DEBUG:
getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: trying to connect to host "XXXXXXXXXXXXXXXXXXX", port 993, isSSL false
* OK server ready. Unauthorized Access Prohibited.
A0 CAPABILITY
* CAPABILITY IMAP4REV1 IDLE AUTH=PLAIN
A0 OK CAPABILITY completed
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=XXXXXXXXXXXXXXXXXXX, user=XXXXXXXXXXXXXXXXXXX, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG:setDebug:
JavaMail版本1.5.0-
b01
调试:
getProvider()返回javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
调试IMAP:mail.IMAP.fetchsize:16384
调试IMAP:mail.IMAP.ignorebodystructuresize:false
调试IMAP:mail.IMAP.statuscachetimeout:1000
调试IMAP:mail.IMAP.appendbuffersize:-1
调试IMAP:mail.IMAP.minidletime:10
调试IMAP:尝试连接到主机“xxxxxxxxxxxxxxxx”,端口993,isSSL false
*好的,服务器准备好了。禁止未经授权的访问。
A0能力
*能力IMAP4REV1空闲身份验证=普通
A0正常能力完成
调试IMAP:AUTH:PLAIN
调试IMAP:protocolConnect登录,主机=xxxxxxxxxxxxxxxx,用户=xxxxxxxxxxxxxxxx,密码=
调试IMAP:取消验证普通命令跟踪
在上面的线之后,它挂起并且没有响应 在Bill Shannon发表评论后,我尝试了下面的配置,它开始使用JDK-8进行下面的配置。
After Bill Shannon comment I tried below configuration and it start working with below configuration with JDK-8.
`<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>`
`
com.sun.mail
javax.mail
1.5.5
`
@DoNhuVy我已经从jdk添加了日志。修复这些日志并升级到。如果这不能解决问题,请通过javamail与我联系_ww@oracle.com获取更详细的调试建议;我们可能需要查看身份验证的详细信息,您不想在这里发布这些信息。
After Bill Shannon comment I tried below configuration and it start working with below configuration with JDK-8.
`<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>`