How to enable SSL for SmtpClient in Web.config

前端 未结 10 1146
再見小時候
再見小時候 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:55

    I think there's a bug in the MailSettingsSectionGroup. See below code:

    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;
    

    It seems that EnableSsl does not exist as a property under Network because when I run and debug this, I can see the value, but can't compile the code due to missing ExtensionMethod.

提交回复
热议问题