当前上下文中不存在名称 Smtp

发布于 2024-10-22 09:55:04 字数 1203 浏览 1 评论 0原文

由于某种原因,我收到的 Smtp 在当前上下文中不存在,我使用名称空间“using 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);

For some reason i am recieving Smtp does not exist in the current context, im using the name space 'using System.Net.Mail;'. I have no idea why and have tried using System.Web.mail;. Hope you can help.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晨说晚安 2024-10-29 09:55:04

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

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

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

SmtpMail is marked as obsolete in newer frameworks. SmtpClient is the recommended class. Maybe this is causing your issue?

EDIT: you can either point to a lower framework in your project, or swap out the class. Either one should work for you. (http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail.aspx)

EDIT: This class was last supported in .Net 1.1 It will give a complier warning in every framework afterwards.

也只是曾经 2024-10-29 09:55:04

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

Add a reference to System.Web to your project, as well as a using System.Web.Mail and see if that helps. I'd have added this as a comment on qor72's answer, but I don't have the rep yet.

风苍溪 2024-10-29 09:55:04

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

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

Sounds like you are missing a reference to System.Net.Mail in your project. Add it in as a reference and see if that gets you further.

EDIT: Fixed reference to get to System.Net.Mail.SmtpClient.

離殇 2024-10-29 09:55:04

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

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

For errors 1,2, and 3, instantiate MailMessage like so: MailMessage mail = new MailMessage("string of sender", "string of acceptor");

For error 4, try using the Host property or ServicePoint instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文