我正在使用Microsoft Identity的OAuth 2.0支持来使用Microsoft Graph发送电子邮件。
创建一个个人电子邮件帐户,名称为XXXX@outlook.com。使用此帐户,我登录到Azure AD并在其中创建一个租户。使用ClientCredentialProvider
(来自msgraph-sdk-auth-java)作为尝试向自己发送电子邮件的授权者。
步骤:
以下是我得到的错误:
POST microsoft.graph.sendMail SdkVersion:graph-java / v1.5.0授权:Bearer _xv1yPye ...
{
"message": {
"subject": "Test",
"body": {
"contentType": "text",
"content": "The new cafeteria is open bujji."
},
"toRecipients": [
{
"emailAddress": {
"address": "xxxxx@outlook.com"
}
}
]
},
"saveToSentItems": true
}401: UnauthorizedStrict-Transport-Security: max-age=31536000Cache-Control: privatex-ms-ags-diagnostic: {
"ServerInfo": {
"DataCenter": "South India",
"Slice": "SliceC",
"Ring": "3",
"ScaleUnit": "001",
"RoleInstance": "AGSFE_IN_1"
}
}client-request-id: 01565263-11b4-45f7-b089-06f57fdd8241request-id: 2e0cac3b-dc32-4dab-bb30-769590fc156eContent-Length: 361Date: Tue,
16Jun202007: 14: 42GMTContent-Type: application/json{
"error": {
"code": "OrganizationFromTenantGuidNotFound",
"message": "The tenant for tenant guid \u002706841624-5828-4382-b0a0-XXXXXX87b08f\u0027 does not exist.",
"innerError": {
"requestId": "01565263-11b4-45f7-b089-06f57fdd8241",
"date": "2020-06-16T07:14:43",
"request-id": "2e0cac3b-dc32-4dab-bb30-769590fc156e"
}
}
}
private static void sendEmail() {
ClientCredentialProvider authProvider = new ClientCredentialProvider(
"fb7f0ecc-b498-XXXX-XXXX-b016f252ea7d",
Arrays.asList("https://graph.microsoft.com/.default"),
"8-rpF8sOwV.CWF~7gK.XXXXXXXX.SSScxj0",
"06841624-5828-4382-b0a0-XXXXXXe87b08f",
NationalCloud.Global);
IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
Message message = new Message();
message.subject = "Test";
Ite * mBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "The new cafeteria is open.";
message.body = body;
LinkedList < Recipient > toRecipientsList = new LinkedList < Recipient > ();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "xxxxx@outlook.com";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
graphClient.me()
.sendMail(message, true)
.buildRequest()
.post();
}
答案 0 :(得分:0)
我猜您想使用Microsoft Graph API从您的个人帐户电子邮件XXXX@outlook.com
发送电子邮件。
但是,当您使用此帐户登录Azure AD并创建租户,并在代码中使用ClientCredentialProvider
时,该帐户将被视为租户的工作帐户(而非个人帐户)。
因此,当工作帐户要发送电子邮件时,将需要O365订阅的Exchange在线许可证。您没有使用Exchange在线许可证进行O365订阅。这就是为什么出现此错误的原因:The tenant for tenant guid \u002706841624-5828-4382-b0a0-XXXXXX87b08f\u0027 does not exist.
如果要通过个人帐户发送电子邮件,则无需创建AAD租户。并且您应该使用Authorization code provider而不是Client credentials provider。另一件事是,个人帐户需要基于Send mail permissions的委派权限而不是应用程序权限。创建一个应用程序并在图形>委托> Mail.Send 中授予权限。
请注意,它可能要求范围为https://graph.microsoft.com/mail.send
,而不是https://graph.microsoft.com/.default
。
答案 1 :(得分:0)
谢谢艾伦(Allen)的帮助。我可以通过我的Outlook帐户发送和接收电子邮件。使用授权代码提供商
1.登录到Azure AD,在“来自Personl帐户的应用程序”中创建一个应用程序。
2.授予权限图形>委托>邮件发送。
3.提供的重定向URL为“。注意所有appId,创建一个秘密密钥。
4.现在,点击下面的URL并添加详细信息
cat > delete_stuck_ns.sh << "EOF"
#!/usr/bin/env bash
function delete_namespace () {
echo "Deleting namespace $1"
kubectl get namespace $1 -o json > tmp.json
sed -i 's/"kubernetes"//g' tmp.json
kubectl replace --raw "/api/v1/namespaces/$1/finalize" -f ./tmp.json
rm ./tmp.json
}
TERMINATING_NS=$(kubectl get ns | awk '$2=="Terminating" {print $1}')
for ns in $TERMINATING_NS
do
delete_namespace $ns
done
EOF
chmod +x delete_stuck_ns.sh
5。获取代码。我们需要将此代码传递给授权代码提供者。 6.范围“ http://localhost:8080/muapp” 7.权限“ https://graph.microsoft.com/mail.send”
每次发送电子邮件时我都有一个问题,我必须获取代码。有什么办法可以将到期日等吗??