Why sending email via Gmail in .NET is a headache (and how to avoid it)
\n\nIf 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?
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\nSpoiler: 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\nApproach 1: Classic SmtpClient (and why it's outdated)
\n\nThe most obvious way is to use the built-in SmtpClient from System.Net.Mail. Here's what it looked like in the "golden days":
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\nIn 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
Disadvantages of this approach:
\n- \n
SmtpClientis marked asObsoletein .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
When to use: only for quick prototypes or internal tools where security is not critical.
\n\nApproach 2: MailKit — modern and powerful
\n\nIf you want a production-grade solution, use the MailKit library by the Mono developer. It not only works but is actively developed.
\n\nFirst, install the package:
\ndotnet add package MailKit\n\nNow the code using App Password:
\n\nusing 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\nWhy 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
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\nApproach 3: Google API + OAuth 2.0 (for serious projects)
\n\nThe most correct and secure way is to use