System.Net.Mail.dll 中的 SmtpException

SmtpException in System.Net.Mail.dll

我正在尝试使用 C# 发送电子邮件,如下所示:

public string SendEmail(string employeeEmail, string clientEmail, string subject, string message)
{    
     MailMessage mailMessage = new MailMessage(employeeEmail, clientEmail)
     {
          Subject = subject,
          Body = message,
          BodyEncoding = Encoding.UTF8,
          IsBodyHtml = true
     };

     SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
     NetworkCredential basicCredential1 = new NetworkCredential("email", "password");
     client.EnableSsl = true;
     client.UseDefaultCredentials = false;
     client.Credentials = basicCredential1;

     try
     {
          if (UserExists(employeeEmail))
          {
               if (objDAL.ClientExists(clientEmail))
               {
                    client.Send(mailMessage);
                    return "Email sent";
               }
               else
               {
                    return "Client email not found";
               }
          }
          else
          {
               return "Employee email not found.";
          }
     }
     catch
     {
          return "ERROR";
     }
}

但它在 client.Send(mailMessage); 中的 System.Net.Mail.dll 中抛出了 'System.Net.Mail.SmtpException',我不知道为什么。这几天一直在找答案。。。 有人可以帮我了解发生了什么事吗?我错过了什么吗?

关于您遇到的例外情况,您必须在您的 google 帐户中启用安全性较低 Sign-In(或安全性较低的应用程序访问)。

登录 google 帐户后,转到:

https://www.google.com/settings/security/lesssecureapps 要么 https://myaccount.google.com/lesssecureapps

并启用该选项。完成此步骤后,您应该能够发送 e-mails.