Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Web.config读取system.net/mailSettings/smtp_C#_Web Config_Smtp_Sendmail - Fatal编程技术网

C# 如何从Web.config读取system.net/mailSettings/smtp

C# 如何从Web.config读取system.net/mailSettings/smtp,c#,web-config,smtp,sendmail,C#,Web Config,Smtp,Sendmail,这是我的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"/

这是我的
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>

但是凭证总是空的。如何访问这些值?

使用以下配置行:

var smtp = new System.Net.Mail.SmtpClient();
将使用配置的值-您无需再次访问和分配它们


至于
null
值-您试图错误地访问配置值。您只是创建一个空的
SmtpSection
,而不是从配置中读取它

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

我认为,如果您设置了defaultCredentials=“true”,那么您将拥有=null的凭据,因为您没有使用它们

当您调用.Send方法时,电子邮件是否发送

所以

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

<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>

这是cs

SmtpClient SmtpClient=new SmtpClient();
字符串smtpDetails=
@"
DeliveryMethod={0},
主机={1},
PickupDirectoryLocation={2},
端口={3},
TargetName={4},
UseDefaultCredentials={5}”;
控制台写入线(SMTP详细信息,
smtpClient.DeliveryMethod.ToString(),
smtpClient.Host,
smtpClient.PickupDirectoryLocation==null
?“未设置”
:smtpClient.PickupDirectoryLocation.ToString(),
smtpClient.Port,
smtpClient.TargetName,
smtpClient.UseDefaultCredentials.ToString)
);

请确保您的应用程序中有对System.Net的引用

,因为没有答案被接受,而且其他答案都不适用于我:

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

无需使用
ConfigurationManager
手动获取值。只需实例化一个
SmtpClient
就足够了

SmtpClient client = new SmtpClient();
这是说:

此构造函数使用应用程序或计算机配置文件中的设置初始化新SmtpClient的主机、凭据和端口属性


斯科特·古思里(Scott Guthrie)不久前在这方面写了一篇小文章。

设置defaultCredentials=“false”,因为当它设置为true时,不会使用凭据。

不,这不是问题所在。OP正在创建一个新的
SmtpSection
,而不是从配置中读取它。。因此,如果您只调用SmtpClient SmtpClient=new SmtpClient();它应该为您阅读condig设置,但我几乎可以肯定我还记得一些关于默认凭证的内容是真实的。。。我会检查是的,如果您使用默认凭据,用户名和密码应该为空。正如奥德所说。。。现在创建一个新凭据。。。只需实例化您的SmtpClient,如SmtpClient SmtpClient=new SmtpClient();并检查设置,以便使用配置中的用户名和Pwd。。。设置如果您想说,请使用networkdefaultcredentials=“false”;var smtp=new System.Net.Mail.SmtpClient();字符串strUserName=smtp.UserName;字符串strFromPass=smtp.Password;我不能这样访问。@nermik-我不知道你的意思。我只想从web.config访问用户名和密码,比如Host和port.var smtpSection=(smtpSection)ConfigurationManager.GetSection(“”);我用过,但还是空的。这是更好的答案。这不适合我。每次我都是空的。。代码与此处相同。。我遗漏了什么?但这并不能告诉您是否正在使用,例如,在这种情况下,您可能需要在SMTP初始代码中关闭
EnableSSL
,以避免“不能为分拣目录传递方法启用SSL”。2、等。。。
SmtpClient client = new SmtpClient();