如何从Web.config文件读取system.net/mailSettings/smtp(How

2019-08-02 22:09发布

这是我web.config邮件设置:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
        <network defaultCredentials="true" host="localhost" port="587" userName="smthg@smthg.net" password="123456"/>
      </smtp>
    </mailSettings>
  </system.net>

这里就是我试图读取值web.config

 var smtp = new System.Net.Mail.SmtpClient();
 var credential = new System.Net.Configuration.SmtpSection().Network;

 string strHost = smtp.Host;
 int port = smtp.Port;
 string strUserName = credential.UserName;
 string strFromPass = credential.Password;

但凭据总是空。 我如何才能获得这些价值?

Answer 1:

由于没有答案已被接受,并没有其他的工作对我来说:

using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;


Answer 2:

这是没有必要使用ConfigurationManager并手动获取值。 只需实例化一个SmtpClient就足够了。

SmtpClient client = new SmtpClient();

这是MSDN说:

这个构造函数通过在应用程序或设备配置文件中的设置初始化主机,凭证和港口新SmtpClient属性。

斯科特格思里写了一个小职位上,前一段时间。



Answer 3:

通过使用上述结构,下面的行:

var smtp = new System.Net.Mail.SmtpClient();

将使用配置的值 - 你不需要访问,并再次将它们分配。


而对于null值-您尝试访问不正确的配置值。 你只是创建一个空的SmtpSection从配置读取它来代替。

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;


Answer 4:

            //You can access the network credentials in the following way.
            //Read the SmtpClient section from the config file    
            var smtp = new System.Net.Mail.SmtpClient();
            //Cast the newtwork credentials in to the NetworkCredential class and use it .
            var credential = (System.Net.NetworkCredential)smtp.Credentials;
            string strHost = smtp.Host;
            int port = smtp.Port;
            string strUserName = credential.UserName;
            string strFromPass = credential.Password;


Answer 5:

我认为,如果你有的DefaultCredentials =“真”设置,您将有凭据= null作为不使用它们。

请问,当你调用方法。发送电子邮件发送?

所以

这是我的web配置邮件设置:

<system.net>
   <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
         <network defaultCredentials="false" host="localhost" port="587"
            userName="smthg@smthg.net" password="123456"/>
      </smtp>
   </mailSettings>
</system.net>

这是CS

SmtpClient smtpClient = new SmtpClient();

string smtpDetails =
    @"
    DeliveryMethod = {0},
    Host = {1},
    PickupDirectoryLocation = {2},
    Port = {3},
    TargetName = {4},
    UseDefaultCredentials = {5}";

Console.WriteLine(smtpDetails,
    smtpClient.DeliveryMethod.ToString(),
    smtpClient.Host,
    smtpClient.PickupDirectoryLocation == null
        ? "Not Set"
        : smtpClient.PickupDirectoryLocation.ToString(),
    smtpClient.Port,
    smtpClient.TargetName,
    smtpClient.UseDefaultCredentials.ToString)
);


Answer 6:

请确保你的应用程序有参考System.Net



Answer 7:

设置的DefaultCredentials =“假”,因为当它设置为true,不使用任何凭据。



文章来源: How to read system.net/mailSettings/smtp from Web.config