繁体   English   中英

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

[英]unable to send emails using google Smtp client

我正在Visual Studio中开发一个asp.net mvc Web应用程序。 没有我的Web应用程序基本上不是面向公共用户的网站(因此,我没有使用https)。 现在,我的网站提供了一个普通的“与我们联系”表单,以及以下“联系后”操作方法:

[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以便能够从我的应用程序发送电子邮件吗?

尝试添加凭据属性,

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

您可以尝试将Gmail帐户设置为允许安全性较低的应用程序(启用基本身份验证)。

https://support.microsoft.com/zh-cn/kb/2984937

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM