首页 > 解决方案 > 连接outlook 365登录失败

问题描述

package com.email.test;

import java.util.Arrays;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SampleEmailReader {

    public static final int SUCCESS = 0;
    public static final int FAILURE = -1;

    private transient static final Logger logger = LoggerFactory.getLogger(SampleEmailReader.class);

    public SampleEmailReader() {
    }

    public void processEmailAttachements() {
        try {
            logger.info("Started processEmailAttachements ");
            Properties emailProperties = new Properties();
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";


        emailProperties.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
        emailProperties.setProperty("mail.imaps.socketFactory.fallback", "false");
        emailProperties.setProperty("mail.imaps.port", "993");
        emailProperties.setProperty("mail.imaps.socketFactory.port", "993");
        emailProperties.setProperty("mail.imaps.auth", "true");     
        emailProperties.setProperty("mail.imaps.host", "outlook.office365.com");
        emailProperties.setProperty("mail.imaps.auth.plain.disable", "true");
        emailProperties.setProperty("mail.imaps.auth.gssapi.disable", "true");
        emailProperties.setProperty("mail.imaps.auth.ntlm.disable", "true");
        emailProperties.setProperty("mail.imaps.ssl.enable", "true");

            Session session = Session.getInstance(emailProperties,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("test@outlook.com", "password@2016");
                        }
                    });

            Store store = session.getStore("imaps");

            store.connect();

            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);
            logger.info("Inbox MessageCount-->" + inbox.getMessageCount());
            Message messages[] = inbox.search(new FlagTerm(
                    new Flags(Flag.SEEN), false));
            logger.info("Number of UnRead Mails = " + messages.length);
            for (Message inboxMessage : messages) {
                int status = processMessageBody(inboxMessage);
                if (status == SUCCESS) {
                    inboxMessage.setFlag(Flag.SEEN, true);
                } else {
                    inboxMessage.setFlag(Flag.SEEN, false);
                }
            }
            inbox.close(true);
            store.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error("Error in processEmailAttachements", ex);
        }

    }

    private int processMessageBody(Message message) {
        int status = FAILURE;
        try {
            logger.info("Started processMessageBody*******");
            logger.info("Message Subject******" + message.getSubject());
            logger.info("From Address *******"
                    + Arrays.toString(message.getFrom()));
            Object content = message.getContent();
            if (content instanceof String) {
                logger.error("Invalid Content Type.No need to process the Mail with Subject "
                        + message.getSubject());
            } else if (content instanceof Multipart) {
                Multipart multiPart = (Multipart) content;
                try {
                    for (int i = 0; i < multiPart.getCount(); i++) {
                        BodyPart bodyPart = multiPart.getBodyPart(i);

                        if (bodyPart.getDisposition() != null
                                && bodyPart.getDisposition().equalsIgnoreCase(
                                        Part.ATTACHMENT)
                                && bodyPart.getFileName() != null) {
                            status = readExcel(bodyPart);

                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("BatchException in procesMultiPart", e);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Exception in processMessageBody", e);
        }
        return status;
    }

    public abstract int readExcel(BodyPart bodyPart);

}

执行

包电子邮件表;

导入 javax.mail.BodyPart;

public class Email {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SampleEmailReader reader = new SampleEmailReader() {

            @Override
            public int readExcel(BodyPart bodyPart) {
                // TODO Auto-generated method stub
                return 0;
            }
        };
        reader.processEmailAttachements();

    }

}

获取身份验证失败..但用户名密码正确。我可以使用用户名和密码登录outlook 365。唯一的区别是我看到浏览器上显示了证书。我需要使用任何证书来阅读邮件吗?

javax.mail.AuthenticationFailedException: LOGIN failed.
    at com.sun.mail.imap.IMAPStore.protocolConnect

标签: jakarta-mail

解决方案


推荐阅读