【问题标题】:Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required错误:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required
【发布时间】:2017-02-17 04:17:01
【问题描述】:

我正在使用以下代码发送电子邮件。当我试图发送邮件时,我收到了错误消息

MailMessage mail = new MailMessage(from.txt, to.txt, subject, body);
SmtpClient clint = new SmtpClient();
//for determile email smtp...
string x = from.txt;
int startIndex = x.IndexOf('@');
int endIndex = x.LastIndexOf('.');
int length = endIndex - startIndex;
string xx = x.Substring(startIndex + 1, length - 1);

if (xx == "gmail" || xx == "Gmail")
{
    clint.Host = "smtp.gmail.com";
    clint.Port = 587;
    clint.EnableSsl = true;
}
if (xx == "Hotmail" || xx == "hotmail" || xx == "live" || xx == "Live")
{
    clint.Host = "smtp.live.com";
    clint.Port = 587;
    clint.EnableSsl = true;
}
if (xx == "yahoo" || xx == "Yahoo")
{
    clint.Host = "smtp.mail.yahoo.com";
    clint.Port = 465;
    clint.EnableSsl = true;
}
clint.Credentials = new System.Net.NetworkCredential(username, password);
clint.DeliveryMethod = SmtpDeliveryMethod.Network;
clint.UseDefaultCredentials = false;
clint.Send(mail);
MetroMessageBox.Show(this, "Email Successfully Send", "Success",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);

以及如何将任何文件附加到此电子邮件

【问题讨论】:

  • 用户名应该是完整的电子邮件地址。你是吗?
  • 启用双重身份验证(又称两步验证),然后生成应用程序专用密码。使用新生成的密码通过 SMTP 进行身份验证。
  • 为什么不直接使用服务提供商允许您使用的 SMTP 服务器并使其更容易使用?
  • 如果您使用 gmail 测试此代码,您将必须启用从 here 访问您的 google 帐户中不太安全的应用程序。还要在clint.Credentials = new System.Net.NetworkCredential(username, password); 之前设置clint.UseDefaultCredentials = false;

标签: c# email smtp


【解决方案1】:

对于你得到的错误,我在网上看到你应该在 Credentials 行之前使用 UseDefaultCredentials 并且你的 clint 对象的 EnableSsl 应该设置为 true。

Reference here

以及如何将任何文件附加到此电子邮件

您可以通过向邮件对象添加附件:

mail.Attachments.Add(new Attachment(filename));

我忘了提到 Gmail 中的双重身份验证可能是个问题。如果是这样,请参考我之前链接的链接中的第二个解决方案。

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 2015-05-01
    • 2014-01-21
    • 2015-05-18
    • 2014-11-08
    • 2016-05-16
    • 2013-10-31
    相关资源
    最近更新 更多