2010-01-07 7 views
7

से smtp सर्वर विवरणों को प्राप्त करने मैं निम्नलिखित एसएमटीपी विवरण web.configकैसे प्रोग्राम के रूप में web.config

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="mail.domain.com" port="25" userName="username" password="password" defaultCredentials="true"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

कैसे मैं एक सी # वर्ग के भीतर से इन मूल्यों को प्राप्त कर सकते हैं में जमा हो जाती है।

उत्तर

18
Configuration configurationFile = WebConfigurationManager 
    .OpenWebConfiguration("~/web.config"); 
MailSettingsSectionGroup mailSettings = configurationFile 
    .GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; 
if (mailSettings != null) 
{ 
    int port = mailSettings.Smtp.Network.Port; 
    string host = mailSettings.Smtp.Network.Host; 
    string password = mailSettings.Smtp.Network.Password; 
    string username = mailSettings.Smtp.Network.UserName; 
} 
+2

एक छोटा सा निट: नहीं "~/web.config" और अधिक विश्वसनीय होगा? –

+0

सही, मैंने इसे ठीक किया। –

1

यदि आपको इस मेल-सर्वर-विवरण के साथ ईमेल भेजने की आवश्यकता है तो आपको सेटिंग्स को पढ़ने और लागू करने की आवश्यकता नहीं है। इन सेटिंग्स को आवेदन में निहित रूप से लागू किया जाता है।

यदि आप इसे किसी अन्य कारण से पढ़ रहे हैं तो मैं डारिन के उत्तर के समान कुछ लिखने वाला था। लेकिन जैसे ही मैं लिख रहा था, मैंने पाया कि उसने उत्तर दिया है, इसलिए अगर आपको वास्तव में पढ़ने की ज़रूरत है तो कृपया उसका जवाब देखें। :)

0

के बारे में क्या:

string fullpath = @"C:\FullPath\YourFile.config"; 
string configSection = "system.net/mailSettings"; 
Configuration config = ConfigurationManager.OpenExeConfiguration(fullpath); 
MailSettingsSectionGroup settings = 
    config.GetSectionGroup(configSection) as MailSettingsSectionGroup; 
संबंधित मुद्दे