Gmail Oauth 2 for smtp

Rajesh Kola 0 Reputation points
2025-03-29T06:18:20.99+00:00

I am working on integrating Gmail SMTP using OAuth2 in an ASP.NET Core Web API project. I am looking for a sample project or implementation guide that demonstrates how to configure Gmail's SMTP server with OAuth2 authentication in the context of an ASP.NET Core application.

Here are some details about what I have tried so far:

I have set up OAuth2 credentials in the Google Developer Console.

I am able to authenticate with Gmail but facing issues while sending emails using SMTP with OAuth2.

I have tried using the standard SMTP client library in ASP.NET Core but have been unable to establish a proper connection or send emails.

Can anyone share a sample project or guide on how to properly implement this functionality in ASP.NET Core Web API using Gmail SMTP with OAuth2?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andrew Velasquez 5 Reputation points
    2025-03-29T08:04:55.69+00:00

    Good Afternoon Rajesh,
    Not exactly what you wanted but here is the google documentation on using the oauth api with .net:
    https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

    Here is the smtp page with examples in Java and python on sending emails:
    https://developers.google.com/workspace/gmail/api/guides/sending#python

    If there is a specific error or issue you are facing please let me know and I will do my best to assist.

    Hope this helps. If you need further help on this, tag me in a comment. If the suggested response helped you resolve your issue, please 'Accept as answer', so that it can help others in the community looking for help on similar topics.

    0 comments No comments

  2. AgaveJoe 30,031 Reputation points
    2025-03-29T10:21:46.7233333+00:00

    I am working on integrating Gmail SMTP using OAuth2 in an ASP.NET Core Web API project.

    It's important to distinguish between Gmail SMTP and the Gmail API. Gmail SMTP doesn't support OAuth2, but the API does and is intended for programmatic email sending.

    Can anyone share a sample project or guide on how to properly implement this functionality in ASP.NET Core Web API using Gmail SMTP with OAuth2?

    You'll want to start with the Google OAuth developer documentation, it's really thorough and has ASP.NET libraries. Figuring out your OAuth client type is key. For web apps sending email, service accounts are often used, but to give you specific help, I'd need to see your code and understand how your application is set up.

    https://developers.google.com/identity/protocols/oauth2

    0 comments No comments

  3. Bruce (SqlWork.com) 74,531 Reputation points
    2025-03-29T16:24:49.37+00:00

    The SmtpClient class does not support oauth (as now required by google and Office) and there are currently no plans to add support. You will need to pick another email library. The docs recommend the open source mailkit.

    https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-9.0

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.