Asp.net mvc 5 在MVC5中使用邮政库发送邮件

Asp.net mvc 5 在MVC5中使用邮政库发送邮件,asp.net-mvc-5,postal,Asp.net Mvc 5,Postal,我正在使用邮政图书馆从联系人页面发送电子邮件。我有以下代码: 联系电子邮件 using AccessorizeForLess.ViewModels; using Postal; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; us

我正在使用邮政图书馆从联系人页面发送电子邮件。我有以下代码:

联系电子邮件

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; }
    }
}
namespace AccessorizeForLess.ViewModels
{
    public class EmailSubjectViewModel
    {
        public int Id { get; set; }
        public string Subject { get; set; }
    }
}
ContactController

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();
        }
public ActionResult Index()
{
列表主题=新列表()
{
新的EmailSubjectViewModel(){Id=1,Subject=“网站问题”},
新的EmailSubjectViewModel(){Id=2,Subject=“订单问题”},
新的EmailSubjectViewModel(){Id=3,Subject=“产品问题”},
新的EmailSubjectViewModel(){Id=4,Subject=“返回问题”},
新建EmailSubjectViewModel(){Id=5,Subject=“其他”}
};
ContactEmail=新的ContactEmail()
{
主题=主题
};
返回视图(电子邮件);
}
公共行动结果发送(联系电子邮件)
{
ContactEmail e=新的ContactEmail();
e、 发件人=电子邮件。发件人;
e、 FirstName=email.FirstName;
e、 LastName=email.LastName;
e、 SelectedSubject=email.SelectedSubject;
e、 Message=email.Message;
e、 Send();
返回视图();
}
最后但并非最不重要的是我的观点:

@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&nbsp;&nbsp;&nbsp;
            <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>


}
@model AccessorizeForLess.Models.ContactEmail
@{
ViewBag.Title=“联系人”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
接触
@使用(Html.BeginForm(“发送”、“联系”、FormMethod.Post))
{
@Html.AntiForgeryToken()
无附件.net
@查看包。留言

@Html.ValidationSummary(true) @LabelFor(model=>model.FirstName,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.FirstName) @Html.ValidationMessageFor(model=>model.FirstName) @LabelFor(model=>model.LastName,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LastName) @Html.ValidationMessageFor(model=>model.LastName) @LabelFor(model=>model.From,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.From) @Html.ValidationMessageFor(model=>model.From) @LabelFor(model=>model.SelectedSubject,“Category”,新的{@class=“controllabel col-md-2”}) @Html.DropDownListFor(model=>model.SelectedSubject,新建SelectList(model.Subjects,“Id”,“Subject”),“-请选择-”) @Html.ValidationMessageFor(model=>model.SelectedSubject) @LabelFor(model=>model.Message,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Message,新的{@cols=“25”,@rows=“55”}) @Html.ValidationMessageFor(model=>model.Message) }
Oops几乎忘记了my 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:SMTP服务器需要安全的 连接或客户端未通过身份验证。服务器响应 was:5.5.1需要身份验证


我以为我在web.config中设置了AuthenticationOn凭据?

我必须在web.config中将defaultCredentials更改为false,所有这些都似乎正在工作。

我必须在web.config中将defaultCredentials更改为false,所有这些都似乎正在工作