检测POP3邮件服务器上的邮件(VB.NET)

'**************************
'检查邮件例子
'作者:wgscd
' qq 153964481
'*************************
Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

 1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
 2Me.components = New System.ComponentModel.Container   
 3Me.Button1 = New System.Windows.Forms.Button   
 4Me.Timer1 = New System.Windows.Forms.Timer(Me.components)   
 5Me.ListBox1 = New System.Windows.Forms.ListBox   
 6Me.SuspendLayout()   
 7'   
 8'Button1   
 9'   
10Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))   
11Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat   
12Me.Button1.Location = New System.Drawing.Point(16, 8)   
13Me.Button1.Name = "Button1"   
14Me.Button1.Size = New System.Drawing.Size(104, 40)   
15Me.Button1.TabIndex = 0   
16Me.Button1.Text = "checkMai"   
17'   
18'Timer1   
19'   
20Me.Timer1.Interval = 2000   
21'   
22'ListBox1   
23'   
24Me.ListBox1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))   
25Me.ListBox1.HorizontalScrollbar = True   
26Me.ListBox1.ItemHeight = 12   
27Me.ListBox1.Location = New System.Drawing.Point(16, 64)   
28Me.ListBox1.Name = "ListBox1"   
29Me.ListBox1.Size = New System.Drawing.Size(104, 100)   
30Me.ListBox1.TabIndex = 2   
31'   
32'Form1   
33'   
34Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
35Me.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))   
36Me.ClientSize = New System.Drawing.Size(146, 182)   
37Me.Controls.Add(Me.ListBox1)   
38Me.Controls.Add(Me.Button1)   
39Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow   
40Me.Name = "Form1"   
41Me.Text = "MSG------By wgscd"   
42Me.ResumeLayout(False) 
43
44End Sub 
45
46#End Region 
47
48Sub checkMail()   
49Dim ip As IPAddress = Dns.GetHostByName(Dns.GetHostName.ToString).AddressList(0)   
50Dim ipe As New IPEndPoint(ip, 2345)   
51Me.Text = ip.ToString   
52Dim tcpc As New TcpClient(ipe)   
53tcpc.Connect(Dns.GetHostByName("pop3.126.com").HostName, 110)   
54Dim ntstream As NetworkStream   
55Dim sr As StreamReader   
56Dim sw As StreamWriter   
57ntstream = tcpc.GetStream   
58Me.ListBox1.Items.Add(ReadFromNetStream(ntstream))   
59WriteToNetStream(ntstream, "user wgscd")   
60Me.ListBox1.Items.Add(ReadFromNetStream(ntstream))   
61WriteToNetStream(ntstream, "pass yourpassword")   
62Dim msg As String = ReadFromNetStream(ntstream)   
63If msg Like "+OK 0 message*" Then   
64MsgBox("No Mail Right now!")   
65Me.ListBox1.Items.Add(ReadFromNetStream(ntstream))   
66Else   
67MsgBox(msg)   
68WriteToNetStream(ntstream, "list")   
69Me.ListBox1.Items.Add(ReadFromNetStream(ntstream))   
70End If   
71WriteToNetStream(ntstream, "quit")   
72Me.ListBox1.Items.Add(ReadFromNetStream(ntstream))   
73tcpc.Close()   
74End Sub 
75
76Function ReadFromNetStream(ByRef NetStream As NetworkStream) As String   
77Dim bb As Byte() = New Byte(521) {}   
78NetStream.Read(bb, 0, bb.Length)   
79Dim read As String = System.Text.Encoding.UTF8.GetString(bb)   
80Return read   
81End Function   
82Function WriteToNetStream(ByRef NetStream As NetworkStream, ByVal command As String) As String   
83Dim stringToSend As String = command &amp; vbCrLf   
84Dim arrayToSend As Byte() = System.Text.Encoding.UTF8.GetBytes(stringToSend.ToCharArray)   
85NetStream.Write(arrayToSend, 0, arrayToSend.Length) '写入流,不用返回值   
86End Function 
87
88Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
89Dim t As New Threading.Thread(AddressOf checkMail)   
90t.Start() 
91
92End Sub 
93
94Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
95
96End Sub   
97End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus