2

有谁知道如何更改代码中的发件人电子邮件地址以覆盖 web.config 设置。

我正在尝试做的是允许某人填写在线表格,当收件人收到电子邮件时,它将来自发件人的电子邮件地址。

但目前它只是在 web.config 中显示默认值。

-----------------Web.config----------

<smtp from="some-email@gmail.com" deliveryMethod="SpecifiedPickupDirectory">
                <network host="localhost" />
                <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\"/>
            </smtp>

public virtual MvcMailMessage DirectotyEmail(string emailTo, string fromName, string arrivalDate, string departureDate, string message)
            {
            ViewBag.FromName        = fromName;
            ViewBag.ArrivalDate     = arrivalDate;
            ViewBag.DepartureDate   = departureDate;
            ViewBag.Message         = message;
            return Populate(x =>
            {
                x.Subject = "Accommodation Enquery form";
                x.ViewName = "DirectoryEmail";
                x.To.Add(emailTo.ToString());
                //x.From.Address("test@email.com");
                x.IsBodyHtml = true;
            });
            }

X-Sender: some-email@gmail.com << Need this to be the email that user entered when submitting form
X-Receiver: test@mail.com
MIME-Version: 1.0
From: some-email@gmail.com << Need this to be the email that user entered when submitting form
To: test@mail.com
Date: 26 Mar 2013 11:08:57 +0000
Subject: Accommodation Enquery form
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
4

1 回答 1

4

您可以使用该属性覆盖 Web 配置提供的发件人,Sender并使用该属性覆盖发件人地址From。两个属性都需要一个MailAddress对象,因此您需要编写:

return Populate(x =>
{
    x.Subject = "Accommodation Enquery form";
    x.ViewName = "DirectoryEmail";
    x.To.Add(emailTo.ToString());
    x.From = new MailAddress("test@email.com");
    x.Sender = new MailAddress("test@email.com");
    x.IsBodyHtml = true;
});
于 2013-03-26T11:38:52.690 回答