मुझे लगता है कि कक्षा सील कर दी गई है, इसलिए मैंने मैन्युअल एक्सटेंशन बनाया है। मैंने सोचा कि मैं इसे दूसरों के लिए यहां प्रदान करूंगा। उम्मीद है कि यह दूसरों के लिए उपयोग किया जा सकता है।
/// <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); } }
}
इस तरह का प्रयोग करें 18:01 पर
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);
स्रोत
2011-08-24 08:45:05
के रूप में एक संदेश भेजना चाहते हैं .NET 4.0 के रूप में आप अब webconfig में enableSsl सेट कर सकते हैं: http://theoldsewingfactory.com/2011/ 01/06/enable-ssl-in-web-config-for-smtpclient/ – ilivewithian
@ilivewithian नहीं, आप नहीं कर सकते हैं। कम से कम मैं नहीं कर सकता। –
@ eKek0, स्वीकार्य उत्तर के रूप में, क्या आप यह कहने के लिए टेक्स्ट अपडेट कर सकते हैं कि आप .NET 4 में कर सकते हैं, कृपया? संदर्भ के लिए ilivewithian की टिप्पणी देखें। –