asp.net-mvc-3 - 如何在 web.config 中设置不同的 smtpclient 实例?

标签 asp.net-mvc-3 smtpclient mvcmailer

我不认为这是一个专门的 MvcMailer 问题(这是我正在使用的邮件程序),但我正在努力构建 Googleplex 搜索,以找出如何根据我的上下文从不同帐户发送电子邮件。

我需要从两个不同的电子邮件帐户发送两封电子邮件。我尝试过使用

mailMessage.From = new MailAddress("some-other-email@gmail.com");

在 MvcMailer 中,但这甚至没有显示在我转储到临时目录的电子邮件中。它显示为 web.config 中的内容:“some-email@gmail.com”。

这是我的 MvcMailer web.config:

<mailSettings>
      <!-- Method#1: Configure smtp server credentials -->
      <!--<smtp from="some-email@gmail.com">
        <network enableSsl="true" host="smtp.gmail.com" port="587" userName="some-email@gmail.com" password="valid-password" />
      </smtp>-->
      <!-- Method#2: Dump emails to a local directory -->

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

    </mailSettings>

这是邮件代码:

public virtual MailMessage EMailConsultation(EMailConsultationData model)
        {
            var mailMessage = new MailMessage { Subject = "INQUIRY: E-Mail Consultation" };

            mailMessage.From = new MailAddress("some-other-email@gmail.com");//I tested this to see if at the very least it would show up in the e-mail, but it didn't.

            mailMessage.To.Add(model.EMail);

            ViewData = new ViewDataDictionary(model);
            PopulateBody(mailMessage, viewName: "InquiryEMailConsultation");

            return mailMessage;
        }

同样,上面的代码可以发送电子邮件。我只是不知道如何将邮件程序设置为从指定的电子邮件地址发送,而不是像 web.config 中那样仅从“some-email@gmail.com”发送。我有多个 MailMessage,并且需要从不同的电子邮件帐户发送某些邮件。

我将非常感谢任何帮助/代码示例。

最佳答案

您可以在代码中创建自己的 SmtpClient 对象,并使用该对象发送生成的电子邮件。并且仅在 web.config 中使用 1 个 smtp 设置(默认设置)。

在 MvcMailer 的 web.config 中:

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

并使用MyMailer.EMailConsultation().Send();

如果您需要通过谷歌发送电子邮件(例如),请使用此:

using (var googleSmtp = new SmtpClient("smtp.gmail.com", 587))
{
    googleSmtp.EnableSsl = true;
    googleSmtp.Credentials = new NetworkCredential("some-email@gmail.com", "valid-password");

    googleSmtp.Send(MyMailer.EMailConsultation());
}

关于asp.net-mvc-3 - 如何在 web.config 中设置不同的 smtpclient 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130277/

相关文章:

asp.net-mvc - 以 "+"分隔的 MVC 传递 id 以执行操作

c# - 发送批量电子邮件时出错 "An asynchronous call is already in progress. It must be completed or canceled before you can call this method"

c# - 如何从Smtp客户端发送邮件?

asp.net - 如何在 Orchard 中设置小部件的样式?

asp.net-mvc-3 - 您可以编辑 CSHTML 而无需再次构建吗

asp.net-mvc-3 - MVCsitemap 节点不渲染

java - 用于测试继承项目的基本 smtp 命令?

asp.net-mvc - 为 MVC Mailer 伪造一个 HTTPContext

mvcmailer - MVC 4 中使用 MvcMailer 的错误脚手架

unit-testing - MvcMailer 单元测试 : System. ArgumentNullException httpContext 不能为空