Sending Email in C# using Google Account

Sending Email in C# using Google Account

Below is code snipped to send email via

MailMessage mailMessage = new MailMessage();
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;

// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "";             
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);

SmtpClient mailClient = new SmtpClient("", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;

Another way is just put the configuration in web.conf

<configuration><><mailSettings><smtp from="" deliveryMethod="Network"><network host="" port="587" userName="" password="password"/></smtp></mailSettings></></configuration>

End when you send email just enable SSL on your SmtpClient:

var message = new MailMessage("");
// here is an important part:
message.From = new MailAddress("", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.

var client = new SmtpClient();
client.EnableSsl = true;



Leave a Reply

Your email address will not be published. Required fields are marked *