如何使用TCPCLIENT登陆搭到GMAIL邮箱?
前面的步骤都可以实现。但是后面的登陆却无法使用!

解决方案 »

  1.   

    你打算用什么协议访问?IMAP?POP?HTTP?
      

  2.   

    GMAIL的是SSL方式的登录,你和TCPClient是否符合要求?
      

  3.   

    就是想知道如何是TCPCLIENT来登陆gmail目的就想知道很多电子邮件列表中那些邮件对于GMAIL来说是合法的电子邮件。
      

  4.   


     /// <summary>
            /// 连接邮件服务器
            /// </summary>
            /// <param name="ServerIP">服务器地址</param>
            /// <param name="Port">服务器端口</param>
            /// <param name="UserName">用户名</param>
            /// <param name="UserPassword">密码</param>
            /// <param name="IsSSL">是否加密</param>
            /// <returns>是否连接成功</returns>
            public bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL)
            {
                try
                {
                    MyTcpClient.ReceiveTimeout = 5000;
                    MyTcpClient.SendTimeout = 5000;
                    MyTcpClient.Connect(ServerIP, Port);
                    //MyTcpClient.ReceiveTimeout = 200000;
                    //MyTcpClient.SendTimeout = 20000;
                    Stream stream;
                    if (IsSSL)
                    {
                        SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false);
                        try
                        {
                            sslStream.AuthenticateAsClient(ServerIP);
                        }
                        catch
                        {
                        }
                        stream = sslStream;
                    }
                    else
                    {
                        NetworkStream netstream = MyTcpClient.GetStream();
                        stream = netstream;
                    }
                    stream.ReadTimeout = 60000;
                    sreader = new StreamReader(stream, Encoding.Default);
                    swriter = new StreamWriter(stream);
                    string result = sreader.ReadLine();                //验证用户和密码                swriter.WriteLine("USER " + UserName);
                    swriter.Flush();                result = sreader.ReadLine();
                    if (result.Substring(0, 3) == "-ER")
                    {
                        throw new Exception("用户名错误");//没有该用户名;
                    }                swriter.WriteLine("PASS " + UserPassword);
                    swriter.Flush();                try
                    {
                        result = sreader.ReadLine();
                    }
                    catch (IOException)
                    {
                        throw new Exception("密码错误");//未能读取行
                    }                if (result.Substring(0, 3) == "-ER")
                    {
                        throw new Exception("密码错误"); //无法登录,可能使用户名密码错误!
                    }
                    this.Connected = true;//设置连接属性
                    return true;            }
                catch (Exception err)
                {
                    throw;
                }
            }
      

  5.   

    接5楼,使用SSL流连接GMail服务器就可以了,上面代码是我写的一个pop类,可以连接Gmail
      

  6.   

    to :ufostop
    能不能吧整个类发出来我看看!
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Net.Security;
    using System.Net.Sockets;namespace OpenSSL
    {
        class Program
        {
            TcpClient MyTcpClient;
            StreamReader sreader;
            StreamWriter swriter;
            /// <summary>
            /// 连接邮件服务器
            /// </summary>
            /// <param name="ServerIP">服务器地址</param>
            /// <param name="Port">服务器端口</param>
            /// <param name="UserName">用户名</param>
            /// <param name="UserPassword">密码</param>
            /// <param name="IsSSL">是否加密</param>
            /// <returns>是否连接成功</returns>
            public  bool Connect(string ServerIP, int Port, string UserName, string UserPassword, bool IsSSL)
            {
                try
                {
                    MyTcpClient.ReceiveTimeout = 5000;
                    MyTcpClient.SendTimeout = 5000;
                    MyTcpClient.Connect(ServerIP, Port);
                    //MyTcpClient.ReceiveTimeout = 200000;
                    //MyTcpClient.SendTimeout = 20000;
                    Stream stream;
                    if (IsSSL)
                    {
                        SslStream sslStream = new SslStream(MyTcpClient.GetStream(), false);
                        try
                        {
                            sslStream.AuthenticateAsClient(ServerIP);
                        }
                        catch
                        {
                        }
                        stream = sslStream;
                    }
                    else
                    {
                        NetworkStream netstream = MyTcpClient.GetStream();
                        stream = netstream;
                    }
                    stream.ReadTimeout = 60000;
                    sreader = new StreamReader(stream, Encoding.Default);
                    swriter = new StreamWriter(stream);
                    string result = sreader.ReadLine();                //验证用户和密码                swriter.WriteLine("USER " + UserName);
                    swriter.Flush();                result = sreader.ReadLine();
                    if (result.Substring(0, 3) == "-ER")
                    {
                        throw new Exception("用户名错误");//没有该用户名;
                    }                swriter.WriteLine("PASS " + UserPassword);
                    swriter.Flush();                try
                    {
                        result = sreader.ReadLine();
                    }
                    catch (IOException)
                    {
                        throw new Exception("密码错误");//未能读取行
                    }                if (result.Substring(0, 3) == "-ER")
                    {
                        throw new Exception("密码错误"); //无法登录,可能使用户名密码错误!
                    }                
                    return true;            }
                catch (Exception err)
                {
                    throw;
                }
            }        public string OperaStream( string strCmd)
            {
                swriter.WriteLine(strCmd);
                swriter.Flush();
                string result = sreader.ReadLine();
                return result; 
            }        static void Main(string[] args)
            {
                Program p = new Program();
                p.MyTcpClient = new TcpClient();
                if (p.Connect("pop.gmail.com", 995, "[email protected]", "15972572759", true) == true)
                {
                    Console.WriteLine("Connect Is OK!");
                    string Cmd = Console.ReadLine();
                    while (Cmd.ToLower() != "quit")
                    {
                        string temp = p.OperaStream( Cmd);
                        Console.WriteLine(temp);
                        Cmd = Console.ReadLine();                }
                }
                
            }
        }
    }
    这是根据U的帖子修改的。拷贝到VS里面可以直接编译的。共同学习一下!