在用java语言使用microsoft.graph 发送邮件时使用的是由世纪互联运营的中国服,根据https://microsoftgraph.chinacloudapi.cn/.default获取token 但是在发送邮件时一直提示
Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: InvalidAuthenticationToken
Error message: Access token validation failure. Invalid audience.
查找相关资料后(https://zhuanlan.zhihu.com/p/621741681) 发现可能是因为地址设置的不匹配造成的 但是在代码层级怎么去设置发送邮箱地址 请各位解答一下
相关代码:
private void auth() {
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.tenantId(tenantId)
.clientSecret(clientSecret)
.authorityHost(AzureAuthorityHosts.AZURE_CHINA)
.build();
//https://microsoftgraph.chinacloudapi.cn/.default
authProvider = new TokenCredentialAuthProvider(
Arrays.asList("https://microsoftgraph.chinacloudapi.cn/.default")
, clientSecretCredential
);
}
public Message sendMail(String sender, List<String> recipients, String subject, String body, List<FileAttachment> files) throws Exception {
// Ensure client isn't null
if (graphClient == null) {
throw new Exception("Graph has not been initialized for user auth");
}
// Create a new message
Message message = new Message();
message.subject = subject;
message.body = new ItemBody();
message.body.content = body;
message.body.contentType = BodyType.HTML;
//收件人
if (recipients != null && recipients.size() > 0) {
message.toRecipients = new ArrayList<>();
for (String item : recipients) {
Recipient toRecipient = new Recipient();
toRecipient.emailAddress = new EmailAddress();
toRecipient.emailAddress.address = item;
message.toRecipients.add(toRecipient);
}
}
//附件
message = addAttarchment(message, files);
// Send the message
graphClient
.users(sender)
.sendMail(UserSendMailParameterSet.newBuilder()
.withMessage(message)
.build())
.buildRequest()
.post();
return message;
}
相关帮助链接 :https://blog.csdn.net/hanjun0612/article/details/136319440?spm=1001.2014.3001.5501
https://blog.csdn.net/hanjun0612/article/details/136806768?spm=1001.2014.3001.5501