【问题标题】:The SMTP server requires a secure connection or the client was not authenticated.SMTP 服务器需要安全连接或客户端未通过身份验证。
【发布时间】:2023-03-21 03:36:01
【问题描述】:

命名空间邮件代码 { 课堂节目 { 静态无效主要(字符串 [] 参数) { 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# 控制台应用程序中发生错误。请帮助我并解决这个问题。错误

【问题讨论】:

    标签: c# console smtpclient


    【解决方案1】:

    你的代码有问题:

    你需要交换这两行:

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

    到这里:

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

    否则认证失败。

    此外,gmail SMTP 服务器采用防洪系统,可防止您向其发送垃圾邮件。如果您执行的发送次数过多或尝试连接失败次数过多,可能会在短时间内禁止您的 IP。

    您应该在代码中处理此类情况并稍后重试。

    【讨论】:

      猜你喜欢
      • 2011-05-28
      • 2017-12-02
      • 2012-03-06
      • 2015-11-26
      • 2016-09-25
      • 2019-01-28
      相关资源
      最近更新 更多