2012-11-19 12 views
40

से system.net/mailSettings/smtp को पढ़ने के लिए यह मेरा web.config मेल सेटिंग्स है:कैसे Web.config

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

और यहाँ कैसे मैं से मूल्यों को पढ़ने की कोशिश है web.config

var smtp = new System.Net.Mail.SmtpClient(); 
var credential = new System.Net.Configuration.SmtpSection().Network; 

string strHost = smtp.Host; 
int port = smtp.Port; 
string strUserName = credential.UserName; 
string strFromPass = credential.Password; 

लेकिन प्रमाण पत्र हमेशा शून्य हैं। मैं इन मूल्यों का उपयोग कैसे कर सकता हूं?

+1

कृपया मेरा जवाब देखने के लिए [नीचे स्क्रॉल करें] (http://stackoverflow.com/questions/13452530/reading-system-net-mailsettings-smtp-from-web-config/19960769#19960769)। ऐसा लगता है कि ज्यादातर लोगों के लिए सबसे अच्छा काम करता है। –

उत्तर

6

विन्यास, निम्न पंक्ति का उपयोग करके:

var smtp = new System.Net.Mail.SmtpClient(); 

कॉन्फ़िगर किया गया मान का उपयोग करेंगे - आप तक पहुंचने और उन्हें फिर से आवंटित करने के लिए जरूरत नहीं है।


null मूल्यों के लिए के रूप में - आप गलत तरीके से कॉन्फ़िगरेशन मान तक पहुँचने की कोशिश कर रहे हैं। आप कॉन्फ़िगरेशन से इसे पढ़ने के बजाय बस खाली SmtpSection बना रहे हैं।

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>"); 
var credentials == smtpSection.Network; 
+0

यदि आप इसे कहना चाहते थे; वर smtp = नए System.Net.Mail.SmtpClient(); स्ट्रिंग strUserName = smtp। उपयोगकर्ता नाम; स्ट्रिंग strFromPass = smtp .Password; मैं इस तरह से नहीं पहुंच सकता। – nermik

+0

@nermik - मुझे नहीं पता कि आपका क्या मतलब है। – Oded

+0

मैं सिर्फ web.config से उपयोगकर्ता नाम और पासवर्ड को होस्ट और पोर्ट की तरह एक्सेस करना चाहता हूं। – nermik

2

मुझे लगता है कि अगर आप defaultCredentials = "true" सेट आप साख होगा = अशक्त आप उन्हें प्रयोग नहीं कर रहे के रूप में की है।

क्या ईमेल भेजें जब आप कॉल करते हैं। भेजें विधि?

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
     <network defaultCredentials="false" host="localhost" port="587" 
      userName="[email protected]" password="123456"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

और यह सीएस

SmtpClient smtpClient = new SmtpClient(); 

string smtpDetails = 
    @" 
    DeliveryMethod = {0}, 
    Host = {1}, 
    PickupDirectoryLocation = {2}, 
    Port = {3}, 
    TargetName = {4}, 
    UseDefaultCredentials = {5}"; 

Console.WriteLine(smtpDetails, 
    smtpClient.DeliveryMethod.ToString(), 
    smtpClient.Host, 
    smtpClient.PickupDirectoryLocation == null 
     ? "Not Set" 
     : smtpClient.PickupDirectoryLocation.ToString(), 
    smtpClient.Port, 
    smtpClient.TargetName, 
    smtpClient.UseDefaultCredentials.ToString) 
); 
+0

नहीं, यह मुद्दा यहां नहीं है। ओपी कॉन्फ़िगरेशन से इसे पढ़ने के बजाय एक नया 'SmtpSection' बना रहा है। – Oded

+0

Emmh ने अभी देखा है .. तो यदि आप बस SmtpClient smtpClient = new SmtpClient() को कॉल करते हैं; यह आप के लिए condig सेटिंग्स पढ़ना चाहिए, लेकिन मैं लगभग यकीन है कि मैं भी सच जा रहा है डिफ़ॉल्ट credentitals चारों ओर कुछ याद कर रहा हूँ ... मैं जांच करेंगे यह – Doiremik

+0

हाँ यदि आप डिफ़ॉल्ट क्रेडेंशियल्स का उपयोग उपयोगकर्ता नाम और लोक निर्माण विभाग के रिक्त होना चाहिए। जैसा कि ओडेड ने कहा ... अब एक नया प्रमाण पत्र बनाएं ... बस अपने SmtpClient को SmtpClient smtpClient = new SmtpClient() की तरह तत्काल करें; और सेटिंग्स की जाँच – Doiremik

0

सुनिश्चित करें कि आप अपने आवेदन

75
में System.Net के संदर्भ में है:

तो

यह मेरा वेब config मेल सेटिंग्स है

चूंकि कोई जवाब स्वीकार नहीं किया गया है, और अन्य लोगों ने मेरे लिए काम नहीं किया:

using System.Configuration; 
using System.Net.Configuration; 
// snip... 
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); 
string username = smtpSection.Network.UserName; 
+1

यह बेहतर जवाब है – JsonStatham

+1

साझा करने के लिए धन्यवाद से उपयोगकर्ता नाम और लोक निर्माण विभाग के उपयोग करने के लिए! –

+0

यह मेरे लिए काम नहीं करता है। हर बार जब मैं शून्य हो रहा हूं .. कोड यहाँ जैसा ही है .. मुझे क्या याद आ रही है? – ruud

2
  //You can access the network credentials in the following way. 
      //Read the SmtpClient section from the config file  
      var smtp = new System.Net.Mail.SmtpClient(); 
      //Cast the newtwork credentials in to the NetworkCredential class and use it . 
      var credential = (System.Net.NetworkCredential)smtp.Credentials; 
      string strHost = smtp.Host; 
      int port = smtp.Port; 
      string strUserName = credential.UserName; 
      string strFromPass = credential.Password; 
26

यह ConfigurationManager का उपयोग करें और मूल्यों को मैन्युअल प्राप्त करने के लिए आवश्यक नहीं है। बस SmtpClient को तुरंत चालू करना पर्याप्त है।

SmtpClient client = new SmtpClient(); 

यह वही है MSDN कहते है:

यह निर्माता आवेदन या मशीन विन्यास फाइल में सेटिंग का उपयोग करके होस्ट, साख और पोर्ट नई SmtpClient के लिए गुण initializes।

स्कॉट गुथरी कि पर एक छोटा सा post कुछ समय पहले लिखा था।

0

डिफ़ॉल्ट प्रमाणपत्र सेट करें = "झूठा", क्योंकि जब यह सत्य पर सेट किया जाता है, तो कोई प्रमाण-पत्र उपयोग नहीं किया जाता है।