15 个问题
使用Java代码OAuth2.0认证方式进行outook服务器邮箱发件,出现202后在收件方没有收到任何邮件。
Erik John
40
信誉分
具体原因是通过后提示202状态码,但是收件方是没有收到任何的邮件。
追踪失败原因
分配的权限
下方是Java代码
public class SendEmail {
private static final String CLIENT_ID = "**";
private static final String CLIENT_SECRET = "**";
private static final String TENANT_ID = "****";
private static final String AUTHORITY = "https://login.microsoftonline.com/" + TENANT_ID;
private static final String GRAPH_API_URL = "https://graph.microsoft.com/v1.0/users/****/sendMail";
public static void main(String[] args) {
try {
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
CLIENT_ID,
ClientCredentialFactory.createFromSecret(CLIENT_SECRET))
.authority(AUTHORITY)
.build();
Set<String> scopes = new HashSet<>(Collections.singletonList("https://graph.microsoft.com/.default"));
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(scopes)
.build();
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
IAuthenticationResult result = future.join();
if (result != null) {
sendEmail(result.accessToken());
} else {
System.out.println("Failed to acquire access token.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sendEmail(String accessToken) throws Exception {
String toEmail = "*****";
String subject = "Test Email";
JSONObject bodyContent = new JSONObject().put("content", "Hello, this is a test email sent from Java using Microsoft Graph API.")
.put("contentType", "Text");
JSONObject messageBody = new JSONObject().put("body", bodyContent);
JSONObject toRecipient = new JSONObject().put("emailAddress", new JSONObject().put("address", toEmail));
JSONObject message = new JSONObject().put("message", new JSONObject()
.put("subject", subject)
.put("body", bodyContent)
.put("toRecipients", new JSONArray().put(toRecipient)));
URL url = new URL(GRAPH_API_URL);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer " + accessToken);
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
try (OutputStream os = con.getOutputStream()) {
byte[] input = message.toString().getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = con.getResponseCode();
System.err.println("### O365Email 请求结果 code:" + responseCode);
BufferedReader in;
if (responseCode == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
System.err.println("### O365Email 请求结果 code:" + responseCode);
}
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
if (responseCode == 200) {
Map<String, String> map = JSON.parseObject(response.toString(), HashMap.class);
}
System.err.println("### 响应内容: " + response.toString());
}
}
Windows 商业版 | Windows 365 商业版
Outlook | Windows | 经典 Outlook for Windows | 商业版
Windows 商业版 | 面向 IT 专业人士的 Windows 客户端 | 用户体验 | 其他
登录以回答