在 MVC 5 中使用邮政库发送邮件
Sending mail with Postal library in MVC 5
我正在使用邮政图书馆从联系页面发送电子邮件。我有以下代码:
联系邮箱
using AccessorizeForLess.ViewModels;
using Postal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AccessorizeForLess.Models
{
public class ContactEmail : Email
{
public string To { get; set; }
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "The Email field is not valid, Please enter a valid email address!")]
[Required(ErrorMessage="Email address is required.")]
public string From { get; set; }
[DisplayName("First Name")]
[Required(ErrorMessage="Please provide your first name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required(ErrorMessage="Please provide your last name")]
public string LastName { get; set; }
[DisplayName("Subject")]
[Required(ErrorMessage="Please select a topic")]
public SelectList Subject { get; set; }
public string SelectedSubject { get; set; }
[DisplayName("Message")]
[DataType(DataType.MultilineText)]
[Required(ErrorMessage="PLease provide a message for the email")]
public string Message { get; set; }
public List<EmailSubjectViewModel> Subjects { get; set; }
}
}
EmailSubjectViewModel
namespace AccessorizeForLess.ViewModels
{
public class EmailSubjectViewModel
{
public int Id { get; set; }
public string Subject { get; set; }
}
}
联系人控制器
public ActionResult Index()
{
List<EmailSubjectViewModel> Subjects = new List<EmailSubjectViewModel>()
{
new EmailSubjectViewModel(){Id=1, Subject="Website Issues"},
new EmailSubjectViewModel(){Id=2, Subject="Order Issue"},
new EmailSubjectViewModel(){Id=3, Subject="Product Question"},
new EmailSubjectViewModel(){Id=4, Subject="Returns Questions"},
new EmailSubjectViewModel(){Id=5, Subject="Other"}
};
ContactEmail email = new ContactEmail()
{
Subjects = Subjects
};
return View(email);
}
public ActionResult Send(ContactEmail email)
{
ContactEmail e = new ContactEmail();
e.From = email.From;
e.FirstName = email.FirstName;
e.LastName = email.LastName;
e.SelectedSubject = email.SelectedSubject;
e.Message = email.Message;
e.Send();
return View();
}
最后但同样重要的是我的观点:
@model AccessorizeForLess.Models.ContactEmail
@{
ViewBag.Title = "Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Contact</h2>
@using (Html.BeginForm("Send", "Contact", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AccessorizeForLess.net
<a href="https://www.facebook.com/accessorize5orless" target="_blank" title="Accessorize For Less On Facebook"><img src="~/Content/icon-facebook.png" /></a></h4>
<div style="color:red;font-weight:bold;">@ViewBag.Message</div>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.From, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.From)
@Html.ValidationMessageFor(model => model.From)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SelectedSubject, "Category", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedSubject, new SelectList(Model.Subjects, "Id", "Subject"), "- Please Select -")
@Html.ValidationMessageFor(model => model.SelectedSubject)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { @cols = "25", @rows = "55" })
@Html.ValidationMessageFor(model => model.Message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
}
糟糕,差点忘了我 web.config
中的部分
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="admin@accessorizeforless.net">
<network host="smtp.gmail.com" port="587" enableSsl="true" defaultCredentials="true" userName="***********@gmail.com" password="**********" />
</smtp>
</mailSettings>
</system.net>
现在,当我填写表格并点击发送时,出现以下错误:
System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.5.1 Authentication Required.
我以为我在 web.config?
中设置了身份验证凭据
我不得不在 web.config 中将 defaultCredentials 更改为 false,现在似乎一切正常。
我正在使用邮政图书馆从联系页面发送电子邮件。我有以下代码:
联系邮箱
using AccessorizeForLess.ViewModels;
using Postal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AccessorizeForLess.Models
{
public class ContactEmail : Email
{
public string To { get; set; }
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "The Email field is not valid, Please enter a valid email address!")]
[Required(ErrorMessage="Email address is required.")]
public string From { get; set; }
[DisplayName("First Name")]
[Required(ErrorMessage="Please provide your first name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required(ErrorMessage="Please provide your last name")]
public string LastName { get; set; }
[DisplayName("Subject")]
[Required(ErrorMessage="Please select a topic")]
public SelectList Subject { get; set; }
public string SelectedSubject { get; set; }
[DisplayName("Message")]
[DataType(DataType.MultilineText)]
[Required(ErrorMessage="PLease provide a message for the email")]
public string Message { get; set; }
public List<EmailSubjectViewModel> Subjects { get; set; }
}
}
EmailSubjectViewModel
namespace AccessorizeForLess.ViewModels
{
public class EmailSubjectViewModel
{
public int Id { get; set; }
public string Subject { get; set; }
}
}
联系人控制器
public ActionResult Index()
{
List<EmailSubjectViewModel> Subjects = new List<EmailSubjectViewModel>()
{
new EmailSubjectViewModel(){Id=1, Subject="Website Issues"},
new EmailSubjectViewModel(){Id=2, Subject="Order Issue"},
new EmailSubjectViewModel(){Id=3, Subject="Product Question"},
new EmailSubjectViewModel(){Id=4, Subject="Returns Questions"},
new EmailSubjectViewModel(){Id=5, Subject="Other"}
};
ContactEmail email = new ContactEmail()
{
Subjects = Subjects
};
return View(email);
}
public ActionResult Send(ContactEmail email)
{
ContactEmail e = new ContactEmail();
e.From = email.From;
e.FirstName = email.FirstName;
e.LastName = email.LastName;
e.SelectedSubject = email.SelectedSubject;
e.Message = email.Message;
e.Send();
return View();
}
最后但同样重要的是我的观点:
@model AccessorizeForLess.Models.ContactEmail
@{
ViewBag.Title = "Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Contact</h2>
@using (Html.BeginForm("Send", "Contact", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AccessorizeForLess.net
<a href="https://www.facebook.com/accessorize5orless" target="_blank" title="Accessorize For Less On Facebook"><img src="~/Content/icon-facebook.png" /></a></h4>
<div style="color:red;font-weight:bold;">@ViewBag.Message</div>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.From, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.From)
@Html.ValidationMessageFor(model => model.From)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SelectedSubject, "Category", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedSubject, new SelectList(Model.Subjects, "Id", "Subject"), "- Please Select -")
@Html.ValidationMessageFor(model => model.SelectedSubject)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { @cols = "25", @rows = "55" })
@Html.ValidationMessageFor(model => model.Message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
}
糟糕,差点忘了我 web.config
中的部分<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="admin@accessorizeforless.net">
<network host="smtp.gmail.com" port="587" enableSsl="true" defaultCredentials="true" userName="***********@gmail.com" password="**********" />
</smtp>
</mailSettings>
</system.net>
现在,当我填写表格并点击发送时,出现以下错误:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
我以为我在 web.config?
中设置了身份验证凭据我不得不在 web.config 中将 defaultCredentials 更改为 false,现在似乎一切正常。