无法使用谷歌Smtp客户端发送电子邮件

本文关键字:客户端 电子邮件 Smtp 谷歌 | 更新日期: 2023-09-27 18:08:55

我正在我的visual studio内工作的asp.net mvc web应用程序。不,我的web应用程序基本上是一个网站的公共用户(所以我不使用https)。现在我的网站提供了一个正常的联系我们的形式,和以下的Post contact动作方法:-

[HttpPost]
        public ActionResult Contact(Contact c)
        {
            var mail = new MailMessage();
            // Set the to and from addresses.
            // The from address must be your GMail account
            mail.From = new MailAddress("****@gmail.com");
            mail.To.Add(new MailAddress("****@yahoo.com"));
            // Define the message
            mail.Subject = "New Feedback from Web Site";
            mail.IsBodyHtml = false;
            //System.Environment.NewLine
            mail.Body = "Dear Sirs," + System.Environment.NewLine +
                "New Feedback has been submitted fromWeb site, with the following details :- " + System.Environment.NewLine +
                "Name :- " + c.Name + System.Environment.NewLine +
                "Email :- " + c.Email +
                "Telephone :- " + c.Telephone + System.Environment.NewLine
                +
                "Message :- " + c.Message

                ;
            // Create a new Smpt Client using Google's servers
            var mailclient = new SmtpClient();
            mailclient.Host = "smtp.gmail.com";
            mailclient.Port = 587;
            // This is the critical part, you must enable SSL
            mailclient.EnableSsl = true;
            // Specify your authentication details
            mailclient.Credentials = new System.Net.NetworkCredential(
                                             "***@gmail.com",
                                             "***");
            mailclient.Send(mail);
            TempData["message"] = string.Format("{Your Feedback has been submitted. Thanks");
            return RedirectToAction("Contact");
        }

,但这会引发以下异常:-

An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

所以我有以下问题:-

  • 我应该在我的联系人页面中使用https才能从我的应用程序发送电子邮件吗?

无法使用谷歌Smtp客户端发送电子邮件

尝试添加凭据属性,

SmtpClient mailclient = new SmtpClient("smtp.gmail.com", 587);
mailclient.Credentials = new NetworkCredential("username", "pass");

你可以试着设置你的gmail帐户允许不太安全的应用程序(启用基本身份验证)。

https://support.microsoft.com/en-us/kb/2984937