通过Mvc3 asp.net发送邮件 [英] Sending Mail through Mvc3 asp.net
本文介绍了通过Mvc3 asp.net发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过邮件在Asp.Net Mvc3中将用户的忘记密码发送给他
首先,我从数据库中获取用户的电子邮件地址..然后,我必须发送邮件,我的代码在下面..
型号课
Hi,
I wants to send a forgotten password of user to him through mail... in Asp.Net Mvc3
First i fetch the email address of user from database.. then i have to send the mail my code is below..
Model class
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; }
}
}
查看
View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikLogin.Models.ViewModels.ForgotPassword>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ForgotPassword
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ForgotPassword</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% 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"/>
<%} %>
</asp:Content>
控制器
Controller
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)
{
//....Here is the code for sending a mail.....
MailMessage msg = new MailMessage();
msg.From = new MailAddress("direction@gmail.com");
msg.To.Add(user_email_address);
msg.Subject = "Password";
msg.Body = "Test";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("direction@gmail.com", "direction", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.UseDefaultCredentials = true;
client.Send(msg);
return RedirectToAction("MailTransfer", "MailT");
}
return View();
}
它在下面的行中给我一个错误
It gives me an error on below line
client.Send(msg);
错误是:
And Error 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
我想解决这个错误..所以需要帮助....
在此先感谢...
I wanna solve this error.. so need help....
Thanks in advance...
推荐答案
使用以下代码发送电子邮件:
Use the below code to sent Email:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("yourID@gmail.com");
msg.To.Add("sendID@yahoo.com");
msg.Body = "body my mail";
msg.Subject = "my mail subject";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("yourID@gmail.com", "yourName", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.UseDefaultCredentials =true;
client.Send(msg);
检查一下:
ASP.NET MVC3密码重置 [ ^ ]
Check this out:
ASP.NET MVC3 Password Reset[^]
使用此代码
Use this code
public int SendUserMail(string fromad, string toad, string body, string header, string subjectcontent)
{
int result = 0;
MailMessage usermail = Mailbodplain(fromad, toad, body, header, subjectcontent);
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential("your user id ", "pwd"); ;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
try
{
client.Send(usermail);
result = 1;
}
catch (Exception ex)
{
result = 0;
} // end try
return result;
}
public MailMessage Mailbodplain(string fromad, string toad, string body, string header, string subjectcontent)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
try
{
string from = fromad;
string to = toad;
mail.To.Add(to);
mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
mail.Subject = subjectcontent;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
}
catch (Exception ex)
{
throw;
}
return mail;
}
这篇关于通过Mvc3 asp.net发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文