【问题标题】:How to read system.net/mailSettings/smtp from Web.config如何从 Web.config 读取 system.net/mailSettings/smtp
【发布时间】:2012-11-07 07:11:51
【问题描述】:

这是我的web.config 邮件设置:

<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>

这是我尝试从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;

但凭据始终为空。我如何访问这些值?

【问题讨论】:

    标签: c# web-config smtp sendmail


    【解决方案1】:

    因为没有答案被接受,其他人都没有为我工作:

    using System.Configuration;
    using System.Net.Configuration;
    // snip...
    var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
    string username = smtpSection.Network.UserName;
    

    【讨论】:

    • 这是更好的答案
    • 这对我不起作用。每次我得到null..代码都和这里一样..我错过了什么?
    【解决方案2】:

    不必使用ConfigurationManager 并手动获取值。只需实例化 SmtpClient 就足够了。

    SmtpClient client = new SmtpClient();
    

    MSDN 是这么说的:

    此构造函数使用应用程序或机器配置文件中的设置初始化新 SmtpClient 的 Host、Credentials 和 Port 属性。

    Scott Guthrie 前段时间为此写了一个小 post

    【讨论】:

    • 虽然这并不能告诉您,例如,SpecifiedPickupDirectory 是否正在被使用——在这种情况下,您可能需要在 SMTP 初始化代码中关闭 EnableSSL 以避免“SSL 必须没有为取货目录交付方法启用。'。 2美分等...
    【解决方案3】:

    通过使用配置,下面这行:

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

    将使用配置的值 - 您无需再次访问和分配它们。


    至于null 值 - 您正在尝试错误地访问配置值。您只是在创建一个空的 SmtpSection,而不是从配置中读取它。

    var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
    var credentials == smtpSection.Network;
    

    【讨论】:

    • 如果你想说的话; var smtp = new System.Net.Mail.SmtpClient();字符串 strUserName = smtp .UserName;字符串 strFromPass = smtp .Password;我不能这样访问。
    • @nermik - 我不明白你的意思。
    • 我只想从 web.config 中访问用户名和密码,比如主机和端口。
    • var smtpSection = (SmtpSection)ConfigurationManager.GetSection("");我使用了它,但仍然为空。
    【解决方案4】:
                //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;
    

    【讨论】:

      【解决方案5】:

      我认为,如果您设置了 defaultCredentials="true",您将拥有 credentials = null,因为您没有使用它们。

      调用 .Send 方法时是否发送电子邮件?

      所以

      这是我的网络配置邮件设置:

      <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>
      

      这是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)
      );
      

      【讨论】:

      • 不,这不是这里的问题。 OP 正在创建一个新的SmtpSection,而不是从配置中读取它。
      • Emmh 刚刚看到.. 所以如果你只是调用 SmtpClient smtpClient = new SmtpClient();它应该为您读取 condig 设置,但我几乎可以肯定我还记得默认凭据是真的周围的东西......我会检查它
      • 是的,如果您使用默认凭据,则用户名和密码应该为空。正如 Oded 所说...现在创建一个新凭据...只需实例化您的 SmtpClient 就像 SmtpClient smtpClient = new SmtpClient();并检查设置
      • 好的,因此可以使用配置中的用户名和密码... set.. network defaultCredentials="false"
      【解决方案6】:

      确保您在应用程序中引用了 System.Net

      【讨论】:

        【解决方案7】:

        设置 defaultCredentials="false",因为当它设置为 true 时,不使用任何凭据。

        【讨论】:

          猜你喜欢
          • 2011-03-17
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          • 1970-01-01
          • 2010-11-28
          • 1970-01-01
          • 2016-07-20
          • 2010-12-26
          相关资源
          最近更新 更多