我尝试通过office 365的smtp发送邮件。我在azure中为本机客户端设置了应用程序注册并设置了权限SMTP.Send。但是当我连接时,我已经能够获得令牌,但发送不起作用。
在我阅读的所有其他文章中,我需要设置“https://outlook.office.com/SMTP.Send”,但我没有找到任何方法在 Azure Active 中我的应用程序的 API 权限中设置它目录。我只能设置 Graph 'https://graph.microsoft.com/SMTP.Send' 或 Exchange 'https://ps.outlook.com/Mail.Send.All'。
我是否忽略了什么?
如果有人能给我提示,那就太好了?
或者也许对于本地客户端来说这是不可能的?
这是我获取访问凭据的代码:
var cca = ConfidentialClientApplicationBuilder
.Create("---")
.WithClientSecret("----")
.WithTenantId("----").Build();
var ewsScopes = new List<string>() { "https://outlook.office365.com/.default" };
var authResult = cca.AcquireTokenForClient(ewsScopes).ExecuteAsync().GetAwaiter().GetResult();
string xoauthKey = OAuth2.GetXOAuthKeyStatic(fromAddress, authResult.AccessToken);
// content of xoauthKey:
// user=my-testmailAccount�auth=Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IjR....��
我使用 mailbee 或 maikit 来实际发送邮件。
这是 smtp 日志:
[15:41:09.26] [INFO] Assembly version: 11.0.0 build 569 for .NET 4.5.
[15:41:09.26] [INFO] Will test sending mail message.
[15:41:09.32] [INFO] Will resolve host "smtp.office365.com".
[15:41:09.33] [INFO] Host "smtp.office365.com" resolved to IP address(es) 52.97.151.146, 52.98.208.82, 52.98.212.194, 52.97.137.66.
[15:41:09.33] [INFO] Will connect to host "smtp.office365.com" on port 587.
[15:41:09.35] [INFO] Socket connected to IP address 52.97.151.146 on port 587.
[15:41:09.38] [RECV] 220 AM6P194CA0020.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 16 May 2022 13:41:09 +0000\r\n
[15:41:09.38] [INFO] Connected to mail service at host "smtp.office365.com" on port 587 and ready.
[15:41:09.38] [INFO] Will send Hello command (HELO or EHLO).
[15:41:09.38] [SEND] EHLO [10.0.0.4]\r\n
[15:41:09.40] [RECV] 250-AM6P194CA0020.outlook.office365.com Hello [20.113.132.65]\r\n250-SIZE 157286400\r\n250-PIPELINING\r\n250-DSN\r\n250-ENHANCEDSTATUSCODES\r\n250-STARTTLS\r\n250-8BITMIME\r\n250-BINARYMIME\r\n250-CHUNKING\r\n250 SMTPUTF8\r\n
[15:41:09.41] [INFO] SMTP Hello completed.
[15:41:09.41] [INFO] Notify server that we are ready to start TLS/SSL negotiation.
[15:41:09.41] [SEND] STARTTLS\r\n
[15:41:09.43] [RECV] 220 2.0.0 SMTP server ready\r\n
[15:41:09.43] [INFO] Will start TLS/SSL negotiation sequence.
[15:41:09.47] [INFO] TLS/SSL negotiation completed.
[15:41:09.47] [INFO] Will send Hello command (HELO or EHLO).
[15:41:09.48] [SEND] EHLO [10.0.0.4]\r\n
[15:41:09.49] [RECV] 250-AM6P194CA0020.outlook.office365.com Hello [20.113.132.65]\r\n250-SIZE 157286400\r\n250-PIPELINING\r\n250-DSN\r\n250-ENHANCEDSTATUSCODES\r\n250-AUTH LOGIN XOAUTH2\r\n250-8BITMIME\r\n250-BINARYMIME\r\n250-CHUNKING\r\n250 SMTPUTF8\r\n
[15:41:09.49] [INFO] SMTP Hello completed.
[15:41:09.49] [INFO] Will login as "my-testmailAccount".
[15:41:09.50] [INFO] Will try SASL XOAUTH2 authentication method.
[15:41:09.50] [SEND] AUTH XOAUTH2\r\n
[15:41:09.51] [RECV] 334 \r\n
[15:41:09.52] [SEND] ********\r\n
[15:41:15.15] [RECV] 535 5.7.3 Authentication unsuccessful [AM6P194CA0020.EURP194.PROD.OUTLOOK.COM]\r\n
[15:41:15.17] [INFO] Will disconnect from host "smtp.office365.com".
[15:41:15.17] [INFO] Disconnected from host "smtp.office365.com".
[15:41:15.17] [INFO] Error: The server has rejected authentication data sent by the client. The server responded: 535 5.7.3 Authentication unsuccessful [AM6P194CA0020.EURP194.PROD.OUTLOOK.COM].
I enabled a lot of permissions, but nothing helped.