从MVCMailer 4.5中的电子邮件地址更改

时间:2013-03-26 11:13:40

标签: c# asp.net-mvc-4 mvcmailer

有没有人知道如何更改代码中的电子邮件地址以覆盖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

1 个答案:

答案 0 :(得分:4)

您可以使用Sender属性覆盖Web配置提供的发件人,使用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;
});