VB.Net Tutorial/Socket Network/POP3 — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:55, 26 мая 2010
POP3 form
<source lang="vbnet">"Visual Basic.Net JingCai Programming 100 Examples "Author: Yong Zhang "Publisher: Water Publisher China "ISBN: 750841156 Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Text Imports System.Reflection.Emit Imports System.Windows.Forms public class PopClientForm
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() sHost = SetupForm.txtHost.Text sPort = SetupForm.txtPort.Text sUser = SetupForm.txtUser.Text sPass = SetupForm.txtPassword.Text showObject(False) End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar Friend WithEvents ImageList1 As System.Windows.Forms.ImageList Friend WithEvents mnuProfile As System.Windows.Forms.MenuItem Friend WithEvents mnuExit As System.Windows.Forms.MenuItem Friend WithEvents btnReceive As System.Windows.Forms.ToolBarButton Friend WithEvents btnPrevious As System.Windows.Forms.ToolBarButton Friend WithEvents btnNext As System.Windows.Forms.ToolBarButton Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents TabControl1 As System.Windows.Forms.TabControl Friend WithEvents TabPage1 As System.Windows.Forms.TabPage Friend WithEvents Panel2 As System.Windows.Forms.Panel Friend WithEvents txtMessage As System.Windows.Forms.TextBox Friend WithEvents Panel3 As System.Windows.Forms.Panel Friend WithEvents TabPage2 As System.Windows.Forms.TabPage Friend WithEvents lstLog As System.Windows.Forms.ListBox Friend WithEvents StatusBarPanel1 As System.Windows.Forms.StatusBarPanel Friend WithEvents Label1 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.mnuProfile = New System.Windows.Forms.MenuItem() Me.MenuItem3 = New System.Windows.Forms.MenuItem() Me.mnuExit = New System.Windows.Forms.MenuItem() Me.ToolBar1 = New System.Windows.Forms.ToolBar() Me.btnReceive = New System.Windows.Forms.ToolBarButton() Me.btnPrevious = New System.Windows.Forms.ToolBarButton() Me.btnNext = New System.Windows.Forms.ToolBarButton() Me.ImageList1 = New System.Windows.Forms.ImageList(Me.ruponents) Me.StatusBar1 = New System.Windows.Forms.StatusBar() Me.StatusBarPanel1 = New System.Windows.Forms.StatusBarPanel() Me.Panel1 = New System.Windows.Forms.Panel() Me.TabControl1 = New System.Windows.Forms.TabControl() Me.TabPage1 = New System.Windows.Forms.TabPage() Me.Panel2 = New System.Windows.Forms.Panel() Me.txtMessage = New System.Windows.Forms.TextBox() Me.Panel3 = New System.Windows.Forms.Panel() Me.Label1 = New System.Windows.Forms.Label() Me.TabPage2 = New System.Windows.Forms.TabPage() Me.lstLog = New System.Windows.Forms.ListBox() CType(Me.StatusBarPanel1, System.ruponentModel.ISupportInitialize).BeginInit() Me.Panel1.SuspendLayout() Me.TabControl1.SuspendLayout() Me.TabPage1.SuspendLayout() Me.Panel2.SuspendLayout() Me.Panel3.SuspendLayout() Me.TabPage2.SuspendLayout() Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuProfile, Me.MenuItem3, Me.mnuExit}) Me.MenuItem1.Text = "&File" " "mnuProfile " Me.mnuProfile.Index = 0 Me.mnuProfile.Text = "&Profile" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "-" " "mnuExit " Me.mnuExit.Index = 2 Me.mnuExit.Text = "E&xit" " "ToolBar1 " Me.ToolBar1.AllowDrop = True Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.btnReceive, Me.btnPrevious, Me.btnNext}) Me.ToolBar1.ButtonSize = New System.Drawing.Size(35, 35) Me.ToolBar1.DropDownArrows = True Me.ToolBar1.ImageList = Me.ImageList1 Me.ToolBar1.Name = "ToolBar1" Me.ToolBar1.ShowToolTips = True Me.ToolBar1.Size = New System.Drawing.Size(274, 38) Me.ToolBar1.TabIndex = 0 " "btnReceive " Me.btnReceive.Text = "Mail" " "btnPrevious " Me.btnPrevious.Enabled = False Me.btnPrevious.Text = "Prev." " "btnNext " Me.btnNext.Enabled = False Me.btnNext.Text = "Next" Me.StatusBar1.Location = New System.Drawing.Point(0, 295) Me.StatusBar1.Name = "StatusBar1" Me.StatusBar1.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.StatusBarPanel1}) Me.StatusBar1.Size = New System.Drawing.Size(274, 24) Me.StatusBar1.TabIndex = 2 Me.StatusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring " "Panel1 " Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabControl1}) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill Me.Panel1.Location = New System.Drawing.Point(0, 38) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(274, 257) Me.Panel1.TabIndex = 3 " "TabControl1 " Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2}) Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Fill Me.TabControl1.Name = "TabControl1" Me.TabControl1.SelectedIndex = 0 Me.TabControl1.Size = New System.Drawing.Size(274, 257) Me.TabControl1.TabIndex = 2 " "TabPage1 " Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel2, Me.Panel3}) Me.TabPage1.Location = New System.Drawing.Point(4, 21) Me.TabPage1.Name = "TabPage1" Me.TabPage1.Size = New System.Drawing.Size(266, 232) Me.TabPage1.TabIndex = 0 Me.TabPage1.Text = "POP3" " "Panel2 " Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtMessage}) Me.Panel2.Dock = System.Windows.Forms.DockStyle.Fill Me.Panel2.DockPadding.All = 2 Me.Panel2.Location = New System.Drawing.Point(0, 24) Me.Panel2.Name = "Panel2" Me.Panel2.Size = New System.Drawing.Size(266, 208) Me.Panel2.TabIndex = 1 " "txtMessage " Me.txtMessage.Dock = System.Windows.Forms.DockStyle.Fill Me.txtMessage.Location = New System.Drawing.Point(2, 2) Me.txtMessage.Multiline = True Me.txtMessage.Name = "txtMessage" Me.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.txtMessage.Size = New System.Drawing.Size(262, 204) Me.txtMessage.TabIndex = 2 Me.txtMessage.Text = "" " "Panel3 " Me.Panel3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1}) Me.Panel3.Dock = System.Windows.Forms.DockStyle.Top Me.Panel3.Name = "Panel3" Me.Panel3.Size = New System.Drawing.Size(266, 24) Me.Panel3.TabIndex = 0 " "Label1 " Me.Label1.Location = New System.Drawing.Point(8, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(84, 15) Me.Label1.TabIndex = 2 Me.Label1.Text = "POP3 Message:" " "TabPage2 " Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstLog}) Me.TabPage2.Location = New System.Drawing.Point(4, 21) Me.TabPage2.Name = "TabPage2" Me.TabPage2.Size = New System.Drawing.Size(302, 308) Me.TabPage2.TabIndex = 1 Me.TabPage2.Text = "Transaction" Me.TabPage2.Visible = False " "lstLog " Me.lstLog.Dock = System.Windows.Forms.DockStyle.Fill Me.lstLog.HorizontalScrollbar = True Me.lstLog.ItemHeight = 12 Me.lstLog.Name = "lstLog" Me.lstLog.Size = New System.Drawing.Size(302, 308) Me.lstLog.TabIndex = 0 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15) Me.ClientSize = New System.Drawing.Size(274, 319) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.StatusBar1, Me.ToolBar1}) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "POP3" CType(Me.StatusBarPanel1, System.ruponentModel.ISupportInitialize).EndInit() Me.Panel1.ResumeLayout(False) Me.TabControl1.ResumeLayout(False) Me.TabPage1.ResumeLayout(False) Me.Panel2.ResumeLayout(False) Me.Panel3.ResumeLayout(False) Me.TabPage2.ResumeLayout(False) Me.ResumeLayout(False) End Sub Private SetupForm As New Form2() Private tcpClient As New System.Net.Sockets.TcpClient() Private networkStream As Stream Private totalMail, currentMail, mailSize As Integer Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick Dim flag As Boolean If e.Button Is btnReceive Then Try If receiveMail() Then If totalMail > 0 Then currentMail = 1 flag = showMail(currentMail) showObject(True) Else MessageBox.Show("There is no mail.", "POP3", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) End If Else showObject(False) End If Catch ex As Exception lstLog.Items.Add("Socket: " & ex.ToString()) End Try End If If e.Button Is btnPrevious Then currentMail = currentMail - 1 If (currentMail <= 1) Then currentMail = 1 If (Not showMail(currentMail)) Then currentMail = currentMail + 1 showObject(True) End If If e.Button Is btnNext Then currentMail = currentMail + 1 If (currentMail >= totalMail) Then currentMail = totalMail If (Not showMail(currentMail)) Then currentMail = currentMail - 1 showObject(True) End If End Sub Private Sub mnuProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProfile.Click SetupForm.ShowDialog() End Sub Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click tcpClient.Close() End End Sub Private Function receiveMail() As Boolean Dim strHost, strPort, strUser, strPass As String Dim strTo, strSubject, strMsg As String Dim bytes(tcpClient.ReceiveBufferSize) As Byte Dim strResponse As String = Nothing strHost = SetupForm.txtHost.Text strPort = SetupForm.txtPort.Text strUser = SetupForm.txtUser.Text strPass = SetupForm.txtPassword.Text lstLog.Items.Clear() Try lstLog.Items.Add("C: Trying to connect to host " & strHost & ", port: " & strPort) tcpClient.Connect(strHost, Int32.Parse(strPort)) networkStream = tcpClient.GetStream() If Not POP3Response() Then Return False strMsg = "USER " & strUser If (Not POP3Request(strMsg)) Then Return False If (Not POP3Response()) Then Return False strMsg = "PASS " & strPass If (Not POP3Request(strMsg)) Then Return False If (Not POP3Response()) Then Return False strMsg = "STAT" If (Not POP3Request(strMsg)) Then Return False networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) strResponse = Encoding.ASCII.GetString(bytes) lstLog.Items.Add("S: " & strResponse.ToString()) If (Not strResponse.StartsWith("+OK")) Then MessageBox.Show(strResponse.ToString(), "POP3 Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) Return False End If Dim strTemp() As String strTemp = strResponse.Split(" ") totalMail = Int32.Parse(strTemp(1).Trim) mailSize = Int32.Parse(strTemp(2).Trim) Return True Catch ex As Exception tcpClient.Close() lstLog.Items.Add(ex.ToString()) Return False End Try End Function Private Sub showObject(ByVal flag As Boolean) If (flag) Then btnReceive.Enabled = False If ((totalMail > 1) And (currentMail = 1)) Then btnPrevious.Enabled = False btnNext.Enabled = True ElseIf ((currentMail < totalMail) And (currentMail > 1)) Then btnPrevious.Enabled = True btnNext.Enabled = True ElseIf ((currentMail = totalMail) And (currentMail > 1)) Then btnPrevious.Enabled = True btnNext.Enabled = False End If Else btnPrevious.Enabled = False btnNext.Enabled = False btnReceive.Enabled = True txtMessage.Clear() lstLog.Items.Clear() End If End Sub Private Function showMail(ByVal mailNo As Integer) As Boolean Dim strMsg, strTemp As String Dim strContent As String = Nothing Dim blnFlag As Boolean = True Dim bytes(tcpClient.ReceiveBufferSize) As Byte Dim strResponse As String = Nothing Dim byteMsg() As Byte Try strMsg = "RETR " & mailNo If (Not POP3Request(strMsg)) Then Return False Do networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) strResponse = Encoding.ASCII.GetString(bytes) If blnFlag Then If (Not strResponse.StartsWith("+OK")) Then MessageBox.Show(strResponse.ToString(), "POP3 Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) Return False End If blnFlag = False End If strContent = strContent & strResponse.Trim.ToString Dim cNull As Char = ControlChars.NullChar byteMsg = Encoding.ASCII.GetBytes(strContent.Trim(cNull).ToCharArray()) strTemp = Encoding.ASCII.GetString(byteMsg, 0, byteMsg.Length - 2) If (strTemp.Trim().EndsWith(".")) Then Exit Do End If Loop txtMessage.Text = strContent StatusBar1.Text = "Total: " & totalMail & " (Size: " & mailSize & ") Current: " & mailNo Return True Catch ex As Exception lstLog.Items.Add(ex.ToString()) Return False End Try End Function Private Function POP3Response() As Boolean Dim bytes(tcpClient.ReceiveBufferSize) As Byte Dim strResponse As String = Nothing Try networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) strResponse = Encoding.ASCII.GetString(bytes) lstLog.Items.Add("S: " & strResponse.ToString()) If Not strResponse.StartsWith("+OK") Then lstLog.Items.Add("POP3Response Error.") tcpClient.Close() Return False Else Return True End If Catch ex As Exception lstLog.Items.Add("POP3Response Error: " & ex.ToString()) tcpClient.Close() Return False End Try End Function Private Function POP3Request(ByVal strMsg As String) As Boolean Dim byteMsg() As Byte strMsg = strMsg & ControlChars.CrLf Try byteMsg = Encoding.ASCII.GetBytes(strMsg.ToCharArray()) networkStream.Write(byteMsg, 0, byteMsg.Length) lstLog.Items.Add("C: " & strMsg.ToString()) Return True Catch ex As Exception lstLog.Items.Add("POP3Request Error: " & ex.ToString()) tcpClient.Close() Return False End Try End Function
End Class Module Module1
Public sHost As String Public sPort As String Public sUser As String Public sPass As String
End Module Public Class Form2
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents txtHost As System.Windows.Forms.TextBox Friend WithEvents txtPort As System.Windows.Forms.TextBox Friend WithEvents txtUser As System.Windows.Forms.TextBox Friend WithEvents btnOK As System.Windows.Forms.Button Friend WithEvents btnCancel As System.Windows.Forms.Button Friend WithEvents txtPassword As System.Windows.Forms.TextBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents Label8 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.GroupBox1 = New System.Windows.Forms.GroupBox() Me.txtPassword = New System.Windows.Forms.TextBox() Me.txtUser = New System.Windows.Forms.TextBox() Me.txtPort = New System.Windows.Forms.TextBox() Me.txtHost = New System.Windows.Forms.TextBox() Me.Label5 = New System.Windows.Forms.Label() Me.Label6 = New System.Windows.Forms.Label() Me.Label7 = New System.Windows.Forms.Label() Me.Label8 = New System.Windows.Forms.Label() Me.btnOK = New System.Windows.Forms.Button() Me.btnCancel = New System.Windows.Forms.Button() Me.GroupBox1.SuspendLayout() Me.SuspendLayout() " "GroupBox1 " Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtPassword, Me.txtUser, Me.txtPort, Me.txtHost, Me.Label5, Me.Label6, Me.Label7, Me.Label8}) Me.GroupBox1.Location = New System.Drawing.Point(3, 5) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(240, 135) Me.GroupBox1.TabIndex = 0 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "POP3 Server" " "txtPassword " Me.txtPassword.Location = New System.Drawing.Point(70, 98) Me.txtPassword.Name = "txtPassword" Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42) Me.txtPassword.Size = New System.Drawing.Size(160, 22) Me.txtPassword.TabIndex = 3 Me.txtPassword.Text = "" " "txtUser " Me.txtUser.Location = New System.Drawing.Point(70, 72) Me.txtUser.Name = "txtUser" Me.txtUser.Size = New System.Drawing.Size(160, 22) Me.txtUser.TabIndex = 2 Me.txtUser.Text = "leo_huang" " "txtPort " Me.txtPort.Location = New System.Drawing.Point(70, 46) Me.txtPort.Name = "txtPort" Me.txtPort.Size = New System.Drawing.Size(160, 22) Me.txtPort.TabIndex = 1 Me.txtPort.Text = "110" " "txtHost " Me.txtHost.Location = New System.Drawing.Point(70, 20) Me.txtHost.Name = "txtHost" Me.txtHost.Size = New System.Drawing.Size(160, 22) Me.txtHost.TabIndex = 0 Me.txtHost.Text = "192.168.1.59" " "Label5 " Me.Label5.Location = New System.Drawing.Point(8, 24) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(70, 15) Me.Label5.TabIndex = 0 Me.Label5.Text = "Host:" " "Label6 " Me.Label6.Location = New System.Drawing.Point(8, 102) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(70, 15) Me.Label6.TabIndex = 3 Me.Label6.Text = "Password:" " "Label7 " Me.Label7.Location = New System.Drawing.Point(8, 76) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(70, 15) Me.Label7.TabIndex = 2 Me.Label7.Text = "Login:" " "Label8 " Me.Label8.Location = New System.Drawing.Point(8, 50) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(70, 15) Me.Label8.TabIndex = 1 Me.Label8.Text = "Port:" " "btnOK " Me.btnOK.Location = New System.Drawing.Point(28, 152) Me.btnOK.Name = "btnOK" Me.btnOK.Size = New System.Drawing.Size(85, 25) Me.btnOK.TabIndex = 4 Me.btnOK.Text = "OK" " "btnCancel " Me.btnCancel.Location = New System.Drawing.Point(133, 152) Me.btnCancel.Name = "btnCancel" Me.btnCancel.Size = New System.Drawing.Size(85, 25) Me.btnCancel.TabIndex = 5 Me.btnCancel.Text = "Cancel" " "Form2 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15) Me.ClientSize = New System.Drawing.Size(247, 191) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK, Me.GroupBox1}) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form2" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent Me.Text = "POP3 Profile" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtHost.Text = sHost txtPort.Text = sPort txtUser.Text = sUser txtPassword.Text = sPass End Sub Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click If txtHost.Text = "" Or txtPort.Text = "" Or txtUser.Text = "" Then MessageBox.Show("Please enter the information.", "POP3", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) Else sHost = txtHost.Text sPort = txtPort.Text sUser = txtUser.Text sPass = txtPassword.Text Me.Close() End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Me.Close() End Sub
End Class</source>