连接从Outlook 365邮箱

时间:2017-03-21 10:38:49

标签: java email security ssl outlook

我必须使用java代码连接到一个Outlook 365帐户,并从该帐户读取INBOX的电子邮件。

错误:

  

无法连接到邮件存储::   javax.mail.MessagingException:连接失败;嵌套异常是:   javax.net.ssl.SSLHandshakeException:   sun.security.validator.ValidatorException:PKIX路径构建失败:   sun.security.provider.certpath.SunCertPathBuilderException:无法   找到要求的targetCould无法连接的有效证书路径   消息存储:: javax.mail.MessagingException:连接失败;   嵌套异常是:javax.net.ssl.SSLHandshakeException:   sun.security.validator.ValidatorException:PKIX路径构建失败:   sun.security.provider.certpath.SunCertPathBuilderException:无法   找到所请求目标的有效证书路径

我不确定需要在Web逻辑服务器上导入哪些证书,以便My java代码可以在没有SSL握手错误的服务器上运行。

Java代码:

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());
        }
    }
}

你们可以帮助我吗?

0 个答案:

没有答案