2
我試圖從窗戶保存表單到的app.config以下設置在.NET應用WF保存到mailSettings在App.Config中
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="ops01" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
我嘗試使用下面的代碼來設置但是,我收到配置是隻讀的錯誤。
private void SaveMailSettings()
{
var smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
try
{
if (smtpSection == null) return;
smtpSection.Network.Host = txtSmtpHost.Text;
smtpSection.Network.Port = Convert.ToInt32(txtSmtpPort.Text);
smtpSection.From = txtSmtpFrom.Text;
if (smtpSection.Network.UserName != null) smtpSection.Network.UserName = txtSmtpUserName.Text;
if (smtpSection.Network.Password != null) smtpSection.Network.Password = txtSmtpPassword.Text;
Logger.Log(string.Format("Host: {0}, Port: {1}, From: {2}, UserName: {3}, Password: {4}", smtpSection.Network.Host, smtpSection.Network.Port, smtpSection.Network.UserName, smtpSection.Network.Password), "INFO");
}
catch (Exception ex)
{
Logger.Log(ex.Message, "ERROR:");
}
}
有沒有什麼辦法可以更新App.Config文件mailSettings?
可能重複http://stackoverflow.com/questions/4100215/how-to-programmatically-store-save- smtp-server-details-back-to-web-config) –
我試過了,無法啓動它。 – Nim
對不起,引用的問題是在web項目 –