SMTP 服务器需要安全连接或客户端未通过身份验证。
Posted
技术标签:
【中文标题】SMTP 服务器需要安全连接或客户端未通过身份验证。【英文标题】:The SMTP server requires a secure connection or the client was not authenticated. 【发布时间】:2013-12-02 20:41:19 【问题描述】:命名空间邮件代码 课堂节目 静态无效主要(字符串 [] 参数) SendEmailToInformCustomerForNewOrderMessage();
public static void SendEmailToInformCustomerForNewOrderMessage()
try
SendMail();
catch (Exception ex)
Logger.Write(CreateExceptionString(ex));
public static void SendMail()
try
// Gmail Address from where you send the mail
dynamic fromAddress = "frommail@gmail.com";
// any address where the email will be sending
dynamic toAddress = "tomail@gmail.com";
//Password of your gmail address
const string fromPassword = "password";
// Passing the values and make a email formate to display
string subject = "test mail code";
string body = "From: " + "Puneet";
body += "Email: " + "fromemail@gmail.com";
body += "Subject: " + "Test email code";
body += "Question: " + "test question";
// smtp settings
dynamic smtp = new System.Net.Mail.SmtpClient();
if (true)
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = false;
smtp.Timeout = 20000;
// Passing values to smtp object
dynamic message = new MailMessage(fromAddress, toAddress, subject, body);
smtp.Send(message);
catch (Exception ex)
Logger.Write(CreateExceptionString(ex));
public static string CreateExceptionString(Exception e)
StringBuilder sb = new StringBuilder();
CreateExceptionString(sb, e, string.Empty);
return sb.ToString();
private static void CreateExceptionString(StringBuilder sb, Exception e, string indent)
if (indent == null)
indent = string.Empty;
else if (indent.Length > 0)
sb.AppendFormat("0Inner ", indent);
sb.AppendFormat("Exception Found:" + "& vbLf &" + "0Type: 1", indent, e.GetType().FullName);
sb.AppendFormat("& vbLf &" + "0Message: 1", indent, e.Message);
sb.AppendFormat("& vbLf &" + "0Source: 1", indent, e.Source);
sb.AppendFormat("& vbLf &" + "0Stacktrace: 1", indent, e.StackTrace);
if (e.InnerException != null)
sb.Append("& vbLf &");
CreateExceptionString(sb, e.InnerException, indent + " ");
public sealed class Logger
//
// TODO: Add constructor logic here
public static void Write(string value)
StreamWriter writetext = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory+"exceptionfile.txt");
writetext.WriteLine(value);
writetext.Close();
我在这个测试 C# 控制台应用程序中发生错误。请帮助我并解决这个问题。错误
【问题讨论】:
【参考方案1】:你的代码有问题:
你需要交换这两行:
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = false;
到这里:
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
否则认证失败。
此外,gmail SMTP 服务器采用防洪系统,可防止您向其发送垃圾邮件。如果您执行的发送次数过多或尝试连接失败次数过多,可能会在短时间内禁止您的 IP。
您应该在代码中处理此类情况并稍后重试。
【讨论】:
以上是关于SMTP 服务器需要安全连接或客户端未通过身份验证。的主要内容,如果未能解决你的问题,请参考以下文章
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。 [复制]
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 需要身份验证?
System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证
错误。 SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required??? [复制]
asp.net SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required [重复]