使用ASP.Net Core的电子邮件发送错误

时间:2018-07-04 20:19:08

标签: c# asp.net-core smtp gmail

我正在尝试通过Gmail服务器使用ASP.NET发送电子邮件。这是我的代码

 public ActionResult Index()
    {

        var fromAddress = new MailAddress("hosein.dafeyan@gmail.com");
        var fromPassword = "XXXXXXX";
        var toAddress = new MailAddress("hosein.dafeyan.taral@gmail.com");
        string subject = "Test sending mail";
        string body = "Hello Hosein";
        string ExeptionMessage = "";
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        try
        {
            smtp.Send(message);
        }
        catch (Exception Ex)
        {
            ExeptionMessage = Ex.Message;
        }

        return View();
    }

但是我得到的错误是:

  

SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。

0 个答案:

没有答案