Home > Software engineering >  How to use MailKit with Google after May 30, 2022?
How to use MailKit with Google after May 30, 2022?

Time:06-09

Up to this point I was happily connecting to my gmail with the method similar to this one below:

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;
}

After May 30, 2022 this is no longer possible as support for 'less secure apps' was enter image description here

  1. Simply use your gmail username ([email protected]) and the password generated in your c# application.
  • Related