Javax邮局与OAuth2(令牌)SMTP

发布于 2025-02-04 09:17:32 字数 1219 浏览 5 评论 0原文

有人可以给我aguide用oauth2与Javax邮件进行身份验证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());

Microsoft azure的许可照片

fqw54.png“ rel =“ noreferrer”> Microsoft Azure许可的照片

这是我的stackstrace:

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]

Can someone give me aguide to authenticate to office 365 exchange with javax mail with oAuth2?

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

Here is my stackstrace:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尘曦 2025-02-11 09:17:32

目前不支持使用客户端凭据流进行SMTP身份验证。

请参阅Microsoft的文档中的注释, smtp协议交换

另外,请参阅交换团队博客。他们只是在2022年6月添加了对IMAP和POP的支持。SMTP还没有那里,他们尚未分享他们期望何时拥有的时间表。他们报告说,他们将继续支持SMTP的基本身份验证(用户名/密码)。

Using the client credentials flow for SMTP Authentication is currently not supported.

See the NOTE in Microsoft's doc on the SMTP Protocol Exchange.

Also, see this post from the Exchange Team Blog. They just added support for IMAP and POP in June 2022. SMTP is not yet there and they have not shared a timeline for when they expect to have it. They report that they will continue to support basic authentication (username/password) for SMTP.

七度光 2025-02-11 09:17:32

以下是对我有用的属性,假设您在O365帐户上启用了SMTP auth,并且成功地使用范围“ https://outlook.office365.com/smtp.smtp.send offline_access”就代币进行了协商



    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.smtp.starttls.required", "true");
    props.put("mail.smtp.auth", "true");

。 've阅读,在调用transport.connect()时传递您的访问权限,而不是密码,而javamail会处理格式化:



    Session session = Session.getInstance(props);
    try {
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.office365.com", emailAddress, accessToken);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }   

这是由此产生的对话:

    DEBUG SMTP: Attempt to authenticate using mechanisms: XOAUTH2
    DEBUG SMTP: Using mechanism XOAUTH2
    AUTH XOAUTH2 (LongStringOfAlphaNumerics)
    235 2.7.0 Authentication successful

好运,希望这可以节省某人的时间!

here are the properties that worked for me, assuming you have SMTP AUTH enabled on your O365 account and have successfully negotiated the token with scope "https://outlook.office365.com/SMTP.Send offline_access"



    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.smtp.starttls.required", "true");
    props.put("mail.smtp.auth", "true");

And then contrary to other advice I've read, pass your accessToken in instead of the password when calling Transport.connect(), and JavaMail will take care of formatting it:



    Session session = Session.getInstance(props);
    try {
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.office365.com", emailAddress, accessToken);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }   

Here's the resulting conversation:

    DEBUG SMTP: Attempt to authenticate using mechanisms: XOAUTH2
    DEBUG SMTP: Using mechanism XOAUTH2
    AUTH XOAUTH2 (LongStringOfAlphaNumerics)
    235 2.7.0 Authentication successful

Best of luck, hopefully this will save someone some time!

场罚期间 2025-02-11 09:17:32

我认为您需要使用

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

密码

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

,然后将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

I think you need to use

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

and then use null as a password

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

For me this was working

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
苦妄 2025-02-11 09:17:32

截至2022年10月10日,仍然不支持SMTP客户端凭证流。他们宣布了对POP和IMAP的支持,但我没有对SMTP的支持,因此我向Microsoft社区中心提出了一个问题,因为我经历了同一问题。我是由Microsoft团队的一员Greg Taylor告诉我的,SMTP尚不可能进行客户凭证流。他说,他们将宣布何时可用,计划/希望是在年底之前。有关我的问题,请参见此链接,响应 https://techcommunity.microsoft.com/t5/exchange-team-team-blog/announcing-oauth--oauth-2-0-client-client-creient-credentials-flow-flow-support -For-and/ba-p/3562963

As of 10-Nov-2022, the SMTP client credential flow is still not supported. They announced support for POP and IMAP but I saw no support for SMTP so I submitted a question to the Microsoft Community Hub since I am experiencing the same issue. I was told by Greg Taylor, who is part of the EXCHANGE Microsoft team, that client credential flow is not yet possible with SMTP. He said they will announce when it's available, the plan/hope is that it will be by the end of the year. See this link for my question and the response https://techcommunity.microsoft.com/t5/exchange-team-blog/announcing-oauth-2-0-client-credentials-flow-support-for-pop-and/ba-p/3562963

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文