在java中从office 365读取邮件

时间:2016-01-18 09:21:27

标签: java

我必须使用以下代码阅读来自office 365的邮件,但它没有连接到服务器:

private void init()
{
    try{
        Properties smtpProps = new Properties();
        smtpProps.put("mail.host", "192.168.10.18");
        smtpProps.put("mail.port", "995");
        smtpProps.put("mail.transport.protocol", "imaps");  

        session = Session.getInstance(smtpProps,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("abc@abc.om","test");
                    }
                  });
            System.out.println("session = "+session);

       Store store = session.getStore("imaps");
       store.connect();
       Folder inbox = store.getFolder("INBOX");
       inbox.open(Folder.READ_WRITE);           
       Message messages[] = null;
       messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
       System.out.println("leng = "+messages.length);
    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

在命令提示符下执行此命令。 如果您收到响应为"正在连接到192.168.10.18 ...无法打开与主机的连接,则在端口995上:连接失败",问题在于您与防火墙的连接。

C:\>telnet 192.168.10.18 995