{"id":434,"date":"2023-03-02T17:01:38","date_gmt":"2023-03-02T09:01:38","guid":{"rendered":"https:\/\/fugary.com\/?p=434"},"modified":"2023-03-02T17:01:38","modified_gmt":"2023-03-02T09:01:38","slug":"office365%e5%8f%91%e9%80%81%e5%92%8c%e6%8e%a5%e6%94%b6%e9%82%ae%e4%bb%b6%e8%ae%a4%e8%af%81%e6%a8%a1%e5%bc%8f%e5%8d%87%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=434","title":{"rendered":"Office365\u53d1\u9001\u548c\u63a5\u6536\u90ae\u4ef6\u8ba4\u8bc1\u6a21\u5f0f\u5347\u7ea7"},"content":{"rendered":"<p>\u4e3a\u4e86\u63d0\u5347\u5b89\u5168\u6027\uff0c<code>Office365<\/code>\u5347\u7ea7\u4e86\u90ae\u4ef6\u5ba2\u6237\u7aef\u8ba4\u8bc1\u6a21\u5f0f\uff0c\u9700\u8981\u4f7f\u7528<code>OAuth 2.0<\/code>\u8ba4\u8bc1\uff0c\u5e9f\u5f03\u4ee5\u524d\u8001\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u65b9\u5f0f\u3002\u56e0\u6b64\u4ee5\u524d\u7528<code>JavaMail<\/code>\u5f00\u53d1\u7684\u90ae\u4ef6\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\u5c06\u4e0d\u80fd\u4f7f\u7528\uff0c\u6bd4\u5982\u4ece\u5176\u4ed6\u90ae\u4ef6\u670d\u52a1\u8fc1\u79fb\u5230<code>Office365<\/code>\uff0c\u5fc5\u987b\u7528\u65b0\u7684\u65b9\u5f0f\u4fee\u6539\u76f8\u5173\u4ee3\u7801\u3002<\/p>\n<h3>\u5347\u7ea7\u8bf4\u660e<\/h3>\n<p>\u5fae\u8f6f\u5b98\u65b9\u8bf4\u660e\u5730\u5740\uff1a<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/exchange\/troubleshoot\/administration\/cannot-connect-mailbox-pop-imap-outlook?source=recommendations\">https:\/\/learn.microsoft.com\/zh-cn\/exchange\/troubleshoot\/administration\/cannot-connect-mailbox-pop-imap-outlook?source=recommendations<\/a><\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/exchange\/clients-and-mobile-in-exchange-online\/deprecation-of-basic-authentication-exchange-online#pop-imap-and-smtp-auth\">https:\/\/learn.microsoft.com\/zh-cn\/exchange\/clients-and-mobile-in-exchange-online\/deprecation-of-basic-authentication-exchange-online#pop-imap-and-smtp-auth<\/a><\/p>\n<p>\u5efa\u8bae\u79df\u6237\u7981\u7528<a href=\"https:\/\/techcommunity.microsoft.com\/t5\/exchange-team-blog\/basic-authentication-and-exchange-online-april-2020-update\/ba-p\/1275508\">\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1<\/a>\uff0c\u5e76\u8fc1\u79fb\u5230\u65b0\u5f0f\u5ba2\u6237\u7aef\u7684<a href=\"https:\/\/techcommunity.microsoft.com\/t5\/exchange-team-blog\/improving-security-together\/ba-p\/805892\">\u65b0\u5f0f\u8eab\u4efd\u9a8c\u8bc1<\/a>\u79df\u6237\u3002\u4e5f\u5c31\u662f\u3010\u73b0\u4ee3\u8eab\u4efd\u9a8c\u8bc1\uff08\u57fa\u4e8e <code>OAuth 2.0<\/code> \u4ee4\u724c\u7684\u8eab\u4efd\u9a8c\u8bc1\uff09\u3011<\/p>\n<h4>OAuth 2.0\u8ba4\u8bc1<\/h4>\n<p>OAuth 2.0\u8ba4\u8bc1\u6a21\u5f0f\u6709\u56db\u79cd\u6a21\u5f0f<\/p>\n<ol>\n<li>\n<p>\u5ba2\u6237\u7aef\u8ba4\u8bc1\u6a21\u5f0f(POST)<\/p>\n<ul>\n<li><a href=\"https:\/\/xxxxxxxx\/oauth\/token?grant_type=client_credentials&amp;client_id=xxxx&amp;client_secret=xxxx\">https:\/\/xxxxxxxx\/oauth\/token?grant_type=client_credentials&client_id=xxxx&client_secret=xxxx<\/a><\/li>\n<li>\u7b2c\u4e09\u65b9\u5ba2\u6237\u7aef\u6388\u6743\uff0c\u4e00\u822c\u662f\u53ef\u4fe1\u4efb\u7684\u5ba2\u6237\u7aef\uff0c\u4e0d\u9700\u8981\u7528\u6237\u6388\u6743<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u5bc6\u7801\u6a21\u5f0f(POST)<\/p>\n<ol>\n<li><a href=\"https:\/\/xxxxxxxx\/oauth\/token?grant_type=password&amp;client_id=xxxx&amp;client_secret=xxxx&amp;username=username&amp;password=password\">https:\/\/xxxxxxxx\/oauth\/token?grant_type=password&client_id=xxxx&client_secret=xxxx&username=username&password=password<\/a><\/li>\n<li>\u76f4\u63a5\u7528\u7528\u6237\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u6388\u6743\uff0c\u4e00\u822c\u662f\u5185\u90e8\u5b50\u7cfb\u7edf\u4f7f\u7528<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>\u9690\u5f0f\u6388\u6743\u6a21\u5f0f(GET)<\/p>\n<ol>\n<li><a href=\"http:\/\/xxxxxxxx\/oauth\/authorize?response_type=token&amp;client_id=xxxx\">http:\/\/xxxxxxxx\/oauth\/authorize?response_type=token&client_id=xxxx<\/a><\/li>\n<li>\u6388\u6743\u540e<code>redirect<\/code>\u5e26\u56deaccess_token\uff0c\u5b89\u5168\u6027\u6bd4\u6388\u6743\u7801\u6a21\u5f0f\u4f4e<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>\u6388\u6743\u7801\u6a21\u5f0f(\u9700\u8981\u4e24\u6b65\uff0c\u5e38\u89c1\u4e14\u5b89\u5168\u6027\u8f83\u9ad8)<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b65\u83b7\u53d6code(GET)<br \/>\n<a href=\"https:\/\/xxxxxxxx\/oauth\/authorize?response_type=code&amp;client_id=xxxx\">https:\/\/xxxxxxxx\/oauth\/authorize?response_type=code&client_id=xxxx<\/a><\/li>\n<li>\u7b2c\u4e8c\u6b65\u83b7\u53d6token\uff0c\u9700\u8981\u4f20\u9012\u4e0a\u4e00\u6b65\u8fd4\u56de\u7684code(POST)<br \/>\n<a href=\"https:\/\/xxxxxxxx\/oauth\/token?grant_type=authorization_code&amp;code=code&amp;client_id=xxxx\">https:\/\/xxxxxxxx\/oauth\/token?grant_type=authorization_code&code=code&client_id=xxxx<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u4e86\u89e3\u66f4\u591a<code>OAuth2<\/code>\uff1a<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/azure\/active-directory\/develop\/active-directory-v2-protocols\">https:\/\/learn.microsoft.com\/zh-cn\/azure\/active-directory\/develop\/active-directory-v2-protocols<\/a><\/p>\n<h4>\u63d0\u524d\u51c6\u5907<\/h4>\n<p>\u76ee\u524d\u4f3c\u4e4e\u662f\u4fdd\u7559\u4e86SMTP\u6a21\u5f0f\u7684\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\uff1a<\/p>\n<blockquote>\n<p>\u5728 2022 \u5e74 10 \u6708 1 \u65e5\u6c38\u4e45\u7981\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\u65f6\uff0cSMTP \u8eab\u4efd\u9a8c\u8bc1\u4ecd\u53ef\u7528\u3002 SMTP \u4ecd\u7136\u53ef\u7528\u7684\u539f\u56e0\u662f\uff0c\u8bb8\u591a\u591a\u529f\u80fd\u8bbe\u5907\uff08\u5982\u6253\u5370\u673a\u548c\u626b\u63cf\u4eea\uff09\u65e0\u6cd5\u66f4\u65b0\u4e3a\u4f7f\u7528\u65b0\u5f0f\u8eab\u4efd\u9a8c\u8bc1\u3002 \u4f46\u662f\uff0c\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u5ba2\u6237\u5c3d\u53ef\u80fd\u4e0d\u4f7f\u7528\u57fa\u672c\u8eab\u4efd\u9a8c\u8bc1\u548c SMTP AUTH\u3002 \u53d1\u9001\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u90ae\u4ef6\u7684\u5176\u4ed6\u9009\u9879\u5305\u62ec\u4f7f\u7528\u66ff\u4ee3\u534f\u8bae\uff0c\u4f8b\u5982 <a href=\"https:\/\/learn.microsoft.com\/zh-cn\/graph\/api\/user-sendmail\">Microsoft \u56fe\u5f62 API<\/a>\u3002<\/p>\n<\/blockquote>\n<p>\u9996\u5148\u5e94\u8be5\u8981\u6ce8\u518cAPP\uff1a<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/azure\/active-directory\/develop\/quickstart-register-app\">https:\/\/learn.microsoft.com\/zh-cn\/azure\/active-directory\/develop\/quickstart-register-app<\/a><\/p>\n<p>\u7531\u4e8e\u662f\u670d\u52a1\u5668\u540e\u53f0\u53d1\u9001\u548c\u63a5\u6536\u90ae\u4ef6\uff0c\u662f\u9884\u8bbe\u7684\u90ae\u4ef6\u5730\u5740\u548c\u8d26\u53f7\uff0c\u56e0\u6b64\u9009\u7528<code>client_credentials<\/code>\u6a21\u5f0f\uff0c\u5728<code>Office365<\/code>\u4e0a\u914d\u7f6e\u597d\u76f8\u5e94\u7684<code>APP<\/code>\uff0c\u5e76\u5f97\u5230<code>tenant<\/code>\u3001<code>clientId<\/code>\u548c<code>clientSecret<\/code>\u3002<\/p>\n<p>\u8eab\u4efd\u9a8c\u8bc1\u4ecb\u7ecd\uff1a<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/exchange\/client-developer\/legacy-protocols\/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth\">https:\/\/learn.microsoft.com\/zh-cn\/exchange\/client-developer\/legacy-protocols\/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth<\/a><\/p>\n<p>\u4e3b\u8981\u6709\u4e0b\u9762\u4e09\u4e2a\u914d\u7f6e\u9879\u9700\u8981\u7528\u5230\uff1a<\/p>\n<blockquote>\n<p>\u5e94\u7528\u7a0b\u5e8f(\u5ba2\u6237\u7aef) ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx<\/p>\n<p>\u76ee\u5f55(\u79df\u6237) ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx<\/p>\n<p>clientSecret \uff0812\u4e2a\u6708\uff09\uff1axxxx~xxxxxxxxxxxxxxxxxxxxxx<\/p>\n<\/blockquote>\n<h4>\u670d\u52a1\u5730\u5740<\/h4>\n<p>\u914d\u7f6e\u670d\u52a1\u5730\u5740\uff1a<a href=\"https:\/\/support.microsoft.com\/zh-cn\/office\/pop-imap-%E5%92%8C-smtp-%E8%AE%BE%E7%BD%AE-8361e398-8af4-4e97-b147-6c6c4ac95353\">https:\/\/support.microsoft.com\/zh-cn\/office\/pop-imap-%E5%92%8C-smtp-%E8%AE%BE%E7%BD%AE-8361e398-8af4-4e97-b147-6c6c4ac95353<\/a><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>Email\u63d0\u4f9b\u7a0b\u5e8f<\/strong><\/th>\n<th><strong>IMAP \u8bbe\u7f6e<\/strong><\/th>\n<th><strong>POP \u8bbe\u7f6e<\/strong><\/th>\n<th><strong>SMTP \u8bbe\u7f6e<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Microsoft 365<br \/>Outlook<br \/>Hotmail<br \/>Live.com<\/td>\n<td>\u670d\u52a1\u5668\uff1aoutlook.office365.com<br \/>\u7aef\u53e3\uff1a993<br \/>\u52a0\u5bc6\uff1aSSL\/TLS<\/td>\n<td>\u670d\u52a1\u5668\uff1aoutlook.office365.com<br \/>\u7aef\u53e3\uff1a995<br \/>\u52a0\u5bc6\uff1aSSL\/TLS<\/td>\n<td>\u670d\u52a1\u5668\uff1asmtp.office365.com<br \/>\u7aef\u53e3\uff1a587<br \/>\u52a0\u5bc6\uff1aSTARTTLS<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Java\u5ba2\u6237\u7aef\u5347\u7ea7<\/h3>\n<p>JavaMail\u5ba2\u6237\u7aef\u9700\u8981\u5347\u7ea7\uff1a<\/p>\n<p>\u6587\u6863\u5730\u5740\uff1a<a href=\"https:\/\/javaee.github.io\/javamail\/OAuth2\">https:\/\/javaee.github.io\/javamail\/OAuth2<\/a><\/p>\n<p>\u5347\u7ea7\u5230<code>1.5.5<\/code>\u4e4b\u540e\uff0c\u6700\u65b0\u5e94\u8be5\u662f<code>1.6.2<\/code>\u3002<\/p>\n<h4>\u4e0d\u652f\u6301pop3<\/h4>\n<p>\u4ece<code>JavaMail<\/code>\u4e0d\u652f\u6301<code>POP3<\/code>\u4f20\u8f93<code>OAuth2<\/code>\u4ee4\u724c\uff0c<strong>\u53ea\u80fd\u4f7f\u7528<code>IMAP<\/code>\u534f\u8bae<\/strong><\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/cce1094fa210a914ea88d434367a537c\/image-20230302111139232.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/cce1094fa210a914ea88d434367a537c\/image-20230302111139232.png\" alt=\"image-20230302111139232\" \/><\/div><\/p>\n<p>\u4f7f\u7528<code>pop3<\/code>\u534f\u8bae\u6267\u884c\u4f1a\u62a5\u9519\u8bef\uff1a<\/p>\n<blockquote>\n<p>javax.mail.AuthenticationFailedException: Protocol error. Connection is closed. 10<\/p>\n<p>at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:213)<br \/>\nat javax.mail.Service.connect(Service.java:366)<br \/>\nat javax.mail.Service.connect(Service.java:246)<\/p>\n<\/blockquote>\n<h4>\u5b9e\u9645\u4ee3\u7801<\/h4>\n<p>\u7b80\u5355\u4f9d\u8d56\uff0c<code>httpclient<\/code>\u3001<code>slf4j<\/code>\u65e5\u5fd7\u3001<code>jackson<\/code>\u7684<code>json<\/code>\u89e3\u6790\u5668\u3001\u4ee5\u53ca<code>javax.mail<\/code><\/p>\n<pre><code class=\"language-xml\">&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.apache.httpcomponents&lt;\/groupId&gt;\n        &lt;artifactId&gt;httpclient&lt;\/artifactId&gt;\n        &lt;version&gt;4.5.14&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\n        &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\n        &lt;version&gt;2.14.2&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.sun.mail&lt;\/groupId&gt;\n        &lt;artifactId&gt;javax.mail&lt;\/artifactId&gt;\n        &lt;version&gt;1.6.2&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.slf4j&lt;\/groupId&gt;\n        &lt;artifactId&gt;slf4j-api&lt;\/artifactId&gt;\n        &lt;version&gt;1.7.36&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n        &lt;artifactId&gt;logback-classic&lt;\/artifactId&gt;\n        &lt;version&gt;1.2.11&lt;\/version&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;<\/code><\/pre>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">public class Office365MailTest {\n\n    private static final Logger LOGGER = LoggerFactory.getLogger(Office365MailTest.class);\n\n    private static final String SMTP_SERVER_ADDR = &quot;smtp.office365.com&quot;;\n    private static final int SMTP_SERVER_PORT = 587;\n\n    private static final String IMAP_SERVER_ADDR = &quot;outlook.office365.com&quot;;\n    private static final int IMAP_SERVER_PORT = 993;\n\n    private static final String USER_NAME = &quot;xxxx@xxxx.com&quot;;\n\n    private static final String MAIL_FROM = &quot;xxxx@xxxx.com&quot;;\n    private static final String MAIL_TO = &quot;xxxx@xxxx.com&quot;;\n\n    private static final String MAIL_SUBJECT = &quot;Test\u6d4b\u8bd5\u90ae\u4ef6\u6807\u9898&quot;;\n    private static final String MAIL_CONTENT = &quot;Test\u6d4b\u8bd5\u90ae\u4ef6\u6b63\u6587&quot;;\n\n    private static final String TENANT_ID = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx&quot;;\n    private static final String CLIENT_ID = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx&quot;;\n    private static final String CLIENT_SECRET = &quot;xxxxxxxxxxxxxxxxxxxxxxxxx&quot;;\n\n    public static void main(String[] args) throws Exception {\n\/\/        sendEmail(false);\n        recvMail(false);\n    }\n    private static void sendEmail(boolean debug) throws Exception {\n        String protocol = &quot;smtp&quot;;\n        Properties props = getBaseProperties(protocol, SMTP_SERVER_ADDR, SMTP_SERVER_PORT, debug);\n        final Session session = Session.getInstance(props);\n        try {\n            final Message message = new MimeMessage(session);\n            message.setRecipient(Message.RecipientType.TO, new InternetAddress(MAIL_TO));\n            message.setFrom(new InternetAddress(MAIL_FROM));\n            message.setSubject(MAIL_SUBJECT);\n            message.setText(MAIL_CONTENT);\n            message.setSentDate(new Date());\n            Transport transport = session.getTransport(protocol);\n            transport.connect(SMTP_SERVER_ADDR, USER_NAME, getAuthToken(TENANT_ID, CLIENT_ID, CLIENT_SECRET));\n            transport.send(message);\n        } catch (final MessagingException ex) {\n            LOGGER.error(&quot;\u90ae\u4ef6\u53d1\u9001\u9519\u8bef&quot;, ex);\n        }\n    }\n    private static void recvMail(boolean debug) throws Exception {\n        String protocol = &quot;imap&quot;;\n        Properties props = getBaseProperties(protocol, IMAP_SERVER_ADDR, IMAP_SERVER_PORT, debug);\n        props.put(&quot;mail.store.protocol&quot;, protocol);\n        String token = getAuthToken(TENANT_ID, CLIENT_ID, CLIENT_SECRET);\n        Session session = Session.getInstance(props);\n        Store store = session.getStore(protocol);\n        store.connect(IMAP_SERVER_ADDR, USER_NAME, token);\n        Folder folder = store.getFolder(&quot;INBOX&quot;); \/\/ \u8bfbinbox\n        folder.open(Folder.READ_WRITE);\n        Message[] messages = folder.getMessages();\n        for (Message message : messages) {\n            LOGGER.info(&quot;{}&quot;, message.getSubject());\n        }\n    }\n    private static Properties getBaseProperties(String protocol, String address, Integer port, boolean debug) {\n        Properties props = new Properties();\n        props.put(&quot;mail.&quot; + protocol + &quot;.host&quot;, address);\n        props.put(&quot;mail.&quot; + protocol + &quot;.port&quot;, port);\n        if (&quot;smtp&quot;.equals(protocol)) {\n            props.put(&quot;mail.&quot; + protocol + &quot;.starttls.enable&quot;, &quot;true&quot;);\n        } else {\n            props.put(&quot;mail.&quot; + protocol + &quot;.ssl.enable&quot;, &quot;true&quot;);\n        }\n        props.put(&quot;mail.&quot; + protocol + &quot;.ssl.protocols&quot;, &quot;TLSv1.2&quot;);\n        props.put(&quot;mail.&quot; + protocol + &quot;.auth&quot;, &quot;true&quot;);\n        props.put(&quot;mail.&quot; + protocol + &quot;.auth.mechanisms&quot;, &quot;XOAUTH2&quot;);\n        if (debug) { \/\/ \u8c03\u8bd5\u6a21\u5f0f\n            props.put(&quot;mail.debug&quot;, &quot;true&quot;);\n            props.put(&quot;mail.debug.auth&quot;, &quot;true&quot;);\n        }\n        return props;\n    }\n    private static String getAuthToken(String tenant, String clientId, String clientSecret) throws Exception {\n        CloseableHttpClient client = null;\n        CloseableHttpResponse loginResponse = null;\n        try {\n            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (cert, authType) -&gt; true).build();\n            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);\n            client = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();\n            HttpPost loginPost = new HttpPost(&quot;https:\/\/login.microsoftonline.com\/&quot; + tenant + &quot;\/oauth2\/v2.0\/token&quot;);\n            String scopes = &quot;https:\/\/outlook.office365.com\/.default&quot;;\n            String encodedBody = &quot;client_id=&quot; + clientId + &quot;&amp;scope=&quot; + scopes + &quot;&amp;client_secret=&quot; + clientSecret\n                    + &quot;&amp;grant_type=client_credentials&quot;;\n            loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED));\n            loginPost.addHeader(new BasicHeader(&quot;cache-control&quot;, &quot;no-cache&quot;));\n            loginResponse = client.execute(loginPost);\n            InputStream inputStream = loginResponse.getEntity().getContent();\n            Map&lt;String, String&gt; parsed = new ObjectMapper().readValue(inputStream, Map.class);\n            return parsed.get(&quot;access_token&quot;);\n        } finally {\n            HttpClientUtils.closeQuietly(client);\n            HttpClientUtils.closeQuietly(loginResponse);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u6536\u90ae\u4ef6\u6210\u529f\uff1a<\/p>\n<pre><code class=\"language-sh\">16:26:41.567 [main] INFO com.fugary.mail.Office365MailTest - test\u6807\u9898\n16:26:42.189 [main] INFO com.fugary.mail.Office365MailTest - test\n16:26:42.811 [main] INFO com.fugary.mail.Office365MailTest - \u6700\u65b0\u6d4b\u8bd5\u7248\u90ae\u4ef6\u53d1\u9001<\/code><\/pre>\n<h4>\u5e38\u89c1\u9519\u8bef<\/h4>\n<ol>\n<li>IMAP\u9519\u8bef<\/li>\n<\/ol>\n<blockquote>\n<p>javax.mail.MessagingException: A3 BAD User is authenticated but not connected.<br \/>\n;<br \/>\nnested exception is:<br \/>\ncom.sun.mail.iap.BadCommandException: A3 BAD User is authenticated but not connected.<\/p>\n<\/blockquote>\n<p>\u95ee\u9898\u4e00\u822c\u662f\u7528\u6237\u6ca1\u6709\u6743\u9650\uff0c\u53ef\u4ee5\u914d\u7f6e\u4e0b\u6743\u9650\u6216\u8005\u6362\u4e00\u4e2a\u6709\u6743\u9650\u7684\u7528\u6237\u3002<\/p>\n<ol start=\"2\">\n<li>SMTP\u9519\u8bef<\/li>\n<\/ol>\n<blockquote>\n<p>javax.mail.AuthenticationFailedException: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit <a href=\"https:\/\/aka.ms\/smtp_auth_disabled\">https:\/\/aka.ms\/smtp_auth_disabled<\/a> for more information. [SJ0PR03CA0292.namprd03.prod.outlook.com 2023-03-02T08:56:02.982Z 08DB1AFAC2F526FF]<\/p>\n<pre><code class=\"language-sh\">at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)\nat com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)\nat com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)\nat javax.mail.Service.connect(Service.java:366)\nat javax.mail.Service.connect(Service.java:246)\nat com.fugary.mail.Office365MailTest.sendEmail(Office365MailTest.java:66)\nat com.fugary.mail.Office365MailTest.main(Office365MailTest.java:50)<\/code><\/pre>\n<\/blockquote>\n<p>\u6ca1\u6709\u5f00\u542fSMTP\u5ba2\u6237\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u63d0\u5347\u5b89\u5168\u6027\uff0cOffice365\u5347\u7ea7\u4e86\u90ae\u4ef6\u5ba2\u6237\u7aef\u8ba4\u8bc1\u6a21\u5f0f\uff0c\u9700\u8981\u4f7f\u7528OAuth 2.0\u8ba4\u8bc1\uff0c\u5e9f\u5f03\u4ee5\u524d\u8001\u7684\u7528\u6237\u540d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[90,91,92,93,89],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/434"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=434"}],"version-history":[{"count":1,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}