为什么我没有收到电子邮件从我的MVC 4剃刀网站? [英] Why I am not receiving e-mail from my MVC 4 Razor site?
问题描述
我试图让用户填写一个意见表格。该表格是为了发送到我的管理员的电子邮件地址。没有收到任何电子邮件。这是我的看法。我在编程初学者。不确定我所缺少的,因为有在IDE中显示没有错误。
@model DonsSolution.Models.MailModel< H2>请教教练的问题与LT; / H>
<&字段集GT;
<形式为GT;
@using(Html.BeginForm(FormMethod.Get))
{
@ Html.ValidationSummary()
&所述p为H.;由:或其可/差异无显着
< P> @ Html.Label(发件人) @ Html.TextBoxFor(M => m.From)所述; / P> < P> @ Html.Label(到)
@ Html.TextBoxFor(M => m.To)所述; / P>
< P> @ Html.Label(主题)LT; / P>
&所述p为H.; @ Html.TextBoxFor(M => m.Subject)所述; / P>
< P> @ Html.Label(身体)< / P>
&所述p为H.; @ Html.TextAreaFor(M => m.Body)所述; / P>
<输入类型=提交值=发送/>
}
< /表及GT;
< /字段集>
控制器:
公共类SendMailerController:控制器
{
//
// GET:/ SendMailer / 公众的ActionResult指数()
{
返回查看();
} [HttpPost]
公众的ViewResult指数(DonsSolution.Models.MailModel _objModelMail)
{
如果(ModelState.IsValid)
{
邮件MAILMESSAGE新= MAILMESSAGE();
mail.To.Add(_objModelMail.To);
mail.From =新的MailAddress(example@gmail.com);
mail.Subject = _objModelMail.Subject;
绳体= _objModelMail.Body;
mail.Body =身体;
mail.IsBodyHtml = TRUE;
SmtpClient SMTP =新SmtpClient();
smtp.Host =smtp.gmail.com;
smtp.Port = 587;
smtp.UseDefaultCredentials = FALSE;
smtp.Credentials =新System.Net.NetworkCredential
(example@gmail.com,密码); //发件人的用户名和密码
smtp.EnableSsl = TRUE;
smtp.Send(邮件);
返回视图(家,_objModelMail);
}
其他
{
返回查看();
} }
型号:
公共类MailModel
{
公共字符串从{搞定;组; }
公共字符串为{搞定;组; }
公共字符串主题{搞定;组; }
公共字符串车身{搞定;组; }}
我建议设置启用SSL为false,样子你会得到这个
错误类型'System.Net.Mail.SmtpException'发生在System.dll中的一个例外,但在用户code没有处理
附加信息:SMTP服务器要求安全连接或客户端未通过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。 dj8sm48457278wid.2 - gsmtp
您code未建立安全连接。
I am trying to have the user fill out a suggestion form. The form is meant to be sent to my admin's email address. Not receiving any emails. This is my view. I am a beginner at programming. Unsure what I am missing since there are no errors displaying in the IDE.
@model DonsSolution.Models.MailModel
<h2>Ask the Coach a question</h2>
<fieldset>
<form>
@using (Html.BeginForm(FormMethod.Get))
{
@Html.ValidationSummary()
<p>From: </p>
<p>@Html.Label("From")
@Html.TextBoxFor(m => m.From)</p>
<p>@Html.Label("To")
@Html.TextBoxFor(m => m.To)</p>
<p>@Html.Label("Subject") </p>
<p>@Html.TextBoxFor(m => m.Subject)</p>
<p>@Html.Label("Body")</p>
<p>@Html.TextAreaFor(m => m.Body)</p>
<input type="submit" value="Send" />
}
</form>
</fieldset>
Controller:
public class SendMailerController : Controller
{
//
// GET: /SendMailer/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(DonsSolution.Models.MailModel _objModelMail)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.To.Add(_objModelMail.To);
mail.From = new MailAddress("example@gmail.com");
mail.Subject = _objModelMail.Subject;
string Body = _objModelMail.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("example@gmail.com", "password");// senders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
return View("Home", _objModelMail);
}
else
{
return View();
}
}
Model:
public class MailModel
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
I would suggest setting enable SSL to false, looks like you will getting this
error An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: 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. dj8sm48457278wid.2 - gsmtp.
you code is not creating a secure connection.
这篇关于为什么我没有收到电子邮件从我的MVC 4剃刀网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!