我正在尝试重置密码和恢复密码功能。因此,我使用 smtp 方式发送邮件。但是,我的 try catch 出现错误。
Error Occured: Failure sending mail.
我也不确定我应该在 web.config 文件的哪个部分添加此连接代码
<network host="smtp.gmail.com" enableSsl="true" />
下面是我在使用 smtp 逻辑时与 Azure 数据库的连接。
protected void btnSubmit_Click(object sender, EventArgs e)
{
string uniqueCode = string.Empty;
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// get the records matching the supplied username or email id.
cmd = new SqlCommand("select * from MemberAccount where nric COLLATE Latin1_general_CS_AS=@nric or email COLLATE Latin1_general_CS_AS=@email", con);
cmd.Parameters.AddWithValue("@nric", Convert.ToString(txtUserName.Text.Trim()));
cmd.Parameters.AddWithValue("@email", Convert.ToString(txtEmailId.Text.Trim()));
dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.HasRows)
{
dr.Read();
//generate unique code
uniqueCode = Convert.ToString(System.Guid.NewGuid());
//Updating an unique random code in then UniquCode field of the database table
cmd = new SqlCommand("update MemberAccount set UniqueCode=@uniqueCode where nric=@nric or email=@email", con);
cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode);
cmd.Parameters.AddWithValue("@nric", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtEmailId.Text.Trim());
StringBuilder strBody = new StringBuilder();
//Passing emailid,username and generated unique code via querystring. For testing pass your localhost number and while making online pass your domain name instead of localhost path.
strBody.Append("<a href=http://sipolice.azurewebsites.net/MemberRecoverPassword.aspx" + txtEmailId.Text + "&uName=" + txtUserName.Text + "&uCode=" + uniqueCode + ">Click here to change your password</a>");
// sbody.Append("&uCode=" + uniqueCode + "&uName=" + txtUserName.Text + ">Click here to change your password</a>");
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("SenderEmailIAddress@hotmail.com", dr["email"].ToString(), "Reset Your Password", strBody.ToString());
//pasing the Gmail credentials to send the email
System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("SenderEmailIAddress@hotmail.com", "SenderPassword");
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.hotmail.com", 587);
mailclient.EnableSsl = true;
mailclient.Credentials = mailAuthenticaion;
mail.IsBodyHtml = true;
mailclient.Send(mail);
dr.Close();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
con.Close();
lblStatus.Text = "Reset password link has been sent to your email address";
txtEmailId.Text = string.Empty;
txtUserName.Text = string.Empty;
}
else
{
lblStatus.Text = "Please enter valid email address or username";
txtEmailId.Text = string.Empty;
txtUserName.Text = string.Empty;
con.Close();
return;
}
}
catch (Exception ex)
{
lblStatus.Text = "Error Occured: " + ex.Message.ToString();
}
finally
{
cmd.Dispose();
}
}
更新 我刚刚意识到为什么它不能工作。它不起作用,因为我的帐户信息不正确