' Quote from Book
' Visual Basic.net Programming
' Publisher Water Publisher
' China, Beijing
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
public class Pop3Receive
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()
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.ComponentModel.IContainer
Friend WithEvents txtMailContent As System.Windows.Forms.TextBox
Friend WithEvents btnMailReceive As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtID As System.Windows.Forms.TextBox
Friend WithEvents txtPassWord As System.Windows.Forms.TextBox
Friend WithEvents txtServer As System.Windows.Forms.TextBox
Friend WithEvents txtPort As System.Windows.Forms.TextBox
Friend WithEvents lstMailList As System.Windows.Forms.ListBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents txtNumber As System.Windows.Forms.TextBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents txtSize As System.Windows.Forms.TextBox
Friend WithEvents btnDelete As System.Windows.Forms.Button
Friend WithEvents btnQUIT As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lstMailList = New System.Windows.Forms.ListBox()
Me.txtMailContent = New System.Windows.Forms.TextBox()
Me.txtID = New System.Windows.Forms.TextBox()
Me.txtServer = New System.Windows.Forms.TextBox()
Me.txtPort = New System.Windows.Forms.TextBox()
Me.txtPassWord = New System.Windows.Forms.TextBox()
Me.btnMailReceive = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.txtNumber = New System.Windows.Forms.TextBox()
Me.txtSize = New System.Windows.Forms.TextBox()
Me.Label6 = New System.Windows.Forms.Label()
Me.btnDelete = New System.Windows.Forms.Button()
Me.btnQUIT = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'lstMailList
'
Me.lstMailList.ItemHeight = 12
Me.lstMailList.Location = New System.Drawing.Point(20, 48)
Me.lstMailList.Name = "lstMailList"
Me.lstMailList.Size = New System.Drawing.Size(666, 112)
Me.lstMailList.TabIndex = 8
'
'txtMailContent
'
Me.txtMailContent.Location = New System.Drawing.Point(20, 176)
Me.txtMailContent.Multiline = True
Me.txtMailContent.Name = "txtMailContent"
Me.txtMailContent.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtMailContent.Size = New System.Drawing.Size(666, 344)
Me.txtMailContent.TabIndex = 9
Me.txtMailContent.Text = ""
'
'txtID
'
Me.txtID.Location = New System.Drawing.Point(727, 40)
Me.txtID.Name = "txtID"
Me.txtID.Size = New System.Drawing.Size(164, 21)
Me.txtID.TabIndex = 0
Me.txtID.Text = ""
'
'txtServer
'
Me.txtServer.Location = New System.Drawing.Point(727, 200)
Me.txtServer.Name = "txtServer"
Me.txtServer.Size = New System.Drawing.Size(164, 21)
Me.txtServer.TabIndex = 3
Me.txtServer.Text = ""
'
'txtPort
'
Me.txtPort.Location = New System.Drawing.Point(727, 144)
Me.txtPort.Name = "txtPort"
Me.txtPort.Size = New System.Drawing.Size(164, 21)
Me.txtPort.TabIndex = 2
Me.txtPort.Text = "110"
'
'txtPassWord
'
Me.txtPassWord.Location = New System.Drawing.Point(727, 93)
Me.txtPassWord.Name = "txtPassWord"
Me.txtPassWord.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txtPassWord.Size = New System.Drawing.Size(164, 21)
Me.txtPassWord.TabIndex = 1
Me.txtPassWord.Text = ""
'
'btnMailReceive
'
Me.btnMailReceive.Location = New System.Drawing.Point(727, 240)
Me.btnMailReceive.Name = "btnMailReceive"
Me.btnMailReceive.Size = New System.Drawing.Size(164, 24)
Me.btnMailReceive.TabIndex = 6
Me.btnMailReceive.Text = "Check Mail"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(727, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(143, 16)
Me.Label1.TabIndex = 7
Me.Label1.Text = "Account"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(727, 176)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(143, 16)
Me.Label2.TabIndex = 8
Me.Label2.Text = "Server"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(727, 128)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(143, 16)
Me.Label3.TabIndex = 9
Me.Label3.Text = "port"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(727, 72)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(143, 16)
Me.Label4.TabIndex = 10
Me.Label4.Text = "Password"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(730, 275)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(153, 13)
Me.Label5.TabIndex = 11
Me.Label5.Text = "Total"
'
'txtNumber
'
Me.txtNumber.Location = New System.Drawing.Point(730, 299)
Me.txtNumber.Name = "txtNumber"
Me.txtNumber.Size = New System.Drawing.Size(153, 21)
Me.txtNumber.TabIndex = 4
Me.txtNumber.Text = ""
'
'txtSize
'
Me.txtSize.Location = New System.Drawing.Point(731, 355)
Me.txtSize.Name = "txtSize"
Me.txtSize.Size = New System.Drawing.Size(153, 21)
Me.txtSize.TabIndex = 5
Me.txtSize.Text = ""
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(731, 331)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(153, 13)
Me.Label6.TabIndex = 13
Me.Label6.Text = "Size(k)"
'
'btnDelete
'
Me.btnDelete.Location = New System.Drawing.Point(492, 8)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.Size = New System.Drawing.Size(194, 24)
Me.btnDelete.TabIndex = 7
Me.btnDelete.Text = "Delete"
'
'btnQUIT
'
Me.btnQUIT.Location = New System.Drawing.Point(732, 395)
Me.btnQUIT.Name = "btnQUIT"
Me.btnQUIT.Size = New System.Drawing.Size(164, 24)
Me.btnQUIT.TabIndex = 6
Me.btnQUIT.Text = "Quit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(911, 533)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnQUIT, Me.btnDelete, Me.txtSize, Me.Label6, Me.txtNumber, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.btnMailReceive, Me.txtPassWord, Me.txtPort, Me.txtServer, Me.txtID, Me.txtMailContent, Me.lstMailList})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Dim myTcpClient As New TcpClient()
Dim myNetworkStream As NetworkStream
Dim blnDelete As Boolean = False
Private Sub btnMailReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMailReceive.Click
Dim strMessage
ConnectToMailServer()
Dim t As New Thread(AddressOf GetMailNumber)
t.Start()
End Sub
Public Sub ConnectToMailServer()
Try
Dim strReturnMessage As String
myTcpClient.Connect(txtServer.Text, Integer.Parse(txtPort.Text))
myNetworkStream = myTcpClient.GetStream()
Dim myStreamReader As New StreamReader(myNetworkStream)
strReturnMessage = myStreamReader.ReadLine() + vbCrLf
strReturnMessage += SendCommand(myNetworkStream, "USER " + txtID.Text) + vbCrLf
strReturnMessage += SendCommand(myNetworkStream, "PASS " + txtPassWord.Text) + vbCrLf
MessageBox.Show(strReturnMessage)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub GetMailNumber()
Dim strMessage(2) As String
Dim strMessageLine As String
strMessageLine = SendCommand(myNetworkStream, "STAT")
strMessage = strMessageLine.Split(" ")
txtNumber.Text = strMessage(1)
txtSize.Text = (Long.Parse(strMessage(2)) / 1000).ToString
GetMailList(Long.Parse(strMessage(1)))
End Sub
Private Sub GetMailList(ByVal lngNumber As Long)
Dim strReturnMessage As String
Dim lngLoop As Long
Dim lngIndex As Long
Try
For lngLoop = 0 To lngNumber - 1
lngIndex = lngLoop + 1
strReturnMessage = "[" + lngIndex.ToString + "]" + SendCommand(myNetworkStream, "LIST " + lngIndex.ToString)
lstMailList.Items.Add(strReturnMessage)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function SendCommand(ByRef pNetStream As NetworkStream, ByVal pstrCommand As String) As String
Try
Dim strCommand = pstrCommand + vbCrLf
Dim bteCommand() As Byte = Encoding.ASCII.GetBytes(strCommand)
Dim myStreamReader As StreamReader
Dim sstrLine As String
Dim strLine As String
pNetStream.Write(bteCommand, 0, bteCommand.Length)
myStreamReader = New StreamReader(pNetStream)
strLine = myStreamReader.ReadLine()
Return strLine
Catch ex As Exception
Return ex.Message
End Try
End Function
Private Sub lstMailList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMailList.SelectedIndexChanged
If blnDelete = False Then
Dim t As New Thread(AddressOf GetMessage)
t.Start()
End If
End Sub
Private Sub GetMessage()
Dim strMessageContent As String = ""
Dim intRead As Integer
Dim strCommand = "RETR " + (lstMailList.SelectedIndex + 1).ToString + vbCrLf
Dim bteCommand() As Byte = Encoding.ASCII.GetBytes(strCommand)
Dim myStreamReader As StreamReader
txtMailContent.Text = ""
Try
myNetworkStream.Write(bteCommand, 0, bteCommand.Length)
myStreamReader = New StreamReader(myNetworkStream)
AnalyMessage(myStreamReader)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub AnalyMessage(ByVal pStreamReader As StreamReader)
Dim strReadLine As String = ""
Dim sstrReadLine As String = ""
Do While (sstrReadLine = Nothing Or sstrReadLine.Length = 0)
sstrReadLine = pStreamReader.ReadLine
Loop
txtMailContent.Text += vbCrLf + "--- Header --" + vbCrLf
Do While sstrReadLine.Trim.Length <> 0
sstrReadLine = pStreamReader.ReadLine
txtMailContent.Text += vbCrLf + sstrReadLine
Loop
txtMailContent.Text += vbCrLf + "--- Body --" + vbCrLf
Do
sstrReadLine = pStreamReader.ReadLine
If sstrReadLine.Trim() = "." Then Exit Do
If sstrReadLine <> Nothing Then txtMailContent.Text += vbCrLf + sstrReadLine
Loop
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim strReturnMessage As String
Dim intDelete As Integer
intDelete = lstMailList.SelectedIndex + 1
Try
strReturnMessage = SendCommand(myNetworkStream, "DELE " + intDelete.ToString)
txtMailContent.Text = strReturnMessage
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
blnDelete = True
lstMailList.Items.RemoveAt(intDelete - 1)
blnDelete = False
End Sub
Private Sub btnQUIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQUIT.Click
Dim strReturnMessage As String
Try
strReturnMessage = SendCommand(myNetworkStream, "QUIT")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
|