Send mail using gmail account from C#
try
{
//Creating the object of MailMessage
MailMessage message = new MailMessage();
//Creating the object of SmtpClient
SmtpClient smtp = new SmtpClient();
//To Emial Address
message.To.Add(new MailAddress("To Email Address"));
//From Email Address
message.From = new MailAddress("From Email Address");
//It is a subject of your email
message.Subject = "Test";
//Body of the mail
message.Body = "Content";
//Set the true if Html Body , otherwise false
message.IsBodyHtml = true;
//set the priority of email
message.Priority = MailPriority.High;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
//Set credentials of your email from email will be send
smtp.Credentials = new NetworkCredential("Gmail UserName", "Gmail Pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
//Handle the exception here
}
You must need to turn on less security for your account using below link
https://www.google.com/settings/security/lesssecureapps
Comments
Post a Comment