How to enable SSL for SmtpClient in Web.config

前端 未结 10 1134
再見小時候
再見小時候 2020-12-05 07:03

Is there a way to set the EnableSSL from the web.config?

I could set this property in code, but that wouldn\'t work for the Simple Mail Web Event and other classes t

相关标签:
10条回答
  • 2020-12-05 07:34

    this works for me in .net 4

    E.G. in web.config

    network host="somesmtpserver" userName="do_not_reply@yourserver.com" 
    password="whatever" port="25" enableSsl="true"         
    
    0 讨论(0)
  • 2020-12-05 07:45

    Ah, there is a way to do it for the 'forgot password' built in .net login controls though.

    See 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

    Ryan

    0 讨论(0)
  • 2020-12-05 07:47

    Giles Roberts Jan 18 '12 at 18:01 said

    this works for me in .net 4

    E.G. in web.config

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

    Port 25 is not a SSL port. Port 25 is the default SMTP port. Furthermore the web.config code is partly filled out. The code should be

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

    This settings above is more accurate then the original web.config code. I don't know witch method is better. Using web.config or using the code-behind page to send the e-mail. No matter witch method you use the code-behind file has to be modified. I say this because you have to wire up From, Subject, and Body text boxes. I'm taking it for granted that the end results that you want to send a message through an aspx web page

    0 讨论(0)
  • 2020-12-05 07:48

    I appears the class is sealed, so i made a manual extension. I thought i'd provide it for others here. Hope it can be of use to others.

    /// <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); } }
    }
    

    Use this way:

    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);
    
    0 讨论(0)
  • 2020-12-05 07:48

    Just extend the class and set EnableSsl = true and use that class.

    0 讨论(0)
  • 2020-12-05 07:50

    I have searched almost everywhere for this.

    But it seems there is no way we can configure EnableSsl Property in web.config.

    Have a look at this

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