我有一个问题,当我通过此代码发送邮件时,会发生错误" SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。 "
我的编码是:
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
string UniqueCode = string.Empty;
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// get the records matching the supplied username or email id.
cmd = new SqlCommand("select * from registration where username COLLATE Latin1_general_CS_AS=@username or email COLLATE Latin1_general_CS_AS=@emailId", con);
cmd.Parameters.AddWithValue("@username", Convert.ToString(txtUserName.Text.Trim()));
cmd.Parameters.AddWithValue("@emailId", 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 registration set UniqueCode=@uniqueCode where username=@username or email=@emailid", con);
cmd.Parameters.AddWithValue("@uniqueCode", UniqueCode);
cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@emailid", 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://localhost:2464/SampleApplication/ResetPassword.aspx?emailId=" + 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("sudhakar120693@gmail.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("sudhakar120693@gmail.com", "vinothsuresh");
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 465);
mailclient.EnableSsl = true;
mailclient.UseDefaultCredentials = false;
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();
}
}
答案 0 :(得分:0)
抱歉,这是我的问题,在我的代码级别。