Send email c# gmail

Here is an example of how to send an email using C# and Gmail:

using System;
using System.Net.Mail;
using System.Net;

// Replace with your Gmail address and password
string fromEmail = "[email protected]";
string password = "your_password";

// Replace with the recipient's email address
string toEmail = "[email protected]";

// Replace with the subject and body of the email
string subject = "Test Email";
string body = "This is a test email sent using C# and Gmail";

// Create a new SmtpClient object
SmtpClient client = new SmtpClient();

// Set the SmtpClient properties
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(fromEmail, password);

// Create a new MailMessage object
MailMessage message = new MailMessage();

// Set the From, To, Subject, and Body properties
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = subject;
message.Body = body;

// Send the email
client.Send(message);

This code uses the System.Net.Mail namespace to create a new SmtpClient object and send an email using Gmail's SMTP server.

Here are some notes to keep in mind:

Also, you can use System.Net.Mail.SmtpClient with Gmail's SMTP server, but you need to set the EnableSsl property to true and the Port property to 587.

You can also use Gmail.SmtpClient which is a wrapper around System.Net.Mail.SmtpClient and provides some additional features and settings for Gmail.

Here is an example of how to use Gmail.SmtpClient:

using Gmail.SmtpClient;

// Replace with your Gmail address and password
string fromEmail = "[email protected]";
string password = "your_password";

// Replace with the recipient's email address
string toEmail = "[email protected]";

// Replace with the subject and body of the email
string subject = "Test Email";
string body = "This is a test email sent using C# and Gmail";

// Create a new GmailSmtpClient object
GmailSmtpClient client = new GmailSmtpClient(fromEmail, password);

// Set the From, To, Subject, and Body properties
client.From = fromEmail;
client.To.Add(toEmail);
client.Subject = subject;
client.Body = body;

// Send the email
client.Send();

This code uses the Gmail.SmtpClient class to send an email using Gmail's SMTP server.