I have the following error code: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202', how do I edit this code ?
public bool SendMail(string mailTo)
{
try
{
SmtpMail.SmtpServer = ConfigurationManager.AppSettings["SmtpServer"].ToString(); //error line here
MailMessage MailMsg = new MailMessage(); //error line here
MailMsg.To = mailTo;
MailMsg.From = ConfigurationManager.AppSettings["EmailFrom"].ToString();
MailMsg.Subject = txtTitle.Text;
string strBody = txtBody.Text;
strBody = strBody + "<br><br> Nếu bạn không muốn nhận NewsLetter từ lần sau thì click vào link sau: ";
strBody = strBody + ConfigurationManager.AppSettings["Domain"].ToString() + "/RejectNewsLetter.aspx";
MailMsg.Body = strBody;
MailMsg.BodyFormat = MailFormat.Html; //error line here
MailMsg.BodyEncoding = System.Text.Encoding.UTF8;
SmtpMail.Send(MailMsg); //error line here
return true;
}
catch { return false; }
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
public bool SendMail(string mailTo)
{
try
{
var mail = new MailMessage();
mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailFrom"].ToString());
mail.To.Add(mailTo);
mail.Subject = "This is the subject";
mail.Body = "some html <b>in</b> here";
mail.IsBodyHtml = true;
var smtp = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"].ToString());
smtp.Send(mail);
return true;
}
catch { return false; }
}
And as Fei Han said, this is also not recommended because of the obsolete and bug issues.
public bool SendMail(string mailTo)
{
try
{
var mail = new MailMessage(); //error line here (1)
mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailFrom"].ToString()); //error line here
mail.To.Add(mailTo); //error line here (2)
mail.Subject = "This is the subject";
mail.Body = "some html <b>in</b> here";
mail.IsBodyHtml = true; //error line here
var smtp = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"].ToString()); //error line here
smtp.Send(mail);
return true;
}
catch { return false; }
}
Severity Code Description Project File Line
Warning CS0618 'MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage.
http://go.microsoft.com/fwlink/?linkid=14202'
Severity Code Description Project File Line
Error CS0246 The type or namespace name 'MailAddress' could not be found (are you missing a using directive or an assembly reference?)
I add reference... using System.Net.Mail; but it's warning error
This warning message was actually mentioned by both Fei Han and myself that
System.Net.Mail is also obsolete just like System.Web.Mail.
The warning message is just saying this namespace is obsolete, it can still be compiled and run.
If you insist using System.Net.Mail or System.Web.Mail, you will need to
ignore these warning messages or
learn how to disable them.
dongtrien
Error CS0246 The type or namespace name 'MailAddress' could not be found (are you missing a using directive or an assembly reference?)
This error message is truly strange, cause once you add the reference, it should include 'MailAddress' automatically like below:
Maybe you added both the System.Net.Mail and System.Web.Mail caused this error or Maybe it's some kind of bug with System.Net.Mail as mentioned before.
Thus, the current solution suggested for your project is to ignore or disable the warning message and use the System.Web.Mail, or you can try MailKit.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
85 Posts
'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMe...
Sep 10, 2019 03:56 AM|dongtrien|LINK
I have the following error code: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202', how do I edit this code ?
All-Star
40565 Points
6233 Posts
Microsoft
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 10, 2019 06:01 AM|Fei Han - MSFT|LINK
Hi dongtrien,
The classes in [System.Web.Mail namespace](https://docs.microsoft.com/en-us/dotnet/api/system.web.mail?view=netframework-4.8) have been deprecated, so you get the above warnings.
The alternative of using System.Net.Mail.SmtpClient is also not recommended for new development, it is obsolete too. And in following document, we can find that it recommend us use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead.
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8#examples
With Regards,
Fei Han
Contributor
3140 Points
983 Posts
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 10, 2019 06:03 AM|Yang Shen|LINK
Hi dongtrien,
I believe this message is more like a warning than an error message.
This means you can still use System.Web.Mail.MailMessage even though it's obsolete, just ignore the green wavelines.
Or, you can accept its recommendation to use System.Net.Mail.MailMessage to replace the obsolete one.
In this case, you might need to update your code, you can refer to Converting System.Web.Mail to System.Net.Mail to learn how to change your code.
It should be like below code:
And as Fei Han said, this is also not recommended because of the obsolete and bug issues.
Hope this can help you.
Best Regard,
Yang Shen
Member
6 Points
85 Posts
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 10, 2019 09:54 AM|dongtrien|LINK
You rewrite still get the error:
Severity Code Description Project File Line
Warning CS0618 'MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'
Severity Code Description Project File Line
Error CS0246 The type or namespace name 'MailAddress' could not be found (are you missing a using directive or an assembly reference?)
I add reference... using System.Net.Mail; but it's warning error
Contributor
3140 Points
983 Posts
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 11, 2019 03:08 AM|Yang Shen|LINK
Hi dongtrien,
This warning message was actually mentioned by both Fei Han and myself that System.Net.Mail is also obsolete just like System.Web.Mail.
The warning message is just saying this namespace is obsolete, it can still be compiled and run.
If you insist using System.Net.Mail or System.Web.Mail, you will need to ignore these warning messages or learn how to disable them.
This error message is truly strange, cause once you add the reference, it should include 'MailAddress' automatically like below:
Maybe you added both the System.Net.Mail and System.Web.Mail caused this error or Maybe it's some kind of bug with System.Net.Mail as mentioned before.
Thus, the current solution suggested for your project is to ignore or disable the warning message and use the System.Web.Mail, or you can try MailKit.
Best Regard,
Yang Shen
Member
6 Points
85 Posts
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 12, 2019 03:28 AM|dongtrien|LINK
I understand you mean by MailMessage's new technology to replace this outdated technology ? do you know ?
All-Star
40565 Points
6233 Posts
Microsoft
Re: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.Ma...
Sep 12, 2019 06:38 AM|Fei Han - MSFT|LINK
Hi dongtrien,
You can find examples about sending email using MailKit client library from following github repos.
https://github.com/jstedfast/MailKit/blob/master/Documentation/Examples/SmtpExamples.cs
With Regards,
Fei Han