Search code examples
c#asp.net-core-mvcsmtp

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required in .NET Core


I get this error

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.

when the user tries to send a forgotten password email from the website. Help me, how can I fix it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace ERP.Senders
{
    public class SendEmail
    {
        public static void Send(string to,string subject,string body)
        {
            MailMessage mail = new MailMessage();

            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("noreply.erpsystemmu@gmail.com","سامانه مدیریت منابع انسانی ERP");
            mail.To.Add(to);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;

            //System.Net.Mail.Attachment attachment;
            // attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
            // mail.Attachments.Add(attachment);
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("noreply.erpsystemmu@gmail.com", "*******");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
        }
    }
}

Solution

  • I believe you will need to create an app password, as using the standard email address and password with the Less Secure App Access way of sending emails has been deprecated. I believe you will need to create an app password and use that, coupled with 2-step verification