The SMTP server requires a secure connection or the client was not authenticated

落爺英雄遲暮 提交于 2019-11-28 08:55:25

Try setting the EnableSsl property to true:

smtpClient.EnableSsl = true;

AFAIK this property can only be set in code and cannot be specified in the config file.

Michael

Actually you can handle this in your web.config file by adding enableSsl="true". This worked for me and I didn't need to do anything in code.

e.g.

<network host="smtp.gmail.com" enableSsl="true" ... />

This worked for me:

  1. Need to turn on "Access for less secure apps" on google account(From Email).
  2. Set smtpclient.UseDefaultCredentials = false;
  3. Set smtpclient.EnableSsl = true;

        MailMessage mail = new MailMessage("FromEmail_Address", "ToEmail_Address");
        SmtpClient smtpclient = new SmtpClient();
        smtpclient.Port = 587;
        smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpclient.UseDefaultCredentials = false;
        smtpclient.Host = "smtp.gmail.com";
        smtpclient.UseDefaultCredentials = false;
        mail.Subject = "Email Subject";
        mail.Body = "Content of the email";
        smtpclient.EnableSsl = true;
        smtpclient.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "FromEmail_Address",
            Password = "password of the email"
        };
        client.Send(mail);
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!