You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java控制台应用通过IMAP访问Outlook个人邮箱遭认证失败

使用Java通过IMAP协议访问Outlook个人邮箱时遇到“A1 NO AUTHENTICATE failed”错误

我尝试用Java控制台应用,通过IMAP协议获取@outlook.com个人邮箱的收件箱内容。已创建Azure账号、对应应用及密钥,但遇到“A1 NO AUTHENTICATE failed”错误。应用账户类型设置为“任何组织目录中的账户(任何Microsoft Entra ID租户 - 多租户)和个人Microsoft账户(如Skype、Xbox)”,也尝试过其他类型选项。

相关代码如下:

private static String generateAccessToken(String clientId, String clientSecret, String tenantId)
            throws MalformedURLException, ExecutionException, InterruptedException {
       ConfidentialClientApplication
               app = ConfidentialClientApplication.builder(clientId, ClientCredentialFactory.createFromSecret(clientSecret))
                .authority(authUrl+"/"+tenantId)
                .build();
        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(scope)).build();
        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
           boolean isAuthorized = app.validateAuthority();
        System.out.println("isAuthorized = " + isAuthorized);
        System.out.println("url = " + app.authority());
            IAuthenticationResult result = future.get();
            return result.accessToken();

    }

    private static Store getIMAPStore() throws NoSuchProviderException {
        Properties props = getMailIMAPProperties();
        Session session = Session.getInstance(props);
        session.setDebug(true);
        return session.getStore("imaps");
    }

    private static Properties getMailIMAPProperties() {
        Properties props = new Properties();
        props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
        props.put("mail.imaps.auth.plain.disable", "true");
        props.put("mail.imaps.auth.xoauth2.disable", "false");

        return props;
    }

 public static void main(String[] args) throws MalformedURLException, ExecutionException, InterruptedException {

        String host = "outlook.office365.com";
        String email = "personal_email@outlook.com";
        String clientId = "client_id";
        String clientSecret = "clientSecret";
        String tenantId = "tenantId";

        String accessToken = generateAccessToken(clientId, clientSecret, tenantId);


        try{
            Store store = getIMAPStore();

            store.connect(host, email, accessToken);

            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            int messageCount = inbox.getMessageCount();
            System.out.printf("Total messages present in the folder 'Inbox' are %d.%n", messageCount);

            Message[] messages = inbox.getMessages();


            for (Message message : messages) {
                System.out.println(message.getSubject());
            }

            inbox.close(false);
            store.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

Azure相关配置截图:
Azure应用设置
Azure权限配置

内容的提问来源于stack exchange,提问作者umuieme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:05:21