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

Java 1.8用msal4j+javax.mail通过OAuth2发Office365邮件认证失败求助

Java 1.8 + MSAL4J + JavaMail发送Office365邮件时认证失败问题

错误信息

5.7.3 Authentication unsuccessful [........PROD.OUTLOOK.COM 2023-10-07T15:04:28.150Z ....]
Exception in thread "main" javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [........PROD.OUTLOOK.COM 2023-10-07T15:04:28.150Z ...]

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
at javax.mail.Service.connect(Service.java:366)

已尝试的方案

  • 尝试过基于OAuth2读取Outlook邮件的Java实现方案
  • 尝试过Office365 OAuth2发送邮件的相关解决方案
  • 参考过MSAL4J官方仓库的测试实现,但相关实现不完善

使用的依赖

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>msal4j</artifactId>
    <version>1.13.10</version>
</dependency>

当前令牌获取情况

目前仅当scope为.default或https://outlook.office365.com/.default时能正常获取令牌。

简化实现代码

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

    Properties props = new Properties();
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.host", "smtp.office365.com");
    props.put("mail.imap.ssl.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");
    props.put("mail.debug.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.auth.xoauth2.disable", "false");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth.login.disable", "true");
    props.put("mail.smtp.auth.plain.disable", "true");

    Session session = Session.getInstance(props);

    Transport transport = session.getTransport("smtp");

    IAuthenticationResult t = tokenFor("https://outlook.office365.com/.default").get();
    String token = t.accessToken();

    transport.connect("smtp.office365.com", 587, "user@foo.com", token);
    transport.send(new MimeMessage(session));
}

private static CompletableFuture<IAuthenticationResult> tokenFor(String... scopes) throws MalformedURLException {

    String tenantId = "...";
    String clientId = "...";
    String url = "https://login.microsoftonline.com/%s/oauth2/v2.0/token";

    String authority = String.format(url, tenantId);

    String clientSecret = "...";

    IClientCredential secret = ClientCredentialFactory.createFromSecret(clientSecret);

    ConfidentialClientApplication.Builder app = ConfidentialClientApplication.builder(clientId, secret)
            .authority(authority);

    ClientCredentialParameters.ClientCredentialParametersBuilder clientCredentialParam =
            ClientCredentialParameters.builder
                    (new HashSet<>(Arrays.asList(scopes)));

    return app.build().acquireToken(clientCredentialParam.build());
}

恳请各位帮忙测试排查该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:57:21