Sending email via Gmail in .NET: 4 approaches with C# code

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why sending email via Gmail in .NET is a headache (and how to avoid it)

\n\n

If you've ever tried to set up sending emails via Gmail in your .NET application, you know: this is a task that looks simple but turns into a quest. You write 10 lines of code, run it — and get an SmtpException with a mysterious message about an "unsafe application". Sound familiar?

\n\n

On Stack Overflow, the question "Sending email in .NET through Gmail" has gathered thousands of views and hundreds of answers. People have been struggling with this problem for years. In this article, we'll break down 4 real approaches in C#, from classics to modern solutions, and figure out which one is worth using in 2025.

\n\n

Spoiler: the code you found in a blog from 2015 most likely no longer works. Google is constantly tightening security policies. But don't worry — we'll show you working options.

\n\n

Approach 1: Classic SmtpClient (and why it's outdated)

\n\n

The most obvious way is to use the built-in SmtpClient from System.Net.Mail. Here's what it looked like in the "golden days":

\n\n
using System.Net;\nusing System.Net.Mail;\n\nvar message = new MailMessage("your@gmail.com", "recipient@example.com")\n{\n    Subject = "Test email from .NET",\n    Body = "Hello! This is a test."\n};\n\nusing var client = new SmtpClient("smtp.gmail.com", 587)\n{\n    Credentials = new NetworkCredential("your@gmail.com", "your-password"),\n    EnableSsl = true\n};\n\nclient.Send(message);
\n\n

In 2010-2015, this worked. But today, Google has blocked login with a regular password for most accounts. You will need:

\n
    \n
  • App Password — but only with two-factor authentication enabled
  • \n
  • Or allow "access to less secure apps" — Google has already removed this option
  • \n
\n\n

Disadvantages of this approach:

\n
    \n
  • SmtpClient is marked as Obsolete in .NET 5+
  • \n
  • Does not support modern authentication protocols (OAuth 2.0)
  • \n
  • App Password does not last forever — it needs to be updated manually
  • \n
\n\n

When to use: only for quick prototypes or internal tools where security is not critical.

\n\n

Approach 2: MailKit — modern and powerful

\n\n

If you want a production-grade solution, use the MailKit library by the Mono developer. It not only works but is actively developed.

\n\n

First, install the package:

\n
dotnet add package MailKit
\n\n

Now the code using App Password:

\n\n
using MailKit.Net.Smtp;\nusing MimeKit;\n\nvar message = new MimeMessage();\nmessage.From.Add(new MailboxAddress("Your Name", "your@gmail.com"));\nmessage.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));\nmessage.Subject = "Email via MailKit";\nmessage.Body = new TextPart("plain") { Text = "Hello from .NET!" };\n\nusing var client = new SmtpClient();\nawait client.ConnectAsync("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);\nawait client.AuthenticateAsync("your@gmail.com", "your-app-password");\nawait client.SendAsync(message);\nawait client.DisconnectAsync(true);
\n\n

Why is MailKit better than SmtpClient?

\n
    \n
  • Async methods — does not block the thread
  • \n
  • Support for MIME, signatures, encryption
  • \n
  • Works on all .NET platforms (including .NET MAUI and Unity)
  • \n
  • Not marked as deprecated
  • \n
\n\n

But this approach also has a limitation: App Password is a temporary solution. If your Google account is compromised, an attacker will be able to send emails on your behalf.

\n\n

Approach 3: Google API + OAuth 2.0 (for serious projects)

\n\n

The most correct and secure way is to use

Blogs

Book Recommendations