我正在 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# - SMTP 服务器需要安全连接 - gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593420/