Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="[email protected]">
        <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"]?

like image 570
BreakPhreak Avatar asked Dec 15 '25 20:12

BreakPhreak


1 Answers

Nothing special is needed, just initialize and send :)

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

That is all, it will pull from the config.

like image 127
Mitchel Sellers Avatar answered Dec 17 '25 11:12

Mitchel Sellers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!