【问题标题】:Read email from office365 inbox without using of IMAP and POP3不使用 IMAP 和 POP3 从 office365 收件箱中读取电子邮件
【发布时间】:2022-01-17 12:50:52
【问题描述】:

需要在不使用 IMAPPOP3 协议的情况下从 office365 收件箱读取电子邮件,因为在我的情况下这些协议被禁用而且由于某些安全问题,我无法启用它们。如果没有这两个协议,请建议我是否有其他方法可以做到这一点。

提前致谢!

【问题讨论】:

    标签: java email jakarta-mail


    【解决方案1】:

    是的,还有另一种方法(实际上有几种,但我会建议一种)。有一个称为 EWS (Exchange Web Services) 的 API。使用起来非常简单。您只需要帐户的凭据。

    这是一个小例子:

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); // This is the latest version of this library
    
    ExchangeCredentials credentials = new WebCredentials("email", "password");
    service.setCredentials(credentials);
    // this.exchangeService.setWebProxy(new WebProxy("xx.xxx.xxx.xx", 8080)); // If you're behind a proxy
    service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx")); // This is the standard URL
    
    Folder inboxFolder = Folder.bind(service, WellKnownFolderName.Inbox);
    
    FindItemsResults<Item> results = service.findItems(inboxFolder.getId(), new ItemView(10)); // 10 is the number of items to fetch (pagesize)
    
    for (Item result : results)
    {
        EmailMessage currentEmail = (EmailMessage) result;
    
        System.out.println(currentEmail.getFrom());
        // And so on
    }
    

    图书馆可以做更多的事情,例如阅读/预约、发送电子邮件等等。

    据我所知,该库使用 SOAP,因此您不会受到 POP 和 IMAP 的影响。

    【讨论】:

    • 请注意,Exchange Online/Office 365 不再支持基本身份验证,需要使用 OAuth (docs.microsoft.com/en-us/exchange/client-developer/…)。
    • @dunni 上面带有正确凭据的 sn-p 对我有用。我还读到他们已停用基本身份验证。但显然,它仍然有效......
    猜你喜欢
    • 2019-02-21
    • 2017-09-23
    • 1970-01-01
    • 2018-06-21
    • 2020-06-22
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多