使用微軟國際版的看:javamail微軟郵箱imap協(xié)議使用oauth2.0認(rèn)證–微軟郵箱國際版
一、賬號授權(quán)
imap協(xié)議使用oauth2.0身份認(rèn)證,必須先對帳號進(jìn)行授權(quán),授權(quán)方式可以看我之前的文章:微軟郵箱oath2協(xié)議下,使用imap協(xié)議時對賬號授權(quán),如果不進(jìn)行授權(quán),即使認(rèn)證通過了,但是獲取郵件相關(guān)文件夾時,會報錯:A3 BAD User is authenticated but not connected.;
具體報錯如下:文章來源:http://www.zghlxwxcb.cn/news/detail-848479.html
二、直接上代碼
package test;
import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
public class TestImapAndOauth2By21V {
public static void main(String[] args) {
String mailAddress = "郵箱賬號";
String tenant_id = "租戶id";
String client_id = "應(yīng)用id";
String client_secret = "密鑰的值";//注意不是密鑰的id
String scope = "https://partner.outlook.cn/.default";
String url = "https://login.partner.microsoftonline.cn/" + tenant_id + "/oauth2/v2.0/token";
org.apache.commons.httpclient.HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("accept", "*/*");//固定參數(shù)
postMethod.addRequestHeader("connection", "Keep-Alive");//固定參數(shù)
postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=GBK");//固定參數(shù)
//必須設(shè)置下面這個Header
postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");//固定參數(shù)
//添加請求參數(shù)
postMethod.addParameter("grant_type", "client_credentials");//固定參數(shù)
postMethod.addParameter("client_id", client_id);
postMethod.addParameter("client_secret", client_secret);
postMethod.addParameter("scope", scope);
String token = "";
try {
int code = httpClient.executeMethod(postMethod);
String resBody = postMethod.getResponseBodyAsString();
if (code == 200) {
Map<String, String> map = JSON.parseObject(resBody, Map.class);
token = map.get("access_token");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
try {
String protocol = "imap";
int port = 993;
String host = "partner.outlook.cn";
Properties properties = new Properties();
properties.setProperty("mail." + protocol + ".ssl.enable", "true");
properties.setProperty("mail." + protocol + ".auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(properties);
//session.setDebug(true);
Store store = session.getStore(protocol);
store.connect(host, port, mailAddress, token);
System.out.println("測試通過");
Folder inboxFolder = getInboxFolder(store);
Message[] messages = inboxFolder.getMessages();
System.out.println(messages.length);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("不通過");
}
}
public static Folder getInboxFolder(Store store) throws Exception {
Folder folder = null;
if(store != null) {
folder = store.getDefaultFolder();
folder = folder.getFolder("INBOX");
if(!folder.isOpen()) {
folder.open(Folder.READ_ONLY);
}
}
return folder;
}
}
其實和國際版的比較,也就scope 、url 還有host 這三個參數(shù)不一樣,其他的都一樣文章來源地址http://www.zghlxwxcb.cn/news/detail-848479.html
到了這里,關(guān)于javamail微軟郵箱imap協(xié)議使用oauth2.0認(rèn)證--微軟郵箱中國版(21v世紀(jì)互聯(lián)運營的微軟郵箱)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!