0

这是我下面的完整代码,请需要帮助,我一直在尝试通过 gmail 发送电子邮件,我收到此错误“530-5.5.1 Authentication required”,异常发生在此行中名为“Send()”的方法中这里:(请参阅下面的完整代码),由于某种原因,我没有使用 c# 库,所以我需要让它工作,谢谢。

 //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@gmail.com";
        Password = "xxxxxxx";
        to = new List<String>() { "reciever@gmail.com"};
        from = "sender@gmail.com";

    }

  //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);
    }
4

1 回答 1

2

显然,SMTP 服务器在接受要发送的邮件之前需要进行身份验证。这是司空见惯的,因为垃圾邮件发送者和诈骗者过去常常使用“开放中继”来兜售他们的商品。

检查服务器的规则以查看它们将接受什么身份验证。http://en.wikipedia.org/wiki/SMTP_Authentication上的 Wikipedia 参考资料可能有用。

于 2013-04-23T11:00:16.407 回答