Up to this point I was happily connecting to my Gmail account with a method similar to this:
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;
}
Since May 30, 2022, this is no longer possible as support for 'less secure apps' was disabled:
To help keep your account secure, from May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
How do I use Mailkit with Gmail now?