如何通过mvc-3 asp发送邮件.net使用c#?

我必须发送一个忘记的密码,我该怎么做?

型号代码..

 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("abc@gmail.com");
            msg.To.Add(user_email_address);
            msg.Subject = "Password";
            msg.Body = "Test1";
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();




            client.Credentials = new NetworkCredential("abc@gmail.com", "dip", "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);

错误消息是:

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

如何解决呢? 提前谢谢你

推荐答案

导入System.Net.Mail命名空间.

代码将类似于以下内容:

MailMessage mail = new MailMessage();

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Credentials = new System.Net.NetworkCredential("userName", "password");
smtpServer.Port = 587; // Gmail works on this port

mail.From = new MailAddress("myemail@gmail.com");
mail.To.Add("recepient@gmail.com");
mail.Subject = "Password recovery";
mail.Body = "Recovering the password";

smtpServer.Send(mail);

附注:示例代码中存在SQL注入漏洞.使用带参数的SqlCommand对象,而不是String.Format().

使用SqlDataReader比填充DataSet判断记录的效率要高得多.

Asp.net相关问答推荐

设置主键时KeyAttribute属性不起作用

威胁已经被清除了

如何从以特定名称开头的 appsettings 键中获取所有值并将其传递给任何数组?

用户NT AUTHORITY\NETWORK SERVICE登录失败

用于 ASP.NET 的 System.Drawing 的替代品?

如何在txt文件中保存异常?

'Microsoft.ACE.OLEDB.12.0' 提供程序未在本地计算机上注册.

ASP.NET MVC 5 中的路由可选参数

使用 NancyFx 的好处?

显示单选按钮列表内联

ASP.Net 自定义客户端验证

DBContext.Entry 有什么作用?

如何从 ASP.Net 发布然后重定向到外部 URL?

c# list 如何在两个值之间插入一个新值

将字典绑定到中继器

使 WCF Web 服务与 GET 请求一起工作

简单的 LINQ 和列表错误:WhereListIterator`1[Task]' to type 'System.Collections.Generic.List`1[Task]'

避免在 ASP.NET MVC 中使用会话状态是一种好习惯吗?如果是,为什么以及如何?

ASP.NET MVC 5 Web.config:FormsAuthenticationModule或FormsAuthentication

如何解决我的 ASP.Net MVC 应用程序中的 iisreset 后发生的 AntiForgeryToken 异常?