c# - SMTP 服务器需要安全连接 - gmail

标签 c# asp.net azure gmail

我正在 Windows Azure 上运行 ASP.Net 应用程序,我需要从中发送电子邮件。我尝试使用我的 Gmail 帐户发送它,如下所示,但是当我尝试发送它时,出现以下错误:

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

代码:

        private static readonly string EMAIL_ADDRESS = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8397838f8782ae89838f8782c08d8183" rel="noreferrer noopener nofollow">[email protected]</a>";
        private static readonly string EMAIL_FROM = "MY FROM NAME";
        private static readonly string EMAIL_PASS = "my Password";
        private static readonly string EMAIL_HOST = "smtp.gmail.com";
        private static readonly int EMAIL_PORT = 587;
        private static readonly bool EMAIL_SSL = true;
        private static readonly SmtpDeliveryMethod EMAIL_DELIVERY_METHOD = SmtpDeliveryMethod.Network;
        private static readonly bool EMAIL_DEFAULT_CREDENTIALS = false;


        public static void SendEmail(string recipientEmail, string subject, string body) {
            var fromAddress = new MailAddress(EMAIL_ADDRESS, EMAIL_FROM);
            var toAddress = new MailAddress(recipientEmail, "MY CLIENT NAME");

            var smtp = new SmtpClient {
                Host = EMAIL_HOST,
                Port = EMAIL_PORT,
                EnableSsl = EMAIL_SSL,
                DeliveryMethod = EMAIL_DELIVERY_METHOD,
                UseDefaultCredentials = EMAIL_DEFAULT_CREDENTIALS,
                Credentials = new NetworkCredential(fromAddress.Address, EMAIL_PASS,EMAIL_HOST)
            };
            using (var message = new MailMessage(fromAddress, toAddress) {
                Subject = subject,
                Body = body,
                Priority = MailPriority.High
            }) {
                smtp.Send(message);
            }
        }

我已启用不太安全的登录,并且配置似乎没问题。有什么问题吗?

最佳答案

尝试删除 UseDefaultCredentials 属性。

如果这不起作用,请单独设置属性(不使用对象初始化程序)并确保在 UseDefaultCredentials 之后设置 Credentials

参见c# SmtpClient class not able to send email using gmail

关于c# - SMTP 服务器需要安全连接 - gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593420/

相关文章:

Azure Keyvault Sign API - 响应正文 “value” 编码格式

c# - 当 HttpContext.Current 为空时,如何在 Elmah 的 ErrorMail_Mailing 事件中的 C# 中获取服务器变量(或至少客户端 IP 地址)?

c# - 如何使用 Regex 提取带有续行的文本?

asp.net - 如何批量更新用户的安全印章? Asp.net 身份

asp.net - 从 ColdFusion 迁移到 ASP.NET

azure - Microsoft IoT Central 是否通过 Azure IoT 中心发送数据...?

c# - iCalendar 不为组织者创建事件

c# - 发起电话调用并随后返回应用程序 - Xamarin

ASP.net Core 2.0 JWT token 刷新

java - Azure 函数和应用程序服务的输出 JSON 数组被分割成单独的消息