Back to all postsPrevious Post
C#.NETEmail
How to Send Email Using Gmail SMTP Mail Server
SathishMarch 8, 2010
Sending emails using Gmail's SMTP server is straightforward with the System.Net.Mail namespace.
Code Example
csharp
using System.Net;
using System.Net.Mail;
public void SendEmail(string to, string subject, string body)
{
var fromAddress = new MailAddress("[email protected]", "Your Name");
var toAddress = new MailAddress(to);
const string fromPassword = "your-password";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
smtp.Send(message);
}
}Gmail Settings
- SMTP Server: smtp.gmail.com
- Port: 587 (TLS) or 465 (SSL)
- Enable SSL: Yes
- Authentication Required: Yes
Note
For Gmail, you may need to:
- Enable "Less secure app access" or
- Use App Passwords if 2FA is enabled
- Consider using OAuth 2.0 for production
How to Load XML File and Convert to Dictionary
Next PostHow to Use QueryString
Comments
No comments yet. Be the first to comment.