warning CS0618: ‘System.Web.Mail.SmtpMail’ is obsolete:

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. **

Advertisements

5 thoughts on “warning CS0618: ‘System.Web.Mail.SmtpMail’ is obsolete:”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s