This is my web.config
mail settings:
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="[email protected]"> <network defaultCredentials="true" host="localhost" port="587" userName="[email protected]" password="123456"/> </smtp> </mailSettings> </system.net>
and here's how I try to read the values from web.config
var smtp = new System.Net.Mail.SmtpClient(); var credential = new System.Net.Configuration.SmtpSection().Network; string strHost = smtp.Host; int port = smtp.Port; string strUserName = credential.UserName; string strFromPass = credential.Password;
But credentials are always null. How can i access these values?
Since no answer has been accepted, and none of the others worked for me:
using System.Configuration; using System.Net.Configuration; // snip... var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); string username = smtpSection.Network.UserName;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With