in my forgot password page user enters email id and clicks on submit button,the submit click event sends a mail on his emailid,,now i m getting an error that smtp server requirs a secure connection or the client was not authenticated,,let me show my code
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
if (Page.IsValid)
{
PasswordRecovery1.MailDefinition.From = "victorzoya@gmail.com";
e.Message.IsBodyHtml = false;
e.Message.Subject = "Please read below to reset your password.";
e.Message.Body = e.Message.Body.Replace("<%email%>", PasswordRecovery1.UserName);
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["DSN"]);
SqlCommand userid = new SqlCommand("SELECT UserId from aspnet_Users WHERE UserName=@UserName", connection);
connection.Open();
userid.Parameters.Add("@UserName", SqlDbType.VarChar, 50);
userid.Parameters["@UserName"].Value = PasswordRecovery1.UserName;
SqlDataReader result = userid.ExecuteReader();
string UserId = "";
while (result.Read())
{
object obValue = result.GetValue(0);
UserId = obValue.ToString();
}
connection.Close();
string link = "http://www.fixpic.com/Passwordreset.aspx?userid=" + UserId;
开发者_开发百科 e.Message.Body = e.Message.Body.Replace("<%resetlink%>", link);
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(e.Message);
e.Cancel = true;
}
}
in web.config i have defined the mail settings as
<mailSettings>
<smtp deliveryMethod="Network" from="sumitkumarruhela@gmail.com">
<network host="smtp.gmail.com" port="587" defaultCredentials="false" />
</smtp>
</mailSettings>
You need to be authenticated with GMail's SMTP:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount@gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount@gmail.com");
mail.To.Add("youraccount@gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
See followings:
http://learnlinq.blogspot.com/2009/04/smtp-server-requires-secure-connection.html
http://www.codeproject.com/KB/IP/SendMailUsingGmailAccount.aspx
精彩评论