【问题标题】:How set up different stmpclient instances in web.config?如何在 web.config 中设置不同的 smtpclient 实例?
【发布时间】:2012-02-26 04:00:21
【问题描述】:

我不认为这是一个专门的 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,并且需要从不同的电子邮件帐户发送某些邮件。

如果有任何帮助/代码示例,我将不胜感激。

【问题讨论】:

  • 找到了这个:stackoverflow.com/a/4624598/624479,但我不知道如何将它放入代码中。
  • 好吧,我可以等待 1 天的赏金,但我认为我的积分太少,甚至无法提供。 :(

标签: asp.net-mvc-3 smtpclient mvcmailer


【解决方案1】:

您可以在代码中创建自己的 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();

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

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());
}

【讨论】:

    【解决方案2】:

    或者,您可以创建自定义 Web 配置部分来处理具有后备和全部的多个条目。见http://www.coralys.com/Articles/Custom-ASPNET-Config-Sections.aspx

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 1970-01-01
      • 2010-10-02
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2015-01-30
      • 1970-01-01
      相关资源
      最近更新 更多