在asp.net C#

时间:2017-09-29 19:10:16

标签: c# asp.net email

我正在尝试在我的电子商务网站中实现这一点,用户可以在忘记密码时获取密码。我查看了互联网上的一些代码并试图实现它,但它显示我发送电子邮件失败。我阻止我的防火墙尝试了不同的端口,但没有任我还看了一些先前在这里提出的问题,但没有任何效果。

以下是代码:

string username = string.Empty;
string password = string.Empty;

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\SHEHAB\Documents\Visual Studio 2013\WebSites\password\App_Data\LoginDB.mdf;Integrated Security=True");

using (SqlCommand cmd = new SqlCommand("SELECT Username, [Password] FROM Users WHERE Email = @Email"))
{
    cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
    cmd.Connection = con;

    con.Open();

    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
        if (sdr.Read())
        {
            username = sdr["Username"].ToString();
            password = sdr["Password"].ToString();
        }
    }

    con.Close();
}

if (!string.IsNullOrEmpty(password))
{
    MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.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;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;

    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "sender@gmail.com";
    NetworkCred.Password = "Password";

    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;

    smtp.Send(mm);

    lblMessage.ForeColor = Color.Green;
    lblMessage.Text = "Password has been sent to your email address.";
}
else
{
    lblMessage.ForeColor = Color.Red;
    lblMessage.Text = "This email address does not match our records.";
}

错误发生在以下代码行中:

smtp.Send(mm); 

这是我得到的例外:

  

System.dll中出现“System.Net.Mail.SmtpException”类型的异常,但未在用户代码中处理

     

其他信息:发送邮件失败。

有什么想法?感谢

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

       string username = string.Empty;
        string password = string.Empty;
        SqlConnection con = new SqlConnection(@"Data Source 
(LocalDB)\v11.0;AttachDbFilename=C:\Users\SHEHAB\Documents\Visual Studio 2013\WebSites\password\App_Data\LoginDB.mdf;Integrated Security=True")


    using (SqlCommand cmd = new SqlCommand("SELECT Username, [Password] FROM  Users WHERE Email = @Email"))
        {
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.Read())
                {
                    username = sdr["Username"].ToString();
                    password = sdr["Password"].ToString();
                }
            }
            con.Close();
        }


        if (!string.IsNullOrEmpty(password))
        {
            var fromAddress = new MailAddress("from@gmail.com", "From Name");
            var toAddress = new MailAddress("to@example.com", "To Name");
            const string fromPassword = "fromPassword";
            const string subject = "Subject";
            const string body = string.Format("Hi {0},<br /><br />Your password is {1}.< br />< br /> Thank You.", username, password);

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                IsBodyHtml = true,
                Body = body
            })
            {
  //i will not push to PROD
  System.Net.ServicePointManager.ServerCertificateValidationCallback = 
 delegate(object s,                        
 System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };
                smtp.Send(message);
                lblMessage.ForeColor = Color.Green;
                lblMessage.Text = "Password has been sent to your email address.";
            }
        }
        else
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "This email address does not match our records.";
        }