[英]Proper way to pass username/password to SmtpClient (.NET)
The question is too simple, but still appreciate the short answer. 问题太简单了,但仍然很欣赏简短的回答。 I would like the SmtpClient
to fetch username/password from the App.config
file. 我希望SmtpClient
从App.config
文件中获取用户名/密码。 From the MSDN/schema I've figured out that the proper file (excerpt) should look like: 从MSDN /架构我发现正确的文件(摘录)应该是这样的:
<system.net>
<mailSettings>
<smtp from="foo@bar.com">
<network
host="mail.bar.com"
port="25"
userName="foouser"
password="barpassword"
/>
</smtp>
</mailSettings>
</system.net>
I am trying to find the proper API to call, when initializing SmtpClient
state, so that mail and password will be nicely fetched from the XML: 我正在尝试在初始化SmtpClient
状态时找到适当的API来调用,以便从XML中很好地获取邮件和密码:
var client = new SmtpClient( ... ); // how to fetch the servername?
client.Credentials = new NetworkCredential( ... , ... ); // how to fetch user/pass
client.Send(message);
Is there a proper/dedicated way to fetch servername
, user
, password
or should I just call the "regular" API like ConfigurationManager.AppSettings["server"]
? 是否有适当/专用的方式来获取servername
, user
, password
或者我应该像ConfigurationManager.AppSettings["server"]
一样调用“常规”API?
Nothing special is needed, just initialize and send :) 没有什么特别需要,只需初始化并发送:)
SmtpClient client = new SmtpClient();
client.Send(mymessagehere);
That is all, it will pull from the config. 这就是全部,它将从配置中拉出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.