2

This code is giving me some problems when I run it. It does not send the message and it says that the error is on smtp.Send(mm); and I can't solve it

    if (!string.IsNullOrEmpty(password))
    {
        System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage("eden.elao@gmail.com", Username.Text.Trim());
        mm.Subject = "Password Recovery";
        mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
        mm.IsBodyHtml = true;
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = "sender@gmail.com";
        NetworkCred.Password = "<Password>";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm); // the error is here.
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = "Password has been sent to your email address.";
    }
    else
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "This email address does not match our records.";
    }
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
Eden
  • 21
  • 1
  • 1
    what does your issue has to do with `asp.net` or `sql-server`? – Luiso Aug 29 '17 at 18:10
  • 3
    What is the error? Could you put exception info into the question? – Aleks Andreev Aug 29 '17 at 18:11
  • try port 465 if you are enabling SSL. If you disable SSL use port 587 – MIKE Aug 29 '17 at 18:13
  • Google may block sign in attempts from some apps or devices when you try to login from some app. Check this answer https://stackoverflow.com/a/32475872/2946329 – Salah Akbari Aug 29 '17 at 18:37
  • Don't use google as SMTP, unless you have an enterprise account usually gives a ton of problems. If this is on a web page use your server's SMTP. – Gusman Aug 29 '17 at 18:38

1 Answers1

1

I believe you are running into a security issue with the smtp server I got it working by providing my real gmail account info on these lines: "<Password>" doesn't seem like it would work.

  NetworkCred.UserName = "myEmail@gmail.com";
  NetworkCred.Password = "myPassword";

And turning on the setting on my gmail account to allow less secure apps to access it.

FYI, for others this was the error before I changed it.

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Scath
  • 3,777
  • 10
  • 29
  • 40