Java 连接从Outlook 365邮箱读取电子邮件时发生SSL握手错误
我必须使用java代码连接到一个outlook 365帐户,并从该帐户的收件箱中读取电子邮件 错误: 无法连接到邮件存储:: javax.mail.MessaginException:连接失败;嵌套异常是: javax.net.ssl.SSLHandshakeException: sun.security.validator.validator异常:PKIX路径生成失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 查找到所请求目标的有效证书路径无法连接到 消息存储::javax.mail.MessaginException:连接失败; 嵌套异常为:javax.net.ssl.SSLHandshakeException: sun.security.validator.validator异常:PKIX路径生成失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 找到请求目标的有效证书路径 我不确定需要在web逻辑服务器上导入哪些证书,这样我的java代码就可以在服务器上运行,而不会出现SSL握手错误 Java代码:Java 连接从Outlook 365邮箱读取电子邮件时发生SSL握手错误,java,email,security,ssl,outlook,Java,Email,Security,Ssl,Outlook,我必须使用java代码连接到一个outlook 365帐户,并从该帐户的收件箱中读取电子邮件 错误: 无法连接到邮件存储:: javax.mail.MessaginException:连接失败;嵌套异常是: javax.net.ssl.SSLHandshakeException: sun.security.validator.validator异常:PKIX路径生成失败: sun.security.provider.certpath.SunCertPathBuilderException:无法
package client;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchProviderException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
public class readEmail {
public readEmail() {
super();
}
public static void main(String[] args) {
readEmail readEmail = new readEmail();
Properties properties = new Properties();
String saveDirectory = "D://attachment";
// server setting
properties.put("mail.pop3.host", "outlook.office365.com");
properties.put("mail.pop3.port", "995");
// SSL setting
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf("995"));
Session session = Session.getDefaultInstance(properties);
try {
// connects to the message store
Store store = session.getStore("pop3");
store.connect("username", "password");
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
// folderInbox.open(Folder.READ_ONLY);
folderInbox.open(Folder.READ_WRITE);
// fetches new messages from server
Message[] arrayMessages = folderInbox.getMessages();
if (arrayMessages.length == 0) {
}
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
Address[] fromAddress = message.getFrom();
String from = fromAddress[0].toString();
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType();
String messageContent = "";
// store attachment file name, separated by comma
String attachFiles = "";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
message.setFlag(Flags.Flag.DELETED, true);
}
// disconnect
folderInbox.close(true);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("Catch All" + ex.toString());
} catch (MessagingException ex) {
System.out.println("Catch All" + ex.toString());
} catch (IOException ex) {
System.out.println("Catch All" + ex.toString());
} catch (Exception ex) {
System.out.println("Catch All" + ex.toString());
}
}
}
包客户端;
导入java.io.File;
导入java.io.IOException;
导入java.security.NoSuchProviderException;
导入java.util.Properties;
导入javax.mail.Address;
导入javax.mail.Flags;
导入javax.mail.Folder;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.Part;
导入javax.mail.Session;
导入javax.mail.Store;
导入javax.mail.internet.MimeBodyPart;
公共类readEmail{
公开阅读电子邮件(){
超级();
}
公共静态void main(字符串[]args){
readEmail readEmail=新建readEmail();
属性=新属性();
String saveDirectory=“D://附件”;
//服务器设置
properties.put(“mail.pop3.host”、“outlook.office365.com”);
properties.put(“mail.pop3.port”,“995”);
//SSL设置
setProperty(“mail.pop3.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
properties.setProperty(“mail.pop3.socketFactory.fallback”、“false”);
properties.setProperty(“mail.pop3.socketFactory.port”,String.valueOf(“995”);
Session Session=Session.getDefaultInstance(属性);
试一试{
//连接到消息存储
Store Store=session.getStore(“pop3”);
store.connect(“用户名”、“密码”);
//打开收件箱文件夹
Folder folderInbox=store.getFolder(“收件箱”);
//folderInbox.open(文件夹只读);
folderInbox.open(Folder.READ\u WRITE);
//从服务器获取新邮件
Message[]arrayMessages=folderInbox.getMessages();
如果(arrayMessages.length==0){
}
for(int i=0;i1){
attachFiles=attachFiles.substring(0,attachFiles.length()-2);
}
}else if(contentType.contains(“text/plain”)| | contentType.contains(“text/html”)){
对象内容=message.getContent();
如果(内容!=null){
messageContent=content.toString();
}
}
message.setFlag(Flags.Flag.DELETED,true);
}
//断开
folderInbox.close(真);
store.close();
}捕获(无此提供异常例外){
System.out.println(“Catch All”+ex.toString());
}捕获(消息例外){
System.out.println(“Catch All”+ex.toString());
}捕获(IOEX异常){
System.out.println(“Catch All”+ex.toString());
}捕获(例外情况除外){
System.out.println(“Catch All”+ex.toString());
}
}
}
你们能帮我一下吗?我不是outlook专家,但一般来说:如果你连接到服务器(并使用SSL),你必须在客户端的TrustStore中拥有服务器的证书你好,谢谢。。。我将尝试一下Hello All,我可以知道需要导入哪些证书才能让我的服务器连接到outlook 365吗?Hello,我刚刚知道outlook 365使用的是TLS加密,而不是SSL。我已经从properties.setProperty(“mail.pop3.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”)更改了代码;财产