【发布时间】:2013-09-01 12:37:26
【问题描述】:
我正在尝试重置密码和恢复密码功能。因此,我使用 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();
}
}
更新 我才明白为什么它不能工作。由于我的帐户信息不正确,它不起作用
【问题讨论】:
-
你能给我们来自错误而不是消息的堆栈跟踪吗?或者只是完全的例外。包括异常类型等
-
请问如何从错误中获取stacktrace?
-
try { } catch(Exception ex) { System.IO.File.WriteAllText(@"C:\output.txt", ex.ToString()); }
-
我收到此错误访问路径“C:\output.txt”被拒绝。
-
System.IO.File.WriteAllText(System.IO.Path.Combine(Server.MapPath("~/App_Data/"), "output.txt"), ex.ToString());并确保您的 asp.net 站点在执行前有一个App_Data文件夹。
标签: c# asp.net email azure smtp