Previously in .net 1.0 and 1.1, to send mail from a console application, you could use
using System.Web.Mail
and use a method similar to this:
static void SendMail()
{
MailMessage msg = new MailMessage();
msg.To = “toaddress1@whatever.com,toaddress2@blah.com”;
msg.From = “fromaddress@whatever.com”;
msg.Subject = “My Subject”;
msg.Body = “My Body”;
msg.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = “yourmailserveraddressorip”;
SmtpMail.Send(msg);
}
If you then upgrade to .net 2.0, you will get this when building:
warning CS0618: ‘System.Web.Mail.SmtpMail’ is obsolete: ‘The recommended alternative is System.Net.Mail.SmtpClient. http://go.microsoft.com/fwlink/?linkid=14202’
Nice huh, link doesn’t tell you much except the API is obsolete.
The old way still works, but you get a bunch of ugly warnings.
So, to fix that, you need to do this. First remove the using statement for System.Web.Mail, then add and change your mailing method as follows:
using System.Net.Mail;
static void SendMail()
{
MailMessage msg = new MailMessage();
msg.To.Add(“toaddress1@whatever.com,toaddress2@blah.com”);
msg.From = new MailAddress(“fromaddress@whatever.com”);;
msg.Subject = “My Subject”;
msg.Body = “My Body”;
msg.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient(“yourmailserveraddressorip”);
smtpClient.Send(msg);
}
As you can see, pretty close but a few subtle differences. No more warnings! 🙂
** Note that if you really want a good SendMail function, you should pass in the parameters and have overloads for options such as HTML mail, etc. Depending
on the app I am making, I’ll just throw up a email method like this, but it’s really the quick and dirty way to do it. **
6 replies on “warning CS0618: 'System.Web.Mail.SmtpMail' is obsolete:”
Great Article thanks a lot!!!
LikeLike
Excellent article! Thanks! 🙂
LikeLike
Class article…so nice of you
LikeLike
k very good
LikeLike
Thanks for taking the time to share this. Cos I was stuck and the MS help was useless for a nice change.
LikeLike
It’s useful. Thank you
LikeLike