The correct way to pass username / password to SmtpClient (.NET)

The question is too simple, but still appreciate the short answer. I would like to SmtpClientretrieve the username / password from the file App.config. From the MSDN / schema, I realized that the correct file (excerpt) should look like this:

  <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 a suitable API to call when initializing the state SmtpClient, so that the mail and password are well extracted from 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 correct / selected method to extract servername, user, passwordor do I just need to call the "usual" the API, for example ConfigurationManager.AppSettings["server"]?

+3
source share
1

, :)

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

, .

+3

Source: https://habr.com/ru/post/1757878/


All Articles