I have a C# application where I am sending an email. The function works fine when I use my personal email to send an email. However, I have created a gmail account to use for this for a company to send emails but it does not work with that email. On the gmail account I created I have set the Access to Less Secure apps On. That is the same thing I did to get it to work with my personal email. However, it just does not work with new gmail account created. Any ideas?
Here is the code
public static void SendEmail(string strEmail,string strRandomPassword)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("email@gmail.com");
message.To.Add(new MailAddress(strEmail));
message.Subject = "Password Reset";
message.IsBodyHtml = true; //to make message body as html
message.Body = "Here is your key to reset your password. Key: " + strRandomPassword;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com"; //for gmail host
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email@gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}