如何使用MailKit发送Office 365电子邮件?

huangapple go评论57阅读模式
英文:

How to send email for Office 365 using MailKit?

问题

I looked at solving this issue in so many ways. Here I shall discuss what I was able to achieve. I have successfully connected to my office 365 account and can read from the inbox:

我尝试了多种方法来解决这个问题。在这里,我将讨论我能够实现的内容。我已成功连接到我的Office 365帐户并可以读取收件箱:

var oauth2 = new SaslMechanismOAuth2(mail, accessTokenModel.access_token);
await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync(oauth2);
await client.Inbox.OpenAsync(folderAccess)
在对客户端进行身份验证后,我可以成功读取收件箱:

var messages = new List();
var uids = await client.Inbox.SearchAsync(MailKit.Search.SearchQuery.All);

foreach (var uid in uids) {
messages.Add(await client.Inbox.GetMessageAsync(uid));
}

However, when I try to send an email I get a 403 error, below is the code I am using:

但是,当我尝试发送电子邮件时,我收到了403错误,以下是我使用的代码:

var ews = new ExchangeService();
ews.Credentials = credentials;
ews.AutodiscoverUrl(, RedirectionCallback);
ews.ImpersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, );
var email = new EmailMessage(ews);
email.Subject = "My first test";
email.From = new EmailAddress();
email.ToRecipients.Add(new EmailAddress("Mrs. Chanandler Bong", "test@gmail.com"));
email.Body = new MessageBody(BodyType.HTML, "Hey");
email.Send();
When I execute this code I get a 403 error:

当我执行此代码时,我收到403错误:

The request failed. The remote server returned an error: (403) Forbidden.

请求失败。远程服务器返回错误:(403)禁止。

Below are the permissions granted on the App registered in Azure:

以下是在Azure注册的应用程序上授予的权限:

如何使用MailKit发送Office 365电子邮件?

Can somebody assist?

有人能帮忙吗?

英文:

I looked at solving this issue in so many ways. Here I shall discuss what I was able to achieve. I have successfully connected to my office 365 account and can read from the inbox:

 var oauth2 = new SaslMechanismOAuth2(mail, accessTokenModel.access_token);
 await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect);
 await client.AuthenticateAsync(oauth2);
 await client.Inbox.OpenAsync(folderAccess)

On authenticating the client I could then read the inbox successfully:

var messages = new List<MimeMessage>();                
var uids = await client.Inbox.SearchAsync(MailKit.Search.SearchQuery.All);

foreach (var uid in uids) { 
    messages.Add(await client.Inbox.GetMessageAsync(uid));
}

However, when I try to send an email I get 403 error, below is the code I am using:

 var ews = new ExchangeService();
 ews.Credentials = credentials;
 ews.AutodiscoverUrl(<authemail>, RedirectionCallback);
 ews.ImpersonatedUserId =
 new ImpersonatedUserId(ConnectingIdType.SmtpAddress, <authemail>);
 var email = new EmailMessage(ews);
 email.Subject = "My first test";
 email.From = new EmailAddress(<authemail>);
 email.ToRecipients.Add(new EmailAddress("Mrs. Chanandler Bong", "test@gmail.com"));
 email.Body = new MessageBody(BodyType.HTML, "Hey");
 email.Send();

When I execute this code I get a 403 error:

The request failed. The remote server returned an error: (403) Forbidden.

Below are the permissions granted on the App registered in Azure:

如何使用MailKit发送Office 365电子邮件?

Can somebody assist.

答案1

得分: 1

以下是您要翻译的内容:

答案很简单,不要使用IMapClient...原因是IMAP仅用于访问邮箱,要发送电子邮件,您需要使用SMTP。截止到2022年12月,您不能在租户中重新启用基本身份验证。然而,这个视频演示了如何获取访问令牌并使用它。只需将其与SMTP客户端一起使用:

using (var client = new SmtpClient())
{
    var oauth2Smtp = new SaslMechanismOAuth2(mail, accessToken);
    await client.ConnectAsync(host, smtpPort, SecureSocketOptions.StartTls);
    await client.AuthenticateAsync(oauth2Smtp);
    await client.SendAsync(message);
}
英文:

The answer here is very simple, don't use IMapClient... The reason being IMAP is only used for accessing the mailbox, for sending emails you need to use SMTP which as of December 2022 you can't re-enable Basic authentication in your tenant. However, this video demonstrates how to get access token and use it. Just use it with a SMTP client:

using (var client = new SmtpClient())
{
    var oauth2Smtp = new SaslMechanismOAuth2(mail, accessToken);
    await client.ConnectAsync(host, smtpPort, SecureSocketOptions.StartTls);
    await client.AuthenticateAsync(oauth2Smtp);
    await client.SendAsync(message);
}

huangapple
  • 本文由 发表于 2023年5月18日 02:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定