2011-03-17 20 views
1

由於某些原因,我正在接收Smtp在當前上下文中不存在,即時使用名稱空間'using System.Net.Mail;'。我不知道爲什麼,並嘗試使用System.Web.mail ;.希望你能幫助。當前上下文中不存在Smtp的名稱

 MailMessage mail = new MailMessage(); 
       SmtpClient SmtpMail = new SmtpClient(); 

       SmtpMail.Credentials = new System.Net.NetworkCredential("username", "password"); 
       mail.From = ""; //Error 1 Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'  

       mail.To = ""; // Error 2 Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only 
           // Error 3 Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddressCollection' 


       mail.Subject = "password";   
       mail.Body = "test";   
       SmtpMail.SmtpServer = "smtp.brad.ac.uk"; // Error 4 'System.Net.Mail.SmtpClient' does not contain a definition for 'SmtpServer' and no extension method 'SmtpServer' accepting a first argument of type 'System.Net.Mail.SmtpClient' could be found (are you missing a using directive or an assembly reference?) 

       SmtpMail.Send(mail); 
+0

請指出編譯器停止的位置? – 2011-03-17 13:58:02

+1

我在該片段中看不到任何裸露的標識符「Smtp」...複製時是否錯過了某些內容? – 2011-03-17 13:58:06

+0

你在哪裏實例化SmtpClient()? – curtisk 2011-03-17 13:58:38

回答

0

聽起來就像你在項目中缺少對System.Net.Mail的引用。將它作爲參考添加進來,看看是否讓你更進一步。

編輯:修改引用到System.Net.Mail.SmtpClient。

+1

我很確定沒有'System.Mail'。 – Kobi 2011-03-17 13:59:37

+0

這就是我想要做的事情,在引用中沒有System.Mail。 – 2011-03-17 14:19:41

+0

林肯定我缺少某種裝配參考壽。 – 2011-03-17 14:20:13

3

SmtpMail在較新的框架中被標記爲過時。 SmtpClient是推薦的類。也許這是造成你的問題?

編輯:您可以指向您的項目中較低的框架,或換出課程。任何一個人都應該爲你工作。 (http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail.aspx)

編輯:這個類最後在.net 1.1中支持它會給每一個編譯器警告之後的框架。

+0

; using System.Net.Mail.SmtpClient; 非上述工作出於某種原因。 – 2011-03-17 14:27:16

+0

您的項目針對哪個框架?您是否將System.Net.Mail添加爲項目中的參考?只是「使用」一個DLL是不夠的。 – 2011-03-17 14:52:14

+0

此外,請確保您沒有針對客戶端框架。我不認爲.Net.Mail是客戶端框架集的一部分。 – 2011-03-17 14:53:29

0

添加一個對System.Web的參考到您的項目,以及using System.Web.Mail,看看是否有幫助。我已經添加了這個作爲對qor72的答案的評論,但我還沒有代表。

+0

; using System.Net.Mail.SmtpClient; 非上述工作出於某種原因。 – 2011-03-17 14:25:22

0

對於錯誤1,2和3,實例化MailMessage,如下所示:MailMessage mail = new MailMessage(「sender of string」,「acceptor」);

對於錯誤4,請嘗試使用Host屬性或ServicePoint。

相關問題