1

出于某种原因,我收到 Smtp 在当前上下文中不存在,我使用名称空间“使用 System.Net.Mail;”。我不知道为什么并尝试使用 System.Web.mail;。希望你能帮忙。

        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);
4

4 回答 4

3

SmtpMail 在较新的框架中被标记为过时的。SmtpClient 是推荐的类。也许这会导致您的问题?

编辑:您可以指向项目中的较低框架,也可以换出该类。任何一个都应该为你工作。(http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail.aspx)

编辑:这个类最后在.Net 1.1中得到支持,之后它会在每个框架中给出一个编译器警告。

于 2011-03-17T14:00:20.283 回答
0

在您的项目中添加对System.Web的引用,using System.Web.Mail并查看是否有帮助。我会将此添加为对 qor72 答案的评论,但我还没有代表。

于 2011-03-17T14:06:32.830 回答
0

听起来您在项目中缺少对 System.Net.Mail 的引用。将其添加为参考,看看是否能让你走得更远。

编辑:修复了对 System.Net.Mail.SmtpClient 的引用。

于 2011-03-17T13:57:48.223 回答
0

对于错误 1,2 和 3,像这样实例化 MailMessage: MailMessage mail = new MailMessage("string of sender", "string of acceptor");

对于错误 4,请尝试改用 Host 属性或 ServicePoint。

于 2011-03-17T15:55:09.207 回答