2009-12-09 60 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); 
相關問題