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("[email protected]", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "password");
MailAddress fromEmail = new MailAddress("[email protected]", "From C#");
MailAddress toAddress = new MailAddress("[email protected]", "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!");
}
}