使用Gmail的SMTP发送电子邮件

3

我正在尝试使用谷歌提供的SMTP在本地环境中发送确认邮件给用户。我已经编写了代码并提供了设置。

SmtpClient client = SiteUtilites.GetMailClient(); 

            MailAddress sender = new MailAddress(coreEmail, coreDisplayName);
            MailAddress receiver = new MailAddress(model.EmailAddress, model.Firstname + " " +model.Lastname);
            MailAddressCollection collection = new MailAddressCollection();

            MailMessage mailMessage = new MailMessage(sender, receiver);
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = "Hello";
            client.Send(mailMessage);

这是我当前的设置:

String smtpServer = ConfigurationManager.AppSettings["smtpServer"];
        String smtpUsername = ConfigurationManager.AppSettings["smtpUsername"];
        String smtpPassword = ConfigurationManager.AppSettings["smtpPassword"];
        String smtpPort = ConfigurationManager.AppSettings["smtpPort"];

        SmtpClient sc = new SmtpClient(smtpServer);
        NetworkCredential nc = new NetworkCredential(smtpUsername, smtpPassword);
        sc.UseDefaultCredentials = false;
        sc.Credentials = nc;
        sc.EnableSsl = true;
        sc.Port = 587;

我需要我的网站在https下运行吗?我只是想在本地机器上测试我的脚本。

这是它给我的错误:

SMTP服务器需要一个安全连接或客户端未通过身份验证。服务器响应是:5.5.1需要认证


1
当您尝试从某些应用程序登录时,Google 可能会阻止某些应用程序或设备的登录尝试。请查看以下答案以获取更多详细信息:http://stackoverflow.com/a/32475872/2946329 - Salah Akbari
网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接