如何从Asp.net MVC-3发送电子邮件? [英] How to send email from Asp.net Mvc-3?

查看:118
本文介绍了如何从Asp.net MVC-3发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to send a mail through mvc-3 asp.net using c#?

我要送一个忘记密码,所以我怎么能做到这一点?
我的code如下。

I have to send a forgot password so how can I do this? My code is below.

示范code ..

 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; }
    }
}

控制器code ..

Controller code..

  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", "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();
        }

下面我通过输入的电子邮件地址获取用户从数据库中的密码。

Here I fetched the password of user from database through entered email address..

查看code ..

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

   <%} %>

它给了我一个错误在这些行

It gives me an error on these line

 client.Send(msg);

错误messege是:

Error messege is:

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

如何解决呢?
在此先感谢

How to solve it? thanks in advance

推荐答案

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

在code将类似于这样:

The code will look similar to this:

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);

P.S。你必须在样本code SQL注入漏洞。使用带有参数,而不是的String.Format()

使用 SqlDataReader的将是一个很大更有效率的检查记录,而不是填充的DataSet

Using SqlDataReader would be a lot more efficient to check for a record instead of populating a DataSet.

这篇关于如何从Asp.net MVC-3发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆