locked
'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage ? RRS feed

  • Question

  • User-663551687 posted

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

    Tuesday, September 10, 2019 3:56 AM

All replies

  • User61956409 posted

    Hi dongtrien,

    'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. 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

    Tuesday, September 10, 2019 6:01 AM
  • User-719153870 posted

    Hi dongtrien,

    dongtrien

    'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'

    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:

    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.

    Hope this can help you.

    Best Regard,

    Yang Shen

    Tuesday, September 10, 2019 6:03 AM
  • User-663551687 posted

    You rewrite still get the error:

    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

    Tuesday, September 10, 2019 9:54 AM
  • User-719153870 posted

    Hi dongtrien,

    Warning CS0618 'MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202' 

    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.

    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.

    Best Regard,

    Yang Shen

    Wednesday, September 11, 2019 3:08 AM
  • User-663551687 posted

    I understand you mean by MailMessage's new technology to replace this outdated technology ? do you know ?

    Thursday, September 12, 2019 3:28 AM
  • User61956409 posted

    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

    Thursday, September 12, 2019 6:38 AM