2012-03-23 71 views
-1

在Windows窗體我寫了一個發送郵件的代碼,我把郵件設置放在app.config中,你能檢查我的代碼嗎?它是否正確?在Windows窗體中發送郵件?

<configuration> 
    <system.net> 
<mailSettings> 
    <smtp from="[email protected]"> 
    <network host="mail.mydomain.com" password="xxxxxx" port="25" userName="[email protected]"/> 
    </smtp> 
</mailSettings> 

 MailMessage msg = new MailMessage(); 
     msg.Subject = txtSubject.Text; 
     msg.To.Add(txtTO.Text); 
     msg.From = new MailAddress("[email protected]"); 
     msg.Body = txtDescription.Text; 

     System.Net.Configuration.MailSettingsSectionGroup mMailSettings=null ; 
     string mMailHost = mMailSettings.Smtp.Network.Host; 

     SmtpClient mailClient = new SmtpClient(mMailHost); 
     mailClient.Send(msg); 
+0

這與Windows窗體有什麼關係? – MrLane 2012-03-23 06:57:04

+1

您將'mMailSettings'設置爲'null',但是您正在調用其子元素? – Raptor 2012-03-23 06:57:29

+0

我收到錯誤,System.Net.Configuration.MailSettingsSectionGroup mMailSettings; string mMailHost = mMailSettings.Smtp.Network.Host; – 2012-03-23 07:11:15

回答

0

如果你想SmtpClient使用mailSettings從app.config中,你應該使用它的參數構造函數 - 即沒有在代碼中指定的任何設置。這樣,SmtpClient會嘗試根據app.config設置自動配置,這似乎正是你想要的。