个人工具
您位于: 首页 程序相关 DotNet类 POP3(vb.net)
文档操作

POP3(vb.net)

作者:YibLee上次修改时间: 2006-04-25 04:05
Imports System.Net.SocketsImports System.IOPublic Class Form1Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code "Public Sub New()MyBase.New()'This call is required by the Windows Form Designer.InitializeComponent()'Add any initialization after the InitializeComponent() callEnd Sub'Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Thencomponents.Dispose()End IfEnd IfMyBase.Dispose(disposing)End Sub'Required by the Windows Form DesignerPrivate components As System.ComponentModel.IContainer'NOTE: The following procedure is required by the Windows Form Designer'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor.Friend WithEvents Button1 As System.Windows.Forms.ButtonFriend WithEvents TextBox1 As System.Windows.Forms.TextBoxFriend WithEvents ListView1 As System.Windows.Forms.ListViewFriend WithEvents CHFrom As System.Windows.Forms.ColumnHeaderFriend WithEvents CHSubject As System.Windows.Forms.ColumnHeaderFriend WithEvents CHDate As System.Windows.Forms.ColumnHeaderFriend WithEvents CHSize As System.Windows.Forms.ColumnHeaderFriend WithEvents Button2 As System.Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Me.Button1 = New System.Windows.Forms.ButtonMe.TextBox1 = New System.Windows.Forms.TextBoxMe.ListView1 = New System.Windows.Forms.ListViewMe.CHFrom = New System.Windows.Forms.ColumnHeaderMe.CHSubject = New System.Windows.Forms.ColumnHeaderMe.CHDate = New System.Windows.Forms.ColumnHeaderMe.CHSize = New System.Windows.Forms.ColumnHeaderMe.Button2 = New System.Windows.Forms.ButtonMe.SuspendLayout()''Button1'Me.Button1.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))Me.Button1.Location = New System.Drawing.Point(8, 5)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(182, 32)Me.Button1.TabIndex = 0Me.Button1.Text = "Download Messages"''TextBox1'Me.TextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _Or System.Windows.Forms.AnchorStyles.Left) _Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)Me.TextBox1.Location = New System.Drawing.Point(4, 244)Me.TextBox1.MaxLength = 0Me.TextBox1.Multiline = TrueMe.TextBox1.Name = "TextBox1"Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.BothMe.TextBox1.Size = New System.Drawing.Size(966, 358)Me.TextBox1.TabIndex = 1Me.TextBox1.Text = ""''ListView1'Me.ListView1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.CHFrom, Me.CHSubject, Me.CHDate, Me.CHSize})Me.ListView1.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))Me.ListView1.FullRowSelect = TrueMe.ListView1.HideSelection = FalseMe.ListView1.Location = New System.Drawing.Point(4, 44)Me.ListView1.Name = "ListView1"Me.ListView1.Size = New System.Drawing.Size(966, 192)Me.ListView1.TabIndex = 3Me.ListView1.View = System.Windows.Forms.View.Details''CHFrom'Me.CHFrom.Text = "From"Me.CHFrom.Width = 140''CHSubject'Me.CHSubject.Text = "Subject"Me.CHSubject.Width = 300''CHDate'Me.CHDate.Text = "Date"Me.CHDate.Width = 100''CHSize'Me.CHSize.Text = "Size"Me.CHSize.Width = 80''Button2'Me.Button2.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))Me.Button2.Location = New System.Drawing.Point(462, 5)Me.Button2.Name = "Button2"Me.Button2.Size = New System.Drawing.Size(182, 32)Me.Button2.TabIndex = 4Me.Button2.Text = "Close Connection"''Form1'Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)Me.ClientSize = New System.Drawing.Size(974, 605)Me.Controls.Add(Me.Button2)Me.Controls.Add(Me.ListView1)Me.Controls.Add(Me.TextBox1)Me.Controls.Add(Me.Button1)Me.Name = "Form1"Me.Text = "POP3 Demo -- Vinesh Patel (919898498656)"Me.ResumeLayout(False)End Sub#End Region'------------------------------------------------------------------Dim objPOP3 As New POP3Message()Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim i As IntegerDim msg As POP3Message.MessageDim msgString As StringDim messages As IntegerMe.Cursor = Cursors.WaitCursormessages = objPOP3.Connect()If messages = -1 ThenMe.Cursor = Cursors.DefaultExit SubEnd IfDim originalCaption As String = Me.TextFor i = 1 To messagesMe.Text = "Downloading message " & i.ToString & "/" & messages.ToStringDim msgItem As New ListViewItem()msgString = objPOP3.GetMessage(i)msg = objPOP3.CreateFromText(msgString)msgItem.Text = msg._FrommsgItem.SubItems.Add(msg._Subject)msgItem.SubItems.Add(msg._Date)ListView1.Items.Add(msgItem)TextBox1.AppendText(msg._Body & vbCrLf)Dim stW As StreamWriter, FS As FileStreamFS = New FileStream("d:\" & i & ".eml", FileMode.OpenOrCreate)stW = New StreamWriter(FS)stW.WriteLine(msgString)stW.Close()NextMe.Text = originalCaptionMe.Cursor = Cursors.DefaultEnd SubPrivate Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.ClickDim messages As IntegerTextBox1.Text = objPOP3.GetMessage(ListView1.SelectedIndices(0) + 1)End SubPrivate Sub ListView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyUpIf e.KeyCode = Keys.Delete ThenIf objPOP3.DeleteMessage(ListView1.SelectedIndices(0) + 1) >= 0 ThenListView1.Items(ListView1.SelectedIndices(0)).Text = "DELETED"ListView1.Items(ListView1.SelectedIndices(0)).SubItems.Clear()End IfEnd IfEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickobjPOP3.Quit()ListView1.Items.Clear()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadEnd SubEnd ClassClass POP3MessageDim Server As TcpClientDim NetStrm As NetworkStreamDim RdStrm As StreamReaderPublic Function Connect() As IntegerDim POP3Account As StringPOP3Account = InputBox("pop3邮件服务器")If POP3Account.Trim = "" Then Exit FunctionTryServer = New TcpClient(POP3Account.Trim, 110)NetStrm = Server.GetStream()RdStrm = New StreamReader(Server.GetStream())Catch exc As ExceptionMsgBox(exc.Message)Exit FunctionEnd TryDim user As Stringuser = InputBox("用户名")Dim data As String = "USER " + user.Trim + vbCrLfDim szData() As Byte = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)Dim POPResponse As StringPOPResponse = RdStrm.ReadLine()If POPResponse.Substring(0, 4) = "-ERR" ThenMsgBox("Invalid user name")Return -1End IfDim password As Stringpassword = InputBox("密码")data = "PASS " & password & vbCrLfszData = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)POPResponse = RdStrm.ReadLine()If POPResponse.Substring(0, 4) = "-ERR" ThenMsgBox("Invalid password")Return -1End Ifdata = "STAT" + vbCrLfszData = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)POPResponse = RdStrm.ReadLine()If POPResponse.Substring(0, 4) = "-ERR" ThenMsgBox("Could not log you in")Return -1End Ifdata = "STAT" + vbCrLfszData = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)POPResponse = RdStrm.ReadLine()If POPResponse.Substring(0, 4) = "-ERR" ThenMsgBox("Could not log you in")Return -1End IfDim parts() As Stringparts = POPResponse.Split(" ")Dim messages, totSize As Integermessages = CInt(parts(1))Return messagesEnd Function'-----------------------------------------------------------------------------Public Function DeleteMessage(ByVal msgIndex As Integer)Dim data As String = "DELE " & msgIndex.ToString & vbCrLfDim szData() As Byte = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)Dim tmpString As String = RdStrm.ReadLine()If tmpString.Substring(0, 4) = "-ERR" ThenMsgBox("Could not delete message")Return -1ElseReturn 1End IfEnd FunctionPublic Function Quit()Dim data As String = "QUIT " & vbCrLfDim szData() As Byte = System.Text.Encoding.Default.GetBytes(data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)Dim tmpString As String = RdStrm.ReadLine()End FunctionPublic Structure MessageDim _From As StringDim _To As StringDim _Date As StringDim _Subject As StringDim _Sender As StringDim _CC As StringDim _BCC As StringDim _Received As StringDim _Body As StringEnd StructurePublic Function CreateFromText(ByVal strMessage As String) As MessageDim Mssg As New MessageDim brkPos As IntegerDim Header As StringDim Headers() As StringDim Body As StringDim vField As ObjectDim strHeader As StringDim HeaderName As StringDim HeaderValue As String'MsgBox(strMessage)brkPos = InStr(1, strMessage, vbCrLf & vbCrLf)If brkPos ThenHeader = strMessage.Substring(0, brkPos - 1)Body = strMessage.Substring(brkPos + 1, strMessage.Length - Header.Length - 3)Mssg._Body = BodyElseThrow New Exception("Invalid message format")Exit FunctionEnd IfHeaders = Split(Header, vbCrLf)Dim _header As StringFor Each _header In HeadersbrkPos = _header.IndexOf(":")If brkPos >= 0 ThenHeaderName = _header.Substring(0, brkPos)ElseHeaderName = ""End IfHeaderValue = _header.Substring(brkPos + 1)Select Case HeaderName.ToLowerCase "received"Mssg._Received = HeaderValueCase "from"Mssg._From = HeaderValueCase "sender"Mssg._Sender = HeaderValueCase "to"Mssg._To = HeaderValueCase "cc"Mssg._CC = HeaderValueCase "bcc"Mssg._BCC = HeaderValueCase "subject"Mssg._Subject = HeaderValueCase "date"Mssg._Date = HeaderValueEnd SelectNextReturn MssgEnd FunctionFunction GetMessage(ByVal msgindex As Integer) As StringDim tmpString As StringDim Data As StringDim szData() As ByteDim msg As StringTryData = "RETR " & msgindex.ToString & vbCrLfszData = System.Text.Encoding.Default.GetBytes(Data.ToCharArray())NetStrm.Write(szData, 0, szData.Length)tmpString = RdStrm.ReadLine()If tmpString.Substring(0, 4) <> "-ERR" ThenWhile (tmpString <> ".")msg = msg & tmpString & vbCrLftmpString = RdStrm.ReadLine()End WhileEnd IfCatch exc As InvalidOperationExceptionMsgBox("Message retrieval failed: " & vbCrLf & Err.ToString())End TryReturn msgMsgBox(msg)End FunctionEnd Class
« 2008年 十二月 »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
 

使用Plone开源内容管理系统(CMS)构建

本网站符合如下标准: