2009-12-09 164 views
0

进入电子邮件ID并点击提交按钮,提交点击事件发送邮件在他的EMAILID ,,现在即时得到一个错误,SMTP服务器requirs安全连接或客户端没有经过认证,,让我告诉我的代码SMTP服务器要求安全连接或客户端未在我忘记密码页面的用户身份验证

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e) 
    { 
     if (Page.IsValid) 
     { 
      PasswordRecovery1.MailDefinition.From = "[email protected]"; 

      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 [email protected]", 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; 

     } 
    } 

在web.config中我已经为

<mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
     <network host="smtp.gmail.com" port="587" defaultCredentials="false" /> 
     </smtp> 
    </mailSettings> 

回答

3

你需要与Gmail进行验证定义的邮件设置SMTP:

var client = new SmtpClient("smtp.gmail.com", 587); 
client.EnableSsl = true; 
client.Credentials = new NetworkCredential("[email protected]", "secret"); 

var mail = new MailMessage(); 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 
mail.Subject = "Test mail"; 
mail.Body = "test body"; 
client.Send(mail); 
相关问题