【问题标题】:Javax mail Office Exchange with oAuth2 (Token) SMTP带有 oAuth2(令牌)SMTP 的 Javax 邮件 Office Exchange
【发布时间】:2022-06-29 18:42:10
【问题描述】:

有人可以给我一个指南,通过 oAuth2 使用 javax 邮件对 Office 365 交换进行身份验证吗?

transport.connect(mailConfig.getMailUsername(), mailConfig.getMailPassword());

(I Tried also with Bearer + ...

Properties props = new Properties();

            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.host", "smtp.office365.com");
            props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.debug.auth", "true");
            props.put("mail.debug", "true");
            Session session = Session.getInstance(props);
            session.setDebug(true);
            Transport transport = session.getTransport("smtp");
            transport.connect(mailConfig.getMailUsername(), mailConfig.getMailPassword());

Photo of Permission for Microsoft Azure

Photo of Permission for Microsoft Azure

这是我的堆栈跟踪:

DEBUG SMTP: Attempt to authenticate using mechanisms: XOAUTH2
DEBUG SMTP: Using mechanism XOAUTH2
AUTH XOAUTH2 [...]
535 5.7.3 Authentication unsuccessful [ZR0P278CA0118.CHEP278.PROD.OUTLOOK.COM]

【问题讨论】:

  • 密码是你的访问令牌吧?
  • 我目前有同样的问题我唯一不同的是使用我从 login.microsoftonline.com/{tenantId}/oauth2/v2.0/token 获得的 access_token(使用 client_credential 授权)而不是密码。结果是完全相同的。我想有一个属性,我们必须将令牌放入其中,因为它不计量我用作密码参数的内容,它总是失败。

标签: java oauth-2.0 smtp office365


【解决方案1】:

我认为你需要使用

props.put("mail.smtp.sasl.mechanisms.oauth2.oauthToken", accessToken);

然后使用 null 作为密码

transport.connect("smtp.office365.com", mailConfig.getMailUsername(), null);

对我来说这是有效的

DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 AS9PR04CA0130.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 29 Jun 2022 10:36:34 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 2015-08-31
    • 2016-09-13
    • 2015-11-03
    • 2015-11-27
    • 2020-02-21
    • 1970-01-01
    相关资源
    最近更新 更多