Hi,
I have posted about receiving email before and this is a follow up of my progress. I have only one form and it consists of 2 buttons, one for downloading messages from the server, the other, to disconnect the connection. I also have one listview and one textbox to display the message.
I took the codes from a few open source programming website and compiled them. My program works as such that upon clicking the 'Download Messages' button, a message box will pop up and the user/admin will be asked to enter the POP3 account name. My doubts are I am not sure what POP3 account name to put in.
I use IIS/SMTP to send email and intend to use IIS as a server to receive email. How and where can i find out what is my POP3 account name?
Below are my codes:-
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
Inherits 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() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private 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.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents CHFrom As System.Windows.Forms.ColumnHeader
Friend WithEvents CHSubject As System.Windows.Forms.ColumnHeader
Friend WithEvents CHDate As System.Windows.Forms.ColumnHeader
Friend WithEvents CHSize As System.Windows.Forms.ColumnHeader
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.ListView1 = New System.Windows.Forms.ListView
Me.CHFrom = New System.Windows.Forms.ColumnHeader
Me.CHSubject = New System.Windows.Forms.ColumnHeader
Me.CHDate = New System.Windows.Forms.ColumnHeader
Me.CHSize = New System.Windows.Forms.ColumnHeader
Me.Button2 = New System.Windows.Forms.Button
Me.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 = 0
Me.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 = 0
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.TextBox1.Size = New System.Drawing.Size(640, 200)
Me.TextBox1.TabIndex = 1
'
'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 = True
Me.ListView1.HideSelection = False
Me.ListView1.Location = New System.Drawing.Point(4, 44)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(640, 192)
Me.ListView1.TabIndex = 3
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.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 = 4
Me.Button2.Text = "Close Connection"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(648, 447)
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"
Me.ResumeLayout(False)
Me.PerformLayout()
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.Click
Dim i As Integer
Dim msg As POP3Message.Message
Dim msgString As String
Dim messages As Integer
Me.Cursor = Cursors.WaitCursor
messages = objPOP3.Connect()
If messages = -1 Then
Me.Cursor = Cursors.Default
Exit Sub
End If
Dim originalCaption As String = Me.Text
For i = 1 To messages
Me.Text = "Downloading message " & i.ToString & "/" & messages.ToString
Dim msgItem As New ListViewItem()
msgString = objPOP3.GetMessage(i)
msg = objPOP3.CreateFromText(msgString)
msgItem.Text = msg._From
msgItem.SubItems.Add(msg._Subject)
msgItem.SubItems.Add(msg._Date)
ListView1.Items.Add(msgItem)
TextBox1.AppendText(msg._Body & vbCrLf)
Next
Me.Text = originalCaption
Me.Cursor = Cursors.Default
End Sub
Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
'Dim messages As Integer
'TextBox1.Text = objPOP3.GetMessage(ListView1.SelectedIndices(0) + 1)
End Sub
Private Sub ListView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyUp
If e.KeyCode = Keys.Delete Then
If objPOP3.DeleteMessage(ListView1.SelectedIndices(0) + 1) >= 0 Then
ListView1.Items(ListView1.SelectedIndices(0)).Text = "DELETED"
ListView1.Items(ListView1.SelectedIndices(0)).SubItems.Clear()
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
objPOP3.Quit()
ListView1.Items.Clear()
End Sub
End Class
Class POP3Message
Dim Server As TcpClient
Dim NetStrm As NetworkStream
Dim RdStrm As StreamReader
Public Function Connect() As Integer
Dim POP3Account As String
POP3Account = InputBox("Enter your POP3 account name (e.g., mail.server.com)")
If POP3Account.Trim = "" Then Exit Function
Try
Server = New TcpClient(POP3Account.Trim, 110)
NetStrm = Server.GetStream()
RdStrm = New StreamReader(Server.GetStream())
Catch exc As Exception
MsgBox(exc.Message)
Exit Function
End Try
Dim user As String
user = InputBox("Enter your user name")
Dim data As String = "USER " + user.Trim + vbCrLf
Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
Dim POPResponse As String
POPResponse = RdStrm.ReadLine()
If POPResponse.Substring(0, 4) = "-ERR" Then
MsgBox("Invalid user name")
Return -1
End If
Dim password As String
password = InputBox("Enter your password")
data = "PASS " & password & vbCrLf
szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
POPResponse = RdStrm.ReadLine()
If POPResponse.Substring(0, 4) = "-ERR" Then
MsgBox("Invalid password")
Return -1
End If
data = "STAT" + vbCrLf
szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
POPResponse = RdStrm.ReadLine()
If POPResponse.Substring(0, 4) = "-ERR" Then
MsgBox("Could not log you in")
Return -1
End If
Dim parts() As String
parts = POPResponse.Split(" ")
Dim messages, totSize As Integer
messages = CInt(parts(1))
Return messages
End Function
Public Function DeleteMessage(ByVal msgIndex As Integer)
Dim data As String = "DELE " & msgIndex.ToString & vbCrLf
Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
Dim tmpString As String = RdStrm.ReadLine()
If tmpString.Substring(0, 4) = "-ERR" Then
MsgBox("Could not delete message")
Return -1
Else
Return 1
End If
End Function
Public Function Quit()
Dim data As String = "QUIT " & vbCrLf
Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
Dim tmpString As String = RdStrm.ReadLine()
End Function
Public Structure Message
Dim _From As String
Dim _To As String
Dim _Date As String
Dim _Subject As String
Dim _Sender As String
Dim _CC As String
Dim _BCC As String
Dim _Received As String
Dim _Body As String
End Structure
Public Function CreateFromText(ByVal strMessage As String) As Message
Dim Mssg As New Message()
Dim brkPos As Integer
Dim Header As String
Dim Headers() As String
Dim Body As String
Dim vField As Object
Dim strHeader As String
Dim HeaderName As String
Dim HeaderValue As String
brkPos = InStr(1, strMessage, vbCrLf & vbCrLf)
If brkPos Then
Header = strMessage.Substring(0, brkPos - 1)
Body = strMessage.Substring(brkPos + 1, strMessage.Length - Header.Length - 3)
Mssg._Body = Body
Else
Throw New Exception("Invalid message format")
Exit Function
End If
Headers = Split(Header, vbCrLf)
Dim _header As String
For Each _header In Headers
brkPos = _header.IndexOf(":")
If brkPos >= 0 Then
HeaderName = _header.Substring(0, brkPos)
Else
HeaderName = ""
End If
HeaderValue = _header.Substring(brkPos + 1)
Select Case HeaderName.ToLower
Case "received"
Mssg._Received = HeaderValue
Case "from"
Mssg._From = HeaderValue
Case "sender"
Mssg._Sender = HeaderValue
Case "to"
Mssg._To = HeaderValue
Case "cc"
Mssg._CC = HeaderValue
Case "bcc"
Mssg._BCC = HeaderValue
Case "subject"
Mssg._Subject = HeaderValue
Case "date"
Mssg._Date = HeaderValue
End Select
Next
Return Mssg
End Function
Function GetMessage(ByVal msgindex As Integer) As String
Dim tmpString As String
Dim Data As String
Dim szData() As Byte
Dim msg As String
Try
Data = "RETR " & msgindex.ToString & vbCrLf
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())
NetStrm.Write(szData, 0, szData.Length)
tmpString = RdStrm.ReadLine()
If tmpString.Substring(0, 4) <> "-ERR" Then
While (tmpString <> ".")
msg = msg & tmpString & vbCrLf
tmpString = RdStrm.ReadLine()
End While
End If
Catch exc As InvalidOperationException
MsgBox("Message retrieval failed: " & vbCrLf & Err.ToString())
End Try
Return msg
End Function
End Class