在配置文件中设置两个smtp服务器

时间:2012-06-04 08:44:12

标签: asp.net web-config

我正在尝试在web.config文件中设置两个smtp服务器但是收到错误

 Unrecognized configuration section system.net/mailSettings/smtp_1.

如何正确地做到这一点?

<configuration>
  <configSections>
    <sectionGroup name="mailSettings">
      <section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
      <section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
    </sectionGroup>
  </configSections>

<system.net>
    <mailSettings>
      <smtp_1 from="no-reply1@web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
        <specifiedPickupDirectory  pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
        <network host="smtp...." enableSsl="true" userName="..." password="..." port="587"  />
      </smtp_1>
      <smtp_2 from="no-reply2@web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
        <specifiedPickupDirectory  pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
        <network host="smtp...." port="25" />
      </smtp_2>      
    </mailSettings>
  </system.net>
</configuration>

1 个答案:

答案 0 :(得分:1)

MailSettings不适用于此purpouse:此部分是您可以存储SMTP参数的配置位置,因此您无需在创建new SmtpClient时以编程方式更改它们。

如果你想要,你可以创建自己的部分,但不能改变原来的部分,如下所示:

<configuration>
 <configSections>
   <sectionGroup name="myMailSettings">
    <section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
    <section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
   </sectionGroup>
 </configSections>
 <myMailSettings>
  <smtp_1 from="no-reply1@web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
    <specifiedPickupDirectory  pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
    <network host="smtp...." enableSsl="true" userName="..." password="..." port="587"  />
  </smtp_1>
  <smtp_2 from="no-reply2@web2pdfconvert.com" deliveryMethod="specifiedPickupDirectory">
    <specifiedPickupDirectory  pickupDirectoryLocation="C:\Users\Administrator\Projects\temp\wp" />
    <network host="smtp...." port="25" />
  </smtp_2>
</myMailSettings>
....

最后不要忘记编写一些代码来使用该数据!