簡體   English   中英

SMTP服務器需要安全連接,或者客戶端未通過身份驗證。

[英]The SMTP server requires a secure connection or the client was not authenticated.

命名空間郵件代碼{類Program {靜態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#控制台應用程序中發生錯誤。 請幫助我並解決此問題。 錯誤

您的代碼有問題:

您需要交換這兩行:

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

對此:

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

否則,身份驗證將失敗。

gmail SMTP服務器還采用了泛洪控制系統,可防止您向其發送垃圾郵件。 如果執行太多發送或嘗試進行太多失敗連接,可能會在短時間內禁止IP。

您應該在代碼中處理此類情況,然后再試一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM