当我运行此代码时,它会给出错误(smtp.Send(mm);)我不知道如何解决它

时间:2017-08-29 18:08:59

标签: c#

当我运行它时,这段代码给了我一些问题。它不发送消息,它说错误是在smtp.Send(mm);我无法解决它

    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.";
    }
}

1 个答案:

答案 0 :(得分:1)

我相信你遇到了smtp服务器的安全问题,我通过提供这些行上的真实gmail帐户信息来解决它:RegisterStartupScript似乎不会起作用。

"<Password>"

并启用我的Gmail帐户设置,以允许安全性较低的应用访问它。

仅供参考,对于其他人来说,这是我改变之前的错误。

  

其他信息:SMTP服务器需要安全连接   或者客户端未经过身份验证。服务器响应是:5.5.1   需要验证。

了解更多信息