到目前为止,我很高兴使用类似于以下的方法连接到我的 Gmail 帐户:
public async Task<IEnumerable<MimeMessage>> GetMessagesAsync()
{
using var imapClient = new MailKit.Net.Imap.ImapClient();
var secureSocketOptions = SecureSocketOptions.Auto;
if (useSsl) secureSocketOptions = SecureSocketOptions.SslOnConnect;
await imapClient.ConnectAsync(host, port, secureSocketOptions);
await imapClient.AuthenticateAsync(login, password);
await imapClient.Inbox.OpenAsync(FolderAccess.ReadOnly);
var uids = await imapClient.Inbox.SearchAsync(SearchQuery.All);
var messages = new List<MimeMessage>();
foreach (var uid in uids)
messages.Add(await imapClient.Inbox.GetMessageAsync(uid));
imapClient.Disconnect(true);
return messages;
}
自 2022 年 5 月 30 日起,这不再可能,因为对“不太安全的应用程序”的支持已取消disabled https://support.google.com/accounts/answer/6010255?hl=en:
为了确保您的帐户安全,从 2022 年 5 月 30 日起,Google 不再支持使用要求您仅使用用户名和密码登录 Google 帐户的第三方应用或设备。
现在如何将 Mailkit 与 Gmail 结合使用?
停用不太安全的应用程序会阻止您直接使用您的帐户登录username and password,但这并不妨碍您为您的应用程序生成特定的密码。现在,您将使用为特定应用程序生成的密码登录,而不是使用 Google 密码登录。
解决方案很简单,不需要太多改变:
-
开启两步验证 https://support.google.com/accounts/answer/185839在你的谷歌帐户中。这一步是required因为 Google 只允许为启用了两步验证的帐户上的应用程序生成密码。
-
转到生成应用程序密码(https://myaccount.google.com/apppasswords https://myaccount.google.com/apppasswords) and 生成密码 https://support.google.com/mail/answer/185833?hl=en-GB为您的应用程序。
-
只需使用您的 Gmail 用户名 ([email protected] /cdn-cgi/l/email-protection
)和您的密码中生成的c#
应用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)