2013-04-23 46 views
0

這裏是我的下面的完整代碼,請幫助,我一直試圖通過Gmail發送電子郵件,我得到這個錯誤「530-5.5.1認證要求」,異常發生在這裏的這條線上的「Send()」方法中:(參見下面的完整代碼),由於某種原因,我沒有使用C#庫,所以我需要得到它的工作,謝謝。530-5.5.1認證所需的錯誤,smtp

//EXCEPTION OCCURS HERE, SEE THE SEND METHOD BELOW FOR FULL CODE 
    foreach (string address in to) 
     { 
      try 
      { 
       message = "RCPT TO:<" + address + ">\r\n"; 
       response = ReadLine(); 
       SendCommand(message); 
       Console.WriteLine(response); 
       if (response.Substring(0, 3) != "250") 
       { 
        throw new SmtpException(response); 
       } 
      } 

     } 
    //CONSTRUCTOR 
    public Smtp() 
    { 
     Port =465 ; 
     Host = "smtp.gmail.com"; 
     Email = "[email protected]"; 
     Password = "xxxxxxx"; 
     to = new List<String>() { "[email protected]"}; 
     from = "[email protected]"; 

    } 

    //FULLE CODE 
    public void Connect() 
    { 
     if (Client == null) 
      Client = new TcpClient(); 

     if (!Client.Connected) 
      Client.Connect(Host, Port); 

     //if (IsSecure) 
     //{ 
      SslStream secureStream = 
       new SslStream(Client.GetStream()); 
      secureStream.AuthenticateAsClient(Host); 

      ClientStream = secureStream; 
      secureStream = null; 
      Console.WriteLine("secure"); 

     //} 
     //else 
     //{ 
     // ClientStream = Client.GetStream(); 
     // Console.WriteLine("non secure"); 
     //} 

     Writer = new StreamWriter(ClientStream); 
     Reader = new StreamReader(ClientStream); 

     string c= ReadLine(); 
     Console.WriteLine(c); 

     string message = "EHLO me"; 
     SendCommand(message); 
     string response = ReadLine(); 
     Console.WriteLine(response); 
     if (response.Substring(0, 3) != "250") 
     { 
     // throw new SmtpException(response); 
     } 

     Send(); 
    } 


    // public void 
    public void Send() 
    { 
     string message = "MAIL FROM:<" + Email + ">"; 
     SendCommand(message); 
     string response = ReadLine(); 
     Console.WriteLine(response); 
     if (response.Substring(0, 3) != "250") 
     { 
      throw new SmtpException(response); 
     } 
     string x = ReadLine(); 
     Console.WriteLine(x); 

     **//EXCEPTION OCCURS HERE** 
     foreach (string address in to) 
     { 
      try 
      { 
       message = "RCPT TO:<" + address + ">\r\n"; 
       response = ReadLine(); 
       SendCommand(message); 
       Console.WriteLine(response); 
       if (response.Substring(0, 3) != "250") 
       { 
        throw new SmtpException(response); 
       } 
      } 
      catch (SmtpException e) 
      { 
       System.Console.WriteLine("{ 0}", e.Message); 
      } 
     } 

     message = "DATA\r\n"; 
     SendCommand(message); 
     response = ReadLine(); 
     Console.WriteLine(response); 
     if (response.Substring(0, 3) != "354") 
     { 
      throw new SmtpException(response); 
     } 

     message = "Subject: " + subject + "\r\n"; 
     foreach (string address in to) 
     { 
      message += "To: " + address + "\r\n"; 
     } 
     foreach (string address in cc) 
     { 
      message += "Cc: " + address + "\r\n"; 
     } 

     message += "From: " + from + "\r\n"; 
     if (bodyHtml.Length > 0) 
     { 
      message += "MIME-Version: 1.0\r\n" 
       + " Content-Type: text/ html;\r\n" 
       + " charset=\" iso-8859-1\"\r\n"; 
      message += "\r\n" + bodyHtml; 
     } 
     else 
     { 
      message += "\r\n" + bodyText; 
     }; 
     message += "\r\n.\r\n"; 
     SendCommand(message); 
     response = ReadLine(); 
     Console.WriteLine(response); 
     if (response.Substring(0, 3) != "250") 
     { 
      throw new SmtpException(response); 
     } 

     message = "QUIT\r\n"; 
     SendCommand(message); 
     response = ReadLine(); 
     Console.WriteLine(response); 
     if (response.IndexOf(" 221") == -1) 
     { 
      throw new SmtpException(response); 
     } 

    } 

    public void Close() 
    { 
     if (Client != null) 
     { 
      if (Client.Connected) 
       Logout(); 

      Client.Close(); 
      Client = null; 
     } 

     if (ClientStream != null) 
     { 
      ClientStream.Close(); 
      ClientStream = null; 
     } 

     if (Writer != null) 
     { 
      Writer.Close(); 
      Writer = null; 
     } 

     if (Reader != null) 
     { 
      Reader.Close(); 
      Reader = null; 
     } 

     disposed = true; 
    } 

    public void Dispose() 
    { 
     if (!disposed) 
      Close(); 
    } 

    protected void Login() 
    { 
     if (!IsResponseOk(SendCommand("USER " + Email)) || 
      !IsResponseOk(SendCommand("PASS " + Password))) 
      throw new Exception("User/password not accepted"); 
    } 

    protected void Logout() 
    { 
     SendCommand("RSET"); 
     //SendCommand("QUIT"); 
    } 

    protected string SendCommand(string cmdtext) 
    { 
     Writer.WriteLine(cmdtext); 
     Writer.Flush(); 

     return ReadLine(); 
    } 

    protected string ReadLine() 
    { 
     return Reader.ReadLine() + "\r\n"; 
    } 

    protected string ReadLines() 
    { 
     StringBuilder b = new StringBuilder(); 

     while (true) 
     { 
      string temp = ReadLine(); 

      if (temp == ".\r\n" || temp.IndexOf("-ERR") != -1) 
       break; 

      b.Append(temp); 
     } 

     return b.ToString(); 
    } 

    protected static bool IsResponseOk(string response) 
    { 
     if (response.StartsWith("+OK")) 
      return true; 
     if (response.StartsWith("-ERR")) 
      return false; 

     throw new Exception("Cannot understand server response: " + 
      response); 
    } 
+1

顯示相關的唯一代碼從Gmail中發送郵件。通過SMTP發送電子郵件需要10行,而不是250行。無論如何庫存在,因此您不必再次解決常見問題。您可能沒有獲得Gmail的身份驗證權限。 – CodeCaster 2013-04-23 11:04:19

回答

2

顯然SMTP服務器在接受郵件發送前需要驗證。這是常見的,因爲垃圾郵件發送者和騙子曾經使用「開放式中繼」來推銷他們的產品。

檢查服務器的規則以查看它們將接受哪些驗證。在http://en.wikipedia.org/wiki/SMTP_Authentication維基百科參考可能會有用。