I'm trying to send an email from gmail to my Gmail account. is there any other option rather than turn off Less secure option in Gmail account to be able to send email via program?
private void btnSend_Click(object sender, EventArgs e) {
SmtpClient client = new SmtpClient("smtp@gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("from@gmail.com", "password");
MailAddress fromEmail = new MailAddress("from@gmail.com", "From C#");
MailAddress toAddress = new MailAddress("to@gmail.com", "dogy");
MailMessage message = new MailMessage()
{
From = fromEmail,
Subject = "just test from c# program",
Body = "it's coming from c# program"
};
message.To.Add(toAddress);
try {
client.Send(message);
MessageBox.Show("Email sent successfully");
}
catch (Exception) {
MessageBox.Show("Failed!");
}
}