Sending email through Gmail SMTP server with C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You can send email from C# through Gmail's SMTP service, but the important part is not just setting smtp.gmail.com and port 587. You also need the right authentication model, secure credential handling, and realistic expectations about Gmail's policy restrictions. In current practice, the simplest workable setup for a small app is usually SMTP with an app password on an account that has 2-step verification enabled.
Know What Gmail Will and Will Not Accept
Gmail supports SMTP submission, but it does not want arbitrary applications logging in with your ordinary account password through weak authentication settings. Older tutorials often mention “less secure apps,” but that is outdated and not the right direction for new code.
For a straightforward C# SMTP client, the typical setup is:
- use a Gmail or Google Workspace account
- enable 2-step verification
- create an app password for SMTP
- connect to
smtp.gmail.comon port587 - enable TLS
That gives your application a dedicated credential rather than reusing your main account password.
Basic C# Example with SmtpClient
For small internal tools or legacy code, SmtpClient still illustrates the SMTP flow clearly.
This example is enough to prove connectivity and authentication. For production use, you should move the credential to configuration or a secret store instead of hard-coding it.
Prefer Secret Storage Over Source-Code Credentials
Even for a small app, do not leave SMTP credentials inside source files. Load them from environment variables, user secrets, or a secure secret manager.
A simple environment-variable approach looks like this:
That still uses SMTP, but it removes the most obvious credential-management mistake.
Understand the Port and TLS Settings
Port 587 is the normal submission port with TLS negotiation. In .NET, that means setting EnableSsl = true so the connection is secured.
If you omit TLS, Gmail will reject the connection or the authentication flow will fail. The exact exception text varies, but the root cause is usually policy or authentication setup, not a C# syntax problem.
When SmtpClient Is Not Enough
SmtpClient is still available, but it is old and limited. For more robust mail handling, attachments, MIME control, and modern authentication workflows, many teams prefer MailKit.
A MailKit example is still straightforward:
This is often the better long-term choice if the application's email needs are more than minimal.
Diagnose Failures by Category
If sending fails, separate the problem into one of these buckets:
- authentication failed
- TLS or port mismatch
- blocked sign-in policy
- invalid sender or recipient address
- network connectivity issue
That classification is more useful than staring only at the final exception message.
Common Pitfalls
The most common mistake is using the regular Gmail password instead of an app password on an account configured for SMTP access.
Another mistake is hard-coding credentials in source control. Even for a demo, that habit creates avoidable risk.
Developers also forget that Gmail policy changes matter. A tutorial that worked years ago may describe a login flow that Google no longer permits.
Summary
- Gmail SMTP from C# works, but the setup must match Google's current authentication rules.
- The usual SMTP endpoint is
smtp.gmail.comon port587with TLS enabled. - Use an app password instead of your normal Gmail password.
- Keep credentials in configuration or a secret store, not in source code.
- For more advanced scenarios, consider
MailKitinstead of relying only on legacySmtpClient.
Related reading
- Sequence contains more than one element
- Serialize Class containing Dictionary member
- Serialize Property as Xml Attribute in Element
- Server.UrlEncode vs. HttpUtility.UrlEncode
- Session issue when having async Session_Start method?
- Set background color of WPF Textbox in C code
- Set database timeout in Entity Framework
- Set focus on textbox in WPF

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.