【问题标题】:How to enable SSL for SmtpClient in Web.config如何在 Web.config 中为 SmtpClient 启用 SSL
【发布时间】:2010-10-02 01:00:21
【问题描述】:

有没有办法从 web.config 设置 EnableSSL?

我可以在代码中设置此属性,但这不适用于简单邮件 Web 事件和其他使用默认 Smtp 服务器的类。有什么想法吗?

【问题讨论】:

    标签: asp.net .net web-config smtpclient


    【解决方案1】:

    我几乎到处都在寻找这个。

    但似乎我们无法在 web.config 中配置 EnableSsl 属性。

    看看this

    【讨论】:

      【解决方案2】:

      对于 .NET 3 及更早版本:您不能。您必须手动管理它。

      更多信息可以查看https://blogs.msdn.microsoft.com/vikas/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers/

      对于 .NET 4:可以。

      http://theoldsewingfactory.com/2011/01/06/enable-ssl-in-web-config-for-smtpclient/

      <configuration>
          <system.net>
              <mailSettings>
                  <smtp deliveryMethod=”network”>
                      <network host="localhost"
                               port="25"
                               enableSsl="true"
                               defaultCredentials="true" />
                  </smtp>
              </mailSettings>
          </system.net>
      </configuration>
      

      【讨论】:

      • 从 .net 4.0 开始,您现在可以在 webconfig 中设置 enableSsl:theoldsewingfactory.com/2011/01/06/…
      • @eKek0,作为公认的答案,您能否更新文本以说明您可以在 .net 4 中使用?请参阅 ilivewithian 的评论以获取参考。
      【解决方案3】:

      啊,有一种方法可以解决 .net 登录控件中内置的“忘记密码”。

      http://blogs.msdn.com/vikas/archive/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers.aspx

      瑞恩

      【讨论】:

        【解决方案4】:

        我有一个肮脏的解决方法(直到 .NET 4.0 出来)。它依赖于使用的端口来确定是否需要 SSL,而不是更改我的代码。

        var client = new SmtpClient();
        client.EnableSsl = client.Port == 587 || client.Port == 465;
        // This could also work
        //client.EnableSsl = client.Port != 25;
        

        我说这是一个肮脏的 hack,但它适用于我们遇到的不同配置。

        【讨论】:

          【解决方案5】:

          只需扩展类并设置 EnableSsl = true 并使用该类。

          【讨论】:

          • SmtpClient 类被密封
          【解决方案6】:

          这在 .net 4 中适用于我

          例如在 web.config 中

          network host="somesmtpserver" userName="do_not_reply@yourserver.com" 
          password="whatever" port="25" enableSsl="true"         
          

          【讨论】:

          • enableSsl="true" 实际上并不支持
          【解决方案7】:

          我认为MailSettingsSectionGroup 中存在错误。见以下代码:

          Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
          var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
          
          _smtpClient.Host = mailSettings.Smtp.Network.Host;
          _smtpClient.Port = mailSettings.Smtp.Network.Port;
          _smtpClient.EnableSsl = mailSettings.Smtp.Network.**EnableSsl**;
          _smtpClient.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
          _smtpClient.UseDefaultCredentials = false;
          

          EnableSsl 似乎不作为 Network 下的属性存在,因为当我运行和调试它时,我可以看到该值,但由于缺少 ExtensionMethod,无法编译代码。

          【讨论】:

            【解决方案8】:

            我的班级似乎是密封的,所以我做了一个手动扩展。我想我会在这里为其他人提供它。希望对其他人有用。

            /// <summary>
            /// OldSchool extension of SmtpNetWorkElement, since it's sealed.
            /// </summary>
            public class SmtpNetworkElementEx
            {
                private readonly SmtpNetworkElement m_SmtpNetWorkElement;
            
                /// <summary>
                /// Initializes a new instance of the <see cref="SmtpNetworkElementEx"/> class.
                /// </summary>
                public SmtpNetworkElementEx()
                {
                    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
                    var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            
                    if (mailSettings == null)
                        return;
            
                    m_SmtpNetWorkElement = mailSettings.Smtp.Network;
                }
            
                public string Host { get { return m_SmtpNetWorkElement.Host; } }
                public bool DefaultCredentials { get { return m_SmtpNetWorkElement.DefaultCredentials; } }
                public string ClientDomain { get { return m_SmtpNetWorkElement.ClientDomain; } }
                public string TargetName { get { return m_SmtpNetWorkElement.TargetName; } }
                public int Port { get { return m_SmtpNetWorkElement.Port; } }
                public string UserName { get { return m_SmtpNetWorkElement.UserName; } }
                public string Password { get { return m_SmtpNetWorkElement.Password; } }
                public bool EnableSsl { get { return Convert.ToBoolean(m_SmtpNetWorkElement.ElementInformation.Properties["enableSsl"].Value); } }
            }
            

            这样使用:

            var smtpSettings = new SmtpNetworkElementEx();
            
            _smtpClient.Host = smtpSettings.Host;
            _smtpClient.Port = smtpSettings.Port;
            _smtpClient.EnableSsl = smtpSettings.EnableSsl;
            _smtpClient.Credentials = new System.Net.NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
            

            【讨论】:

              【解决方案9】:

              Giles Roberts 2012 年 1 月 18 日 18:01 说

              这在 .net 4 中适用于我

              例如在 web.config 中

              network host="somesmtpserver" userName="do_not_reply@yourserver.com" 
              password="whatever" port="25" enableSsl="true" 
              

              端口 25 不是 SSL 端口。端口 25 是默认的 SMTP 端口。此外,web.config 代码已部分填写。代码应该是

                  <system.net>
                       <mailSettings>
                            <smtp deliveryMethod="Network" from="user@gmail.com">
                                   <network host="smtp.gmail.com"
                                   userName="user@gmail.com"
                                   password="********"
                                   port="587"
                                   defaultCredentials="true"
                                   enableSsl="true" />
                           </smtp>
                      </mailSettings>
                 </system.net>
              

              上面的这个设置比原来的 web.config 代码更准确。我不知道女巫的方法更好。使用 web.config 或使用代码隐藏页面发送电子邮件。无论使用哪种方法,您都必须修改代码隐藏文件。我这样说是因为您必须连接 From、Subject 和 Body 文本框。我想当然地认为你想通过一个 aspx 网页发送消息的最终结果

              【讨论】:

              • 这是我在 web.config 文件中使用的相同代码。
              【解决方案10】:

              这是我在 web.config 文件中使用的代码。

              【讨论】:

                猜你喜欢
                • 2013-03-02
                • 2012-02-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-12-13
                • 2011-07-01
                • 1970-01-01
                • 2011-01-10
                相关资源
                最近更新 更多