How to read system.net/mailSettings/smtp from Web.config
Asked Answered
H

7

66

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?

Havelock answered 19/11, 2012 at 11:9 Comment(0)
P
115

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;
Peewee answered 13/11, 2013 at 17:45 Comment(2)
This is the better answerAudreyaudri
This does not work for me. Every time I am getting null.. The code is same as here.. What am I missing?Semiweekly
O
38

It is not necessary to use the ConfigurationManagerand get the values manually. Simply instantiating an SmtpClient is sufficient.

SmtpClient client = new SmtpClient();

This is what MSDN says:

This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files.

Scott Guthrie wrote a small post on that some time ago.

Offering answered 20/2, 2014 at 8:41 Comment(1)
Though that doesn't tell you if, for instance, SpecifiedPickupDirectory is being used -- in which case you might need to turn EnableSSL off in your SMTP init code to avoid 'SSL must not be enabled for pickup-directory delivery methods.'. 2¢, etc...Tome
P
9

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;
Paraph answered 19/11, 2012 at 11:13 Comment(4)
If you wanted to say it; var smtp = new System.Net.Mail.SmtpClient(); string strUserName = smtp .UserName; string strFromPass = smtp .Password; I can't access like this.Havelock
@Havelock - I don't know what you mean.Paraph
I just want to access UserName and Password from web.config like Host and port.Havelock
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>"); I used it but still null.Havelock
I
3

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="[email protected]">
         <network defaultCredentials="false" host="localhost" port="587"
            userName="[email protected]" 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)
);
Ibidem answered 19/11, 2012 at 11:19 Comment(4)
Nope, that's not the issue here. The OP is creating a new SmtpSection instead of reading it from the configuration.Paraph
Emmh just seen that.. so if you just call SmtpClient smtpClient = new SmtpClient(); it should read the condig settings for you but I'm nearly sure I also remember something around the default credentitals being true... I'll check itIbidem
Yes if you use default credentials the Username and pwd should be empty. As Oded said... now create a new credential... just instantite your SmtpClient like SmtpClient smtpClient = new SmtpClient(); and check the settingsIbidem
ok so to use the UserName and Pwd from your config... set.. network defaultCredentials="false"Ibidem
I
3
            //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;
Indictable answered 9/12, 2013 at 7:33 Comment(0)
T
0

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

Turn answered 1/4, 2013 at 15:33 Comment(0)
R
-1

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

Risotto answered 31/10, 2017 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.