ASP.NET MVC3 使用 SMTP 发送电子邮件
ASP.NET MVC3 send email using SMTP
我已经实现了一个代码,应该允许我从我的网络应用程序向 gmail 发送电子邮件,但我不知道它有什么问题...
当我按下 "Submit" 按钮时没有任何反应,没有错误。我还在我的控制器中添加了断点,但它们没有被触发...
这是我的代码:
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
namespace TripPlanner.Models
{
public class EmailFormModel
{
[Required]
[StringLength(20, MinimumLength = 5)]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Message { get; set; }
}
}
查看:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TripPlanner.Models.EmailFormModel>" %>
<% using (Html.BeginForm()) {%>
<form method="post">
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<p>Name: </p>
<%: Html.TextBoxFor(m => m.Name)%>
<p>Email: </p>
<%: Html.TextBoxFor(m => m.Email)%>
<p>Subject: </p>
<%: Html.TextBoxFor(m => m.Subject)%>
<p>Message: </p>
<%: Html.TextBoxFor(m => m.Message)%>
<input type ="submit" value ="Send" />
</form>
<% } %>
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.Mvc;
using System.IO;
using System.Net;
using TripPlanner.Models;
namespace TripPlanner.Controllers
{
public class SendMailerController: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmailFormModel vm)
{
if(ModelState.IsValid)
{
try
{
MailMessage msz = new MailMessage();
msz.From = new MailAddress(vm.Email);//Email which you are getting from contact us page
msz.To.Add("my_valid_email@gmail.com");//Where mail will be sent
msz.Subject = vm.Subject;
msz.Body = vm.Message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("my_valid_email@gmail.com", "my_valid_pass");
smtp.EnableSsl = true;
smtp.Send(msz);
ModelState.Clear();
}
catch(Exception ex )
{
ModelState.Clear();
}
}
return View();
}
public ActionResult Error()
{
return View();
}
}
}
至少您的问题之一是您需要告诉 SMTP 客户端在提供 gmail 帐户的用户名和密码之前不要使用您的 windows 凭据。
添加
smtp.UseDefaultCredentials = false;
在这行代码之上(不在之后)。
smtp.Credentials = new System.Net.NetworkCredential("my_valid_email@gmail.com", "my_valid_pass");
另一个明显的问题是你确实需要在你的 catch 块中做一些有用的事情——现在你正在通过吞下异常来积极破坏你的努力。
您需要记录异常,或者至少 throw
。完全删除 try/catch 也比您现在正在做的更好。
此外,gmail 将忽略您在 MailMessage.From
属性 中设置的任何值,因为它不允许您以其他用户身份发送电子邮件。
<% using (Html.BeginForm("Index", "SendMailer", FormMethod.Post)) {%>
<form method="post">
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<p>Name: </p>
<%: Html.TextBoxFor(m => m.Name)%>
<p>Email: </p>
<%: Html.TextBoxFor(m => m.Email)%>
<p>Subject: </p>
<%: Html.TextBoxFor(m => m.Subject)%>
<p>Message: </p>
<%: Html.TextBoxFor(m => m.Message)%>
<input type ="submit" value ="Send" />
</form>
请更改您的开始表单语法,您需要指定要在其上post 表单数据的操作和控制器名称。
我已经实现了一个代码,应该允许我从我的网络应用程序向 gmail 发送电子邮件,但我不知道它有什么问题...
当我按下 "Submit" 按钮时没有任何反应,没有错误。我还在我的控制器中添加了断点,但它们没有被触发...
这是我的代码:
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
namespace TripPlanner.Models
{
public class EmailFormModel
{
[Required]
[StringLength(20, MinimumLength = 5)]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Message { get; set; }
}
}
查看:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TripPlanner.Models.EmailFormModel>" %>
<% using (Html.BeginForm()) {%>
<form method="post">
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<p>Name: </p>
<%: Html.TextBoxFor(m => m.Name)%>
<p>Email: </p>
<%: Html.TextBoxFor(m => m.Email)%>
<p>Subject: </p>
<%: Html.TextBoxFor(m => m.Subject)%>
<p>Message: </p>
<%: Html.TextBoxFor(m => m.Message)%>
<input type ="submit" value ="Send" />
</form>
<% } %>
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.Mvc;
using System.IO;
using System.Net;
using TripPlanner.Models;
namespace TripPlanner.Controllers
{
public class SendMailerController: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmailFormModel vm)
{
if(ModelState.IsValid)
{
try
{
MailMessage msz = new MailMessage();
msz.From = new MailAddress(vm.Email);//Email which you are getting from contact us page
msz.To.Add("my_valid_email@gmail.com");//Where mail will be sent
msz.Subject = vm.Subject;
msz.Body = vm.Message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("my_valid_email@gmail.com", "my_valid_pass");
smtp.EnableSsl = true;
smtp.Send(msz);
ModelState.Clear();
}
catch(Exception ex )
{
ModelState.Clear();
}
}
return View();
}
public ActionResult Error()
{
return View();
}
}
}
至少您的问题之一是您需要告诉 SMTP 客户端在提供 gmail 帐户的用户名和密码之前不要使用您的 windows 凭据。
添加
smtp.UseDefaultCredentials = false;
在这行代码之上(不在之后)。
smtp.Credentials = new System.Net.NetworkCredential("my_valid_email@gmail.com", "my_valid_pass");
另一个明显的问题是你确实需要在你的 catch 块中做一些有用的事情——现在你正在通过吞下异常来积极破坏你的努力。
您需要记录异常,或者至少 throw
。完全删除 try/catch 也比您现在正在做的更好。
此外,gmail 将忽略您在 MailMessage.From
属性 中设置的任何值,因为它不允许您以其他用户身份发送电子邮件。
<% using (Html.BeginForm("Index", "SendMailer", FormMethod.Post)) {%>
<form method="post">
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<p>Name: </p>
<%: Html.TextBoxFor(m => m.Name)%>
<p>Email: </p>
<%: Html.TextBoxFor(m => m.Email)%>
<p>Subject: </p>
<%: Html.TextBoxFor(m => m.Subject)%>
<p>Message: </p>
<%: Html.TextBoxFor(m => m.Message)%>
<input type ="submit" value ="Send" />
</form>
请更改您的开始表单语法,您需要指定要在其上post 表单数据的操作和控制器名称。