Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

How to send email with .NET

22 Mar 2017 » csharp

Firstly, we should enable less secure apps option on gmail

https://www.google.com/settings/security/lesssecureapps

We will use Gmail’s SMTP Server

public static void SendEmail(string toEmailAddress, string subject, string body)
{
	var fromAddress = new MailAddress("[email protected]", "From Display Name");
	var toAddress = new MailAddress(toEmailAddress, "To Display Name");
	var fromPassword = "yourSmtpEmailAccountPassword";
	var smtp = new SmtpClient();
	smtp.Host = "smtp.gmail.com";
	smtp.Port = 587;
	smtp.EnableSsl = true;
	smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
	var networkCredential = new NetworkCredential();
	networkCredential.UserName = fromAddress.Address;
	networkCredential.Password = fromPassword;
	smtp.Credentials = networkCredential;
	smtp.Timeout = 20000;
	var message = new MailMessage(fromAddress, toAddress);
	message.Subject = subject;
	message.Body = body;
	smtp.Send(message);
}

You can download source code from here –> Download