3

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 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"]?

BreakPhreak
  • 10,940
  • 26
  • 72
  • 108

1 Answers1

3

Nothing special is needed, just initialize and send :)

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

That is all, it will pull from the config.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173