2011-03-17 65 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。

相关问题