如何从Asp.net Mvc-3发送电子邮件?

Dip*_*Dip 36 c# asp.net asp.net-mvc-3

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

我必须发一个忘记的密码,我该怎么办?我的代码如下.

型号代码..

 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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器代码..

  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();
        }
Run Code Online (Sandbox Code Playgroud)

在这里,我通过输入的电子邮件地址从数据库中获取用户的密码.

查看代码..

<% 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"/>

   <%} %>
Run Code Online (Sandbox Code Playgroud)

它给了我这条线上的错误

 client.Send(msg);
Run Code Online (Sandbox Code Playgroud)

错误消息是:

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
Run Code Online (Sandbox Code Playgroud)

怎么解决?提前致谢

Dmi*_* S. 69

导入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);
Run Code Online (Sandbox Code Playgroud)

PS您在示例代码中有一个SQL注入漏洞.使用SqlCommand带参数的对象而不是String.Format().

使用SqlDataReader将更有效地检查记录而不是填充DataSet.

  • SmtpServer.Credentials = new System.Net.NetworkCredential(userName,password); SmtpServer.Port = 587; // Gmail适用于此端口 (5认同)

Dha*_*ust 16

看看MvcMailer

MvcMailer为您提供ActionMailer样式的电子邮件,发送NuGet Package for ASP.NET MVC 3/4.因此,您可以使用ViewBag生成由MVC母版页和视图组成的专业外观电子邮件.


Yas*_*ser 10

你可以用这个......

    public void SendEmail(string address, string subject, string message)
    {
        string email = "-your-id-@gmail.com";
        string password = "put-your-GMAIL-password-here";

        var loginInfo = new NetworkCredential(email, password);
        var msg = new MailMessage();
        var smtpClient = new SmtpClient("smtp.gmail.com", 587);

        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress(address));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);
    }
Run Code Online (Sandbox Code Playgroud)