2012-11-19 35 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

如果你想說的話; var smtp = new System.Net.Mail.SmtpClient(); string strUserName = smtp .UserName; string strFromPass = smtp .Password; 我無法像這樣訪問。 – nermik

+0

@nermik - 我不知道你的意思。 – Oded

+0

我只想從Web.config中訪問UserName和Password,如主機和端口。 – nermik

2

我想如果你有defaultCredentials =「true」設置你將有憑據= null,因爲你沒有使用它們。

當您調用.Send方法時發送電子郵件嗎?

所以

這是我的web配置郵件設置:

<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> 

,這是CS

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

不,這不是問題。 OP正在創建一個新的'SmtpSection',而不是從配置中讀取它。 – Oded

+0

嗯剛剛看到..所以如果你只是打電話給SmtpClient smtpClient =新的SmtpClient();它應該爲你讀取condig設置,但是我幾乎可以確定我還記得默認的credentitals是真的......我會檢查它 – Doiremik

+0

是的,如果你使用默認憑證,用戶名和密碼應該是空的。正如Oded所說...現在創建一個新的證書...就像SmtpClient一樣瞬間SmtpClient smtpClient =新的SmtpClient();並檢查設置 – Doiremik

0

確保你的應用程序

75
有參考System.Net

由於沒有答案被接受,而其他人都沒有爲我工作:

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

這對我不起作用。每次我得到null ..代碼和這裏一樣..我錯過了什麼? – 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屬性。

Scott Guthrie在前段時間寫了一個小的post

0

設置defaultCredentials =「false」,因爲當它設置爲true時,不使用憑證。