日期:2014-05-18 浏览次数:20429 次
Imports System.IO Imports System.Net Imports System.Net.Sockets Public Class Form1 Private Function Connect(ByVal strServer As String, ByVal strUser As String, ByVal strPwd As String) As NetworkStream Dim Sender As New TcpClient(strServer, 110) Dim OutBytes As Byte() Dim input As String Dim Ns As NetworkStream = Nothing Try Ns = Sender.GetStream() Dim sr As New StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312")) input = "user " & strUser & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) Ns.Write(OutBytes, 0, OutBytes.Length) Console.WriteLine(sr.ReadLine()) input = "pass " & strPwd & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) Ns.Write(OutBytes, 0, OutBytes.Length) Console.WriteLine(sr.ReadLine()) Console.WriteLine("==========================================================") Return Ns Catch ex As Exception MsgBox("无法连接到服务器", MsgBoxStyle.Critical) Return Ns End Try End Function Private Function GetNumberOfNewMessages() As Integer Dim OutBytes As Byte() Dim input As String Dim strTemp As String GetNumberOfNewMessages = -1 Try Dim Ns As NetworkStream = Connect("pop3.163.com", "username", "password") '这里输入你自己的用户名和密码 If (Ns Is Nothing) Then Exit Function Dim sr As New StreamReader(Ns, System.Text.Encoding.GetEncoding("gb2312")) input = "stat" & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) Ns.Write(OutBytes, 0, OutBytes.Length) Dim resp As String = sr.ReadLine() Console.WriteLine(resp) MsgBox(resp) Dim tokens As String() = resp.Split(" ") input = "UIDL 3" & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) Ns.Write(OutBytes, 0, OutBytes.Length) resp = sr.ReadLine Console.WriteLine(resp) MsgBox("Uidl:" & resp) input = "RETR 3" & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) RichTextBox1.Text = "请稍候.." RichTextBox1.Refresh() Dim wenzhou As String = "" Do Ns.Write(OutBytes, 0, OutBytes.Length) Console.WriteLine(resp) resp = sr.ReadLine wenzhou &= resp & vbCrLf Loop While resp <> "." RichTextBox1.Text = wenzhou input = "quit" & vbCrLf OutBytes = System.Text.Encoding.ASCII.GetBytes(input) Ns.Write(OutBytes, 0, OutBytes.Length) Console.WriteLine(sr.ReadLine()) sr.Close() Ns.Close() Return Val(tokens(1)) Catch ex As