当前位置:编程学习 > C#/ASP.NET >>

各路高手能否给一个pop3正确收邮件的例子啊,先谢谢各位大侠了

 tcp = new TcpClient("pop.gmail.com", 110);//有错误啊
            tcp = new TcpClient();  
            s1 = tcp.GetStream();
            s2 = new StreamReader(s1);
            string a = s2.ReadLine(); --------------------编程问答--------------------

string strServerName = "";//邮件服务器名
                string strUserName = "";//用户名
                string strPassWord = ""; //密码
                StringBuilder strBuildeMailMessage = new StringBuilder();
                int numberOfBytesRead = 0;
                int length = 1024 * 10;
                byte[] buttfer = new byte[length];
                TcpClient tp = new TcpClient(strServerName, 110);
                tp.ReceiveTimeout = 1000 * 300;
                NetworkStream ns = tp.GetStream();
                ns.ReadTimeout = 1000 * 300;
                ns.Read(buttfer, 0, buttfer.Length);

                byte[] outbytes = System.Text.Encoding.ASCII.GetBytes("user " + strUserName + "\r\n");
                ns.Write(outbytes, 0, outbytes.Length);
                numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
                strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead));

                strBuildeMailMessage = new StringBuilder();
                outbytes = System.Text.Encoding.ASCII.GetBytes("pass " + strPassWord + "\r\n");
                ns.Write(outbytes, 0, outbytes.Length);
                numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
                strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead));


                Regex re = new Regex(@"(?<=has\s+)[\d]+(?=\s+messages)");
                int num = int.Parse(re.Match(strBuildeMailMessage.ToString()).Value.Trim());


                List<string> emailList = new List<string>();
                for (int i = 1; i <= num; i++)
                {
                    strBuildeMailMessage = new StringBuilder();
                    outbytes = System.Text.Encoding.ASCII.GetBytes("retr " + i.ToString() + "\r\n");
                    ns.Write(outbytes, 0, outbytes.Length);
                    Thread.Sleep(800);
                    while (ns.DataAvailable)
                    {
                        numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
                        strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead));
                    }
                    emailList.Add(strBuildeMailMessage.ToString());
                }
                outbytes = System.Text.Encoding.ASCII.GetBytes("quit\r\n");
                ns.Write(outbytes, 0, outbytes.Length);

--------------------编程问答-------------------- emailList就是每封邮件的内容 --------------------编程问答-------------------- 服务器名称应该怎么写才对啊,我用的是gmail,110端口默认是开启的
TcpClient tcp=new TcpClient("pop.gmail.com",110);//这句异常
异常内容:(我哪里是不是写错了)
由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 74.125.155.109:110 --------------------编程问答-------------------- 帖子又沉了。。。急 --------------------编程问答-------------------- 顶。。。。 --------------------编程问答-------------------- 看看端口号是否正常,没有使用过gmail,看网上的资料貌似是995 --------------------编程问答-------------------- tcp.GetStream();//异常"不允许对非连接的套接字执行此操作。"
tcp.Connected的值是false,没有连接上啊...该怎么解决啊 --------------------编程问答-------------------- 没人回答么?救命、。。 --------------------编程问答-------------------- 帖子加分了。。。。。 --------------------编程问答-------------------- string strServerName = "pop.gmail.com";//邮件服务器名
                string strUserName = "";//用户名
                string strPassWord = ""; //密码
                StringBuilder strBuildeMailMessage = new StringBuilder();
                int numberOfBytesRead = 0;
                int length = 1024 * 10;
                byte[] buttfer = new byte[length];
                TcpClient tp = new TcpClient(strServerName, 995);
                tp.ReceiveTimeout = 1000 * 300;
                NetworkStream ns = tp.GetStream();
                ns.ReadTimeout = 1000 * 300;
                ns.Read(buttfer, 0, buttfer.Length);

将端口修改为995,测试连接状态为true你试试 --------------------编程问答-------------------- Connected=true,但是
NetWorkStream nwStream=tcp.GetStream();//这一句有异常
异常内容是:
不允许对非连接的套接字执行此操作。


请赐教! --------------------编程问答-------------------- tcp.Client....
EnableBroadcast “tcp.Client.EnableBroadcast”引发了“System.Net.Sockets.SocketException”类型的异常 bool {System.Net.Sockets.SocketException}
--------------------编程问答-------------------- 你想读取信息直接读取buttfer就行了 --------------------编程问答-------------------- 刚学.net
能不能写些代码看看啊, --------------------编程问答-------------------- 1楼的就是完整接受邮件的,只要你把服务器名,端口,用户名,密码设置一下就可以了 --------------------编程问答-------------------- --------------------编程问答-------------------- int num = int.Parse(re.Match(strBuildeMailMessage.ToString()).Value.Trim());
strBuilderMailMessage.ToString()  的值是""空啊 --------------------编程问答-------------------- 请高手继续指教、、、 --------------------编程问答-------------------- gmail 
smtp/pop3/imap 都是用 ssl 协议的,

端口分别为:

465
995
993

建立 tcp 连接后, 要立马进行 ssl 协商, 获得一个 ssl stream, 才开始正常通讯
--------------------编程问答-------------------- 帖子有沉了,,,,,请高手不吝赐教,,,加分 --------------------编程问答-------------------- 差不多样子:

        NetworkStream networkStream = new NetworkStream(_socket, true);
        if (m_useSSL)
        {
          // Create an SSL stream that will close the client's stream.
          SslStream sslStream = new SslStream(
              networkStream,
              false,
              new RemoteCertificateValidationCallback(ValidateServerCertificate),
              null
              );
          try
          {
            if (null != _proxy)
              sslStream.AuthenticateAsClient(_proxy.TargetHost);
            else
              sslStream.AuthenticateAsClient(sHost);
          }
          catch (System.Security.Authentication.AuthenticationException e)
          {
            string lastError = e.Message;
            if (e.InnerException != null)
            {
              lastError += Environment.NewLine + e.InnerException.Message;
            }
            OnLog(LogType.ERROR, lastError);
            _networkStream.Close();
            return ImapResponseEnum.IMAP_FAILURE_RESPONSE;
          }

          _networkStream = sslStream;
        }//end of usessl
--------------------编程问答-------------------- 能不能发的全一点啊,代码。。。 --------------------编程问答-------------------- 顶
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,