SMTP服务器需要安全连接或客户端未经过身份验证。

时间:2013-11-19 10:33:41

标签: c# console smtpclient

命名空间mailcode {     课程     {         static void Main(string [] args)         {             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#控制台应用程序中发生错误。请帮帮我解决这个问题。错误

1 个答案:

答案 0 :(得分:0)

您的代码中存在问题:

你需要交换这两行:

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

对此:

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

否则验证失败。

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

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