C# 如何从 Web.config 读取 system.net/mailSettings/smtp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13452530/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read system.net/mailSettings/smtp from Web.config
提问by nermik
This is my web.configmail settings:
这是我的web.config邮件设置:
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network defaultCredentials="true" host="localhost" port="587" userName="[email protected]" password="123456"/>
      </smtp>
    </mailSettings>
  </system.net>
and here's how I try to read the values from web.config
这是我尝试从中读取值的方法 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;
But credentials are always null. How can i access these values?
但凭据始终为空。我如何访问这些值?
回答by Oded
By using the configuration, the following line:
通过使用配置,以下行:
var smtp = new System.Net.Mail.SmtpClient();
Will use the configured values - you don't need to access and assign them again.
将使用配置的值 - 您不需要再次访问和分配它们。
As for the nullvalues - you are trying accessing the configuration values incorrectly. You are just creating an empty SmtpSectioninstead of reading it from configuration.
至于null值 - 您正在尝试错误地访问配置值。您只是创建一个空的,SmtpSection而不是从配置中读取它。
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;
回答by Dtheitroademik
I think if you have defaultCredentials="true" set you will have the credentials = null as you are not using them.
我认为如果您设置了 defaultCredentials="true" ,您将拥有凭证 = null,因为您没有使用它们。
Does the email Send when you call the .Send method?
调用 .Send 方法时电子邮件是否发送?
So
所以
This is my web config mail settings:
这是我的网络配置邮件设置:
<system.net>
   <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
         <network defaultCredentials="false" host="localhost" port="587"
            userName="[email protected]" password="123456"/>
      </smtp>
   </mailSettings>
</system.net>
and this is cs
这是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)
);
回答by Amr Elgarhy
make sure you have reference to System.Net in your application
确保您在应用程序中引用了 System.Net
回答by Darren Griffith
Since no answer has been accepted, and none of the others worked for me:
由于没有答案被接受,其他人都没有为我工作:
using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;
回答by Kiran Masal
            //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;
回答by bertl
It is not necessary to use the ConfigurationManagerand get the values manually. Simply instantiating an SmtpClientis sufficient.
没有必要ConfigurationManager手动使用和 获取值。简单地实例化 anSmtpClient就足够了。
SmtpClient client = new SmtpClient();
This is what MSDNsays:
这是MSDN所说的:
This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files.
此构造函数通过使用应用程序或机器配置文件中的设置来初始化新 SmtpClient 的 Host、Credentials 和 Port 属性。
Scott Guthrie wrote a small poston that some time ago.
Scott Guthrie前段时间写了一篇关于这个的小帖子。
回答by Mattis
Set defaultCredentials="false", because when it's set to true, no credentials are used.
设置 defaultCredentials="false",因为当它设置为 true 时,不使用任何凭据。

