2009-01-17 17 views
23

में SMTpClient के लिए SSL को सक्षम करने के लिए कैसे करें web.config से EnableSSL सेट करने का कोई तरीका है?Web.config

मैं इस संपत्ति को कोड में सेट कर सकता हूं, लेकिन यह सरल मेल वेब इवेंट और अन्य वर्ग जो डिफ़ॉल्ट SMTP सर्वर का उपयोग करता है, के लिए काम नहीं करेगा। कोई विचार?

उत्तर

21

पर एक नज़र डालें और: आप नहीं कर सकते। आपको इसे हाथ से प्रबंधित करना होगा।

अधिक जानकारी के लिए आप 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> 
+6

के रूप में एक संदेश भेजना चाहते हैं .NET 4.0 के रूप में आप अब webconfig में enableSsl सेट कर सकते हैं: http://theoldsewingfactory.com/2011/ 01/06/enable-ssl-in-web-config-for-smtpclient/ – ilivewithian

+0

@ilivewithian नहीं, आप नहीं कर सकते हैं। कम से कम मैं नहीं कर सकता। –

+0

@ eKek0, स्वीकार्य उत्तर के रूप में, क्या आप यह कहने के लिए टेक्स्ट अपडेट कर सकते हैं कि आप .NET 4 में कर सकते हैं, कृपया? संदर्भ के लिए ilivewithian की टिप्पणी देखें। –

0

मैंने इसके लिए लगभग हर जगह खोज की है।

लेकिन ऐसा लगता है कि हम web.config में EnableSsl प्रॉपर्टी को कॉन्फ़िगर नहीं कर सकते हैं।

पहले नेट 3 के लिए this

18

मैं एक गंदा वैकल्पिक हल है (जब तक .NET 4.0 बाहर आता है)। मेरे कोड को बदलने के बजाय यह निर्धारित पोर्ट पर निर्भर करता है कि एसएसएल की आवश्यकता है या नहीं।

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

मैं ने कहा कि यह एक गंदा हैक था, लेकिन यह अलग विन्यास है कि हम मुठभेड़ के लिए ठीक काम कर रहा।

+0

अच्छा कामकाज। – UpTheCreek

0

बस कक्षा का विस्तार करें और EnableSsl = true सेट करें और उस वर्ग का उपयोग करें।

5

यह मेरे लिए .NET 4

ई.जी. में काम करता है। web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true"   
+0

enableSsl = "true" वास्तव में समर्थित नहीं है –

+2

यह मेरे लिए .NET 4 में काम करता है। –

0

में मुझे लगता है कि 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 नेटवर्क के तहत एक संपत्ति के रूप में मौजूद नहीं है, लेकिन कारण ExtensionMethod लापता करने के लिए कोड संकलन नहीं कर सकते।

0

मुझे लगता है कि कक्षा सील कर दी गई है, इसलिए मैंने मैन्युअल एक्सटेंशन बनाया है। मैंने सोचा कि मैं इसे दूसरों के लिए यहां प्रदान करूंगा। उम्मीद है कि यह दूसरों के लिए उपयोग किया जा सकता है।

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

जाइल्स रॉबर्ट्स जनवरी 18 '12

यह मेरे लिए काम करता है में कहा।नेट 4

ईजी। web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true" 

पोर्ट 25 एसएसएल पोर्ट नहीं है। पोर्ट 25 डिफ़ॉल्ट एसएमटीपी पोर्ट है। इसके अलावा web.config कोड आंशिक रूप से भरा हुआ है। कोड

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

ऊपर इस सेटिंग और अधिक सटीक तब मूल web.config कोड है होना चाहिए। मुझे नहीं पता कि चुड़ैल विधि बेहतर है। ई-मेल भेजने के लिए web.config का उपयोग करना या कोड-बैक पेज का उपयोग करना। कोई फर्क नहीं पड़ता कि आप कोड-बैक फ़ाइल का उपयोग करते हुए चुड़ैल विधि को संशोधित करना है। मैं यह इसलिए कहता हूं क्योंकि आपको से, विषय, और बॉडी टेक्स्ट बॉक्स को तार करना होगा। मैं इसे स्वीकार कर रहा हूं कि अंतिम परिणाम जो आप एएसपीएक्स वेब पेज