开发者

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. From the MSDN/schema I've figured out that the proper file (excerpt) should look like:

  <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 开发者_StackOverflow社区mail and password will be nicely fetched from the 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"]?


Nothing special is needed, just initialize and send :)

SmtpClient client = new SmtpClient();
client.Send(mymessagehere);

That is all, it will pull from the config.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜