C# SMTP服务器需要安全连接,或者客户端未通过身份验证。

C# SMTP服务器需要安全连接,或者客户端未通过身份验证。,c#,console,smtpclient,C#,Console,Smtpclient,命名空间邮件代码 { 班级计划 { 静态void Main(字符串[]参数) { SendEmailToInformCustomerForNewOrderMessage(); } public static void SendEmailToInformCustomerForNewOrderMessage() { try { SendMail(); } catch (Exc

命名空间邮件代码 { 班级计划 { 静态void Main(字符串[]参数) { 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("{0}Inner ", indent);
        }

        sb.AppendFormat("Exception Found:" + "& vbLf &" + "{0}Type: {1}", indent, e.GetType().FullName);
        sb.AppendFormat("& vbLf &" + "{0}Message: {1}", indent, e.Message);
        sb.AppendFormat("& vbLf &" + "{0}Source: {1}", indent, e.Source);
        sb.AppendFormat("& vbLf &" + "{0}Stacktrace: {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#控制台应用程序中出现错误。请帮助我并修复此错误。错误


您的代码中有一个问题:

您需要交换这两行:

smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = false;
为此:

smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
否则,身份验证将失败

此外,gmail SMTP服务器采用洪水控制系统,防止您发送垃圾邮件。如果您执行太多发送或尝试连接太多不成功,可能会在短时间内禁止您的IP

您应该在代码中处理此类情况,然后重试