使用Java从Microsoft邮件Exchange服务器读取带附件的邮件

时间:2015-09-23 07:09:48

标签: java server exchange-server

我一直在尝试使用java代码从我的Microsoft Exchange服务器检索电子邮件。

这是我的代码

    Properties properties = new Properties();
    properties.put("mail.pop3.host", host);
    properties.put("mail.pop3.port", port);
    // 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(port));

    properties.setProperty("mail.auth.debug","true");
    String pid = "";
    Boolean chkFlag = false;
    Session session = Session.getInstance(properties);
    try {
        properties.setProperty("mail.store.protocol", "pop3");
        session = Session.getDefaultInstance(properties, null);
        Store store = session.getStore("pop3");
        System.out.println("host,userName, password"+host +" " +userName + " "+password );
        store.connect(host,userName, password);
        Folder folderInbox = store.getFolder("Inbox");
        folderInbox.open(Folder.READ_WRITE);
        System.out.println("bffhb");
        String MailsaveDirectory = saveDirectory;
        Message[] arrayMessages = folderInbox.search(new FlagTerm(new Flags(
                Flags.Flag.SEEN), false));
        System.out.println("No. of Unread Messages : " + arrayMessages.length);
        for (int i = 0; i < arrayMessages.length; i++) {
            System.out.println("msgcnt");
            Message message = arrayMessages[i];
            Address[] fromAddress = message.getFrom();
            Address[] toAddress = message.getRecipients(Message.RecipientType.TO);
            String from = fromAddress[0].toString();
            String to = toAddress[0].toString();
            String subject = message.getSubject();
            String queueId = "";
            String sentDate = message.getSentDate().toString();
            String contentType = message.getContentType();
            String messageContent = "";
            String attachFiles = "";
            MailMht mh = new MailMht();
            saveDirectory = MailsaveDirectory + File.separator + "Message-" + (i + 1);
            System.out.println("saveDirectory "+saveDirectory);

                pid = subject.substring(subject.lastIndexOf(":") + 1);
                mh.parseMessages(message, folderInbox, saveDirectory);
                if (contentType.contains("multipart")) {

                }
                if (true) {

                    File dir = new File(saveDirectory);
                    System.out.println("dir to be created:" + saveDirectory);
                    if (dir.exists() == false) {
                        System.out.println("Creating Dir...");
                        dir.mkdirs();
                        System.out.println("Created Dir...");
                    }
                    if (contentType.contains("multipart")) {
                    }

            } else {
                chkFlag = false;
            }

        try {
            Folder folderI = null;
            if (chkFlag) {

                folderInbox.setFlags(arrayMessages, new Flags(Flags.Flag.DELETED), true);
                folderInbox.close(true);

                store.close();
                chkFlag = true;
            }
        } catch (Exception e) {
            chkFlag = false;
            e.printStackTrace();
            folderInbox.close(false);
            store.close();
        }
    } 

    } catch (MessagingException ex) {
        System.out.println("Could not connect to the message store");
        ex.printStackTrace();
        chkFlag = false;
    } 
    return chkFlag;
}

我几乎完成了所有事情,我收到了身份验证异常

  

javax.mail.AuthenticationFailedException:[AUTH]身份验证   失败。

但我可以通过live.com网站登录。

有人有建议摆脱这个吗?

1 个答案:

答案 0 :(得分:0)

是:您在收到邮件会话时错过了身份验证器

Authenticator authenticator=new Authenticator()
{
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(userName, password);
    }
};
properties.setProperty("mail.pop3.auth", "true");
Session session=Session.getInstance(props, authenticator);

顺便说一句,你有两个会话初始化:删除其中一个。