1

連絡先ページからメールを送信するために郵便ライブラリを使用しています。次のコードがあります。

連絡先メールアドレス

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&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>


}

おっと、私の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 サーバーがセキュリティで保護された接続を必要とするか、クライアントが認証されませんでした。サーバーの応答は次のとおりです。5.5.1 認証が必要です。

web.config で認証資格情報を設定していると思いましたか?

4

1 に答える 1

0

web.config でdefaultCredentialsを falseに変更する必要がありましたが、現在はすべて機能しているようです。

于 2015-07-31T03:08:32.537 に答える