How to read system.net/mailSettings/smtp from Web.

2020-05-14 13:46发布

This is my web.config mail settings:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
        <network defaultCredentials="true" host="localhost" port="587" userName="smthg@smthg.net" 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?

7条回答
劳资没心,怎么记你
2楼-- · 2020-05-14 14:02

Set defaultCredentials="false", because when it's set to true, no credentials are used.

查看更多
神经病院院长
3楼-- · 2020-05-14 14:06

I think if you have defaultCredentials="true" set you will have the credentials = null as you are not using them.

Does the email Send when you call the .Send method?

So

This is my web config mail settings:

<system.net>
   <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
         <network defaultCredentials="false" host="localhost" port="587"
            userName="smthg@smthg.net" password="123456"/>
      </smtp>
   </mailSettings>
</system.net>

and this is cs

SmtpClient smtpClient = new SmtpClient();

string smtpDetails =
    @"
    DeliveryMethod = {0},
    Host = {1},
    PickupDirectoryLocation = {2},
    Port = {3},
    TargetName = {4},
    UseDefaultCredentials = {5}";

Console.WriteLine(smtpDetails,
    smtpClient.DeliveryMethod.ToString(),
    smtpClient.Host,
    smtpClient.PickupDirectoryLocation == null
        ? "Not Set"
        : smtpClient.PickupDirectoryLocation.ToString(),
    smtpClient.Port,
    smtpClient.TargetName,
    smtpClient.UseDefaultCredentials.ToString)
);
查看更多
看我几分像从前
4楼-- · 2020-05-14 14:08

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;
查看更多
The star\"
5楼-- · 2020-05-14 14:09

By using the configuration, the following line:

var smtp = new System.Net.Mail.SmtpClient();

Will use the configured values - you don't need to access and assign them again.


As for the null values - you are trying accessing the configuration values incorrectly. You are just creating an empty SmtpSection instead of reading it from configuration.

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;
查看更多
你好瞎i
6楼-- · 2020-05-14 14:10

make sure you have reference to System.Net in your application

查看更多
\"骚年 ilove
7楼-- · 2020-05-14 14:11
            //You can access the network credentials in the following way.
            //Read the SmtpClient section from the config file    
            var smtp = new System.Net.Mail.SmtpClient();
            //Cast the newtwork credentials in to the NetworkCredential class and use it .
            var credential = (System.Net.NetworkCredential)smtp.Credentials;
            string strHost = smtp.Host;
            int port = smtp.Port;
            string strUserName = credential.UserName;
            string strFromPass = credential.Password;
查看更多
登录 后发表回答