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

后端 未结 7 2132
萌比男神i
萌比男神i 2021-01-31 01:16

This is my web.config mail settings:


    
      

        
相关标签:
7条回答
  • 2021-01-31 01:56
                //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;
    
    0 讨论(0)
  • 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)
    );
    
    0 讨论(0)
  • 2021-01-31 01:58

    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;
    
    0 讨论(0)
  • 2021-01-31 02:07

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

    0 讨论(0)
  • 2021-01-31 02:15

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

    0 讨论(0)
  • 2021-01-31 02:16

    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.

    0 讨论(0)
提交回复
热议问题