如何在2022年5月30日之后与Google一起使用MailKit?
到目前为止,我一直很高兴地连接到我的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日以来,这是不再可能的,因为对“较不安全的应用”的支持是禁用:
从2022年5月30日开始,Google不再支持使用第三方应用程序或设备的使用,这些应用程序要求您仅使用用户名和密码登录Google帐户。
现在如何将MailKit与Gmail一起使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
停用较不安全的应用程序的停用使您无法直接与您的用户名和 password 登录,但它并不能阻止您能够为您的申请。现在,您不会使用Google密码登录,而是使用为特定应用程序生成的密码登录。
该解决方案很简单,不需要太多更改:
打开2步验证在您的Google帐户中。此步骤是必需的,因为Google仅允许在启用了2步验证的帐户上生成密码。
转到生成应用程序密码( https://myaccount.google.com/apppasswords )和< a href =“ https://support.google.com/mail/answer/185833?hl = en-gb” rel =“ noreferrer”>为您的应用程序生成密码
只需使用您的gmail用户名(
)和
c#
应用程序中生成的密码。The deactivation of less secure applications prevents you from being able to log in directly with your username and password, but it does not prevent you from being able to generate a specific password for your application. Now, instead of logging in with your google password, you'll log in with a password that you generate for your specific app.
The solution is simple and does not require much change:
Turn on 2-Step Verification in your google account. This step is required as Google only allows generating passwords for apps on accounts that have 2-Step Verification enabled.
Go to generate apps password (https://myaccount.google.com/apppasswords) and generate a password for your app.
Simply use your gmail username (
[email protected]
) and the password generated in yourc#
application.您只需要将应用程序密码选项与Gmail一起使用。
今天处理这个。只需转到Gmail帐户,然后转到管理您的Google帐户&gt;安全。
从这里启用2因子身份验证,然后完成后,您将看到“应用程序密码”选项出现在2步验证选项下。单击此,命名要使用的设备,然后复制&amp;粘贴将您输入的生成密码代替您使用的旧密码。
我现在为我们的办公室打印机做到了这一点。我必须自动向所有人提供时间表的Python脚本。
You just need to use the App Password option with gmail.
Dealt with this today. Just go to the gmail account, then go to Manage Your Google Account > Security.
From here enable 2-factor authentication, then once you have done You will see the "App passwords" option appear under the 2-step verification option. Click on this, name the device that you want to use, and then copy & paste the generated password that you are given into your code in place of the old password that you were using.
I've done this now for our office printer & the python script that I had to automatically deliver timesheets to everyone.
访问Google帐户设置。
从左侧选择“安全性”。
在“ Google登录到Google”部分下,单击“ 2步验证”,然后按照提示启用。
启用两步验证后,单击“应用程序密码”。可能会要求您重新输入密码。
在“ App Passwords”部分中,在“ Select App”下拉列表中选择“邮件”,然后在“选择设备”下拉列表中选择您使用的设备,然后单击“生成”。
Google将为您生成新的16个字符密码。您将在代码中使用它,而不是常规的Google密码。
Visit the Google Account settings.
Select “Security” from the left-hand side.
Under the “Signing in to Google” section, click on “2-Step Verification” and follow the prompts to enable it.
After enabling two-step verification, click on “App Passwords”. You may be asked to re-enter your password.
In the App passwords section, select “Mail” in the “Select app” dropdown, and select the device you’re using in the “Select device” dropdown, then click “Generate”.
Google will generate a new 16-character password for you. You will use this in your code, instead of your regular Google password.
https://medium.com/@abhinandkr56/how-to-send-emails-using-net-core-mailkit-and-googles-smtp-server-6521827c4198