The name Smtp does not exist in the current context
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);
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.
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.
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.
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.
精彩评论