Using App Passwords for authenticating to Gmail via SMTP, IMAP or POP3 in .NET

  |   Martin Vobr

Using your Gmail username and password for authenticating to Gmail from third-party applications via IMAP, POP3 or SMTP is no longer possible. For security reasons, you have to either use a restricted App Password, or a more secure authentication method such as OAuth.

This article shows how to obtain App Passwords for your application and use then in C# code.

What are App Passwords

App Passwords are 16-digit passcodes that give third-party applications access to a subset of your Google Account. They are only available for accounts that have 2-Step Verification turned on.

Signing in using your email and the app password will allow the application to access a limited set of Google account functionality, such as accessing your mailbox using IMAP or POP3 protocols, or sending e-mail using SMTP. But it will block potentially malicious third-party apps from accessing other functionality of your Google account, such as configuring the Security settings or accessing your purchases in Google Play store.

Configuring Google Account / Gmail

Enable 2-Step Verification

  • Go to Google Account.
  • Go to "Security" on the left menu.
  • Make sure that "2-Step Verification" is set to "On". Turn On 2-Step Verification

Generate App Password

  • Select "App passwords"
  • Fill out the form and click "Generate".

Generated app password

Write down the generated password, because you will not be able to display it again. Be aware that spaces in the form are only for better readability. Make sure that you are using the password WITHOUT SPACES.

Enable POP3 and IMAP in Gmail

IMAP and POP3 protocols are disabled by default. See instructions on how to enable POP3 and IMAP in Gmail.

Sample code - using app passwords in C# applications

The following code uses the Rebex Secure Mail library. To use it, download the library or add a NuGet reference.

Connect to Gmail SMTP server using C#

using (var smtp = new Smtp())
    // connect and use the App Password instead of the main Google account password
    smtp.Connect("", 465, SslMode.Implicit);
    smtp.Login("", "haiqdcoiefewedxq");

    // do something ...


Connect to Gmail POP3 server using C#

using (var pop3 = new Pop3())
    // connect and use the App Password instead of the main Google account password
    pop3.Connect("", 995, SslMode.Implicit);
    pop3.Login("", "haiqdcoiefewedxq");

    // do something ...


Connect to Gmail IMAP server using C#

using (var imap = new Imap())
    // connect and use the App Password instead of the main Google account password
    imap.Connect("", 993, SslMode.Implicit);
    imap.Login("", "haiqdcoiefewedxq");

    // do something ...


See also