如何使用c#通過mvc-3 asp.net發送郵件?如何從Asp.net Mvc-3發送電子郵件?
我必須發送一個忘記的密碼,所以我該怎麼做? 我的代碼如下。
型號代碼..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace TelerikLogin.Models.ViewModels
{
public class ForgotPassword
{
public int user_id { get; set; }
public string user_login_name { get; set; }
public string user_password { get; set; }
[Required]
[Display(Name="Email Address : ")]
public string user_email_address { get; set; }
}
}
控制器代碼..
public ActionResult ForgotPassword()
{
return View();
}
[HttpPost]
public ActionResult ForgotPassword(string user_email_address)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MVC3\TelerikLogin\TelerikLogin\App_Data\Login.mdf;Integrated Security=True;User Instance=True");
DataTable dt1 = new DataTable();
string strQuery = string.Format("SELECT user_password FROM [user_master] WHERE user_email_address='{0}'",user_email_address);
conn.Open();
SqlDataAdapter da1 = new SqlDataAdapter(strQuery, conn);
da1.Fill(dt1);
conn.Close();
if (dt1.Rows.Count > 0)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(user_email_address);
msg.Subject = "Password";
msg.Body = "Test1";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("[email protected]", "disha", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(msg);
return RedirectToAction("About", "Home");
}
return View();
}
這裏我端來一從通過輸入的電子郵件地址數據庫用戶的密碼..
查看代碼..
<% using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post))
{ %>
<%: Html.LabelFor(m => m.user_email_address) %>
<%: Html.TextBox("user_email_address")%>
<%: Html.ValidationSummary(true) %>
<input type="submit" value="Submit"/>
<%} %>
這使我對這些行
client.Send(msg);
錯誤messege是一個錯誤:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. x1sm1264662igc.16
如何解決呢? 在此先感謝
請考慮*不*電子郵件恢復密碼。您應該提供一個鏈接來重置您網站上的密碼。 – 2012-03-09 06:12:35
是的,你應該將密碼存儲爲散列且不可辨認的格式。查看Troy Hunt的[你想知道的關於構建安全密碼重置功能的所有內容](http://www.troyhunt.com/2012/05/everything-you-ever-wanted-to-know.html)文章。 – 2013-03-17 16:04:59
OT,但此代碼中存在巨大的SQL注入漏洞。請考慮當我使用user_email_address ='[email protected]'或'admin @ yoursite.com'進行POST時會發生什麼...... – gregmac 2013-08-07 19:15:54