我正在使用Microsoft Identity的OAuth 2.0支持来使用Microsoft Graph发送电子邮件。
创建了一个个人电子邮件账户,名为[email protected]。使用该账户登录Azure AD并在那里创建一个租户。使用了 ClientCredentialProvider
(来自msgraph-sdk-auth-java)作为授权者试图给自己发送邮件.步骤。
以下是我得到的错误。
POST microsoft.graph.sendMail SdkVersion : graph-javav1.5.0 Authorization : Bearer _xv1yPye...
{
"message": {
"subject": "Test",
"body": {
"contentType": "text",
"content": "The new cafeteria is open bujji."
},
"toRecipients": [
{
"emailAddress": {
"address": "[email protected]"
}
}
]
},
"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 = "[email protected]";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
graphClient.me()
.sendMail(message, true)
.buildRequest()
.post();
}
我猜你想使用Microsoft Graph API从你的个人帐户电子邮件发送电子邮件。[email protected]
.
但当您使用此帐户登录到 Azure AD 并创建一个租户,并使用 ClientCredentialProvider
在你的代码中,该账户将被视为你的租户的工作账户(不是个人账户)。
因此,当工作账户想要发送邮件时,将需要O365订阅的Exchange在线许可证。你没有O365订阅与Exchange在线许可证。这就是为什么你会出现这个错误。The tenant for tenant guid \u002706841624-5828-4382-b0a0-XXXXXX87b08f\u0027 does not exist.
如果你想从你的个人账户发送电子邮件,就没有必要创建一个AAD租户。而你应该使用 授权码提供者 而非 客户端证书提供者. 另一个问题是,个人账户需要委托权限,而不是基于应用程序的权限。发送邮件的权限. 创建一个应用程序并授予权限,在 Graph > Delegated > Mail.Send.
请注意,它可能需要的范围是 https://graph.microsoft.com/mail.send
而不是 https://graph.microsoft.com/.default
.
谢谢你的帮助,艾伦。我能够从我的outlook帐户发送和接收电子邮件。使用 授权码提供者1. 登录到Azure AD在 "Application from Personl account "中创建一个Application.2.给予权限。Graph > Delegated > Mail.Send.3. 提供的重定向网址为 http:/localhost:8080muapp。".记下所有的appId,创建一个秘密密钥.4.现在点击下面的URL,并输入详细信息。
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=40fcd457-1807-49e3-8bce-XXXXXX40ca194&response_type=code&redirect_uri=https://localhost/myapp/&response_mode=query&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.send%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read&state=12345
5. 获取代码.这段代码我们需要通过在 授权码提供者.6.范围"https:/graph.microsoft.commail.send。"7. 权力"https:/login.microsoftonline.comconsumers。"
我有一个问题,每次发送电子邮件时,我都要获取代码。是否有任何方式,这将有到期日等吗?