Pawel
Pawel

Reputation: 1297

How to use MailKit with Google after May 30, 2022?

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?

Upvotes: 16

Views: 11288

Answers (3)

Yelisei L
Yelisei L

Reputation: 1

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.

public async Task SendEmailAsync(string email, string subject, string message)
{
   var emailMessage = new MimeMessage();

      emailMessage.From.Add(new MailboxAddress("Администрация сайта", "[email protected]"));
      emailMessage.To.Add(new MailboxAddress("", email));
      emailMessage.Subject = subject;
      emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html)
      {
          Text = message
      };

      using (var client = new SmtpClient())
      {
          await client.ConnectAsync("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
          await client.AuthenticateAsync("[email protected]", "app code");
          await client.SendAsync(emailMessage);

          await client.DisconnectAsync(true);
      }
  }

https://medium.com/@abhinandkr56/how-to-send-emails-using-net-core-mailkit-and-googles-smtp-server-6521827c4198

Upvotes: 0

Jaime
Jaime

Reputation: 1631

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:

  1. 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.

  2. Go to generate apps password (https://myaccount.google.com/apppasswords) and generate a password for your app.

    enter image description here

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

Upvotes: 34

kevinvi8
kevinvi8

Reputation: 321

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.

Upvotes: 2

Related Questions