Click here to Skip to main content
15,895,538 members
Articles / Programming Languages / Visual Basic

Simple POP3 Email Class

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
13 Mar 2007CPOL2 min read 245.3K   14.2K   84  
A simple POP3 class to download emails from a mail server.
Public Class frmMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'variable declaration
        Dim strMailServer, strUsername, strPassword, strFrom, strSubject, strToo, strBody, strMailContent As String
        Dim popConn As SamplePop3Class.POP3
        Dim mailMess As SamplePop3Class.EmailMessage
        Dim intMessCnt, i As Integer

        'set the variables to the input boxes
        strMailServer = TextBox1.Text
        strUsername = TextBox2.Text
        strPassword = TextBox3.Text

        'make sure some detials have been filled in
        If strMailServer = "" Then
            MsgBox("No Mail server specified!")
            Exit Sub
        End If
        If strUsername = "" Then
            MsgBox("No Username specified!")
            Exit Sub
        End If
        If strPassword = "" Then
            MsgBox("No Password Specified!")
            Exit Sub
        End If

        'Disable the button when proccessing
        Button1.Enabled = False

        'create the objects
        popConn = New SamplePop3Class.POP3
        mailMess = New SamplePop3Class.EmailMessage

        'if we have got to this point, try and connect to the server
        popConn.POPConnect(strMailServer, strUsername, strPassword)

        'now we have a connection, see if there are any mails on the server
        intMessCnt = popConn.GetMailStat()

        'now, see if we have returned any messages
        If intMessCnt > 0 Then

            'clear contents of the list and add the heading
            ListBox1.Items.Clear()

            'if we returned some messages, loop through each one and get the details
            For i = 1 To intMessCnt

                'load the entire content of the mail into a string
                strMailContent = popConn.GetMailMessage(i)

                'call the functions to get the various parts out of the email 
                strFrom = mailMess.ParseEmail(strMailContent, "From:")
                strSubject = mailMess.ParseEmail(strMailContent, "Subject:")
                strToo = mailMess.ParseEmail(strMailContent, "To:")
                strBody = mailMess.ParseBody()

                'add email details to the list box
                ListBox1.Items.Add(strFrom & ", " & strSubject)

                'un-comment to display full details of the email in a message box
                'MsgBox("From: " & strFrom & vbNewLine & "Too: " & strToo & vbNewLine & "Subject: " & strSubject & _
                'vbNewLine & "Body: " & strBody)

                'call close method to delete the emails.
                If CheckBox1.Checked = True Then
                    'WARNING - uncommening the line below WILL remove the message from the mail server
                    'popConn.MarkForDelete(i)
                End If
            Next i
        End If

        'Quit the connection to the server
        popConn.CloseConn()

        Button1.Enabled = True

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I currently work at for a newspaper group in Lancashire(U.K) in the IT dept. I deal with a quite a few different IT system on a day to day basis. My programming experience includes vb6, asp.net, sql and some C++. Check my blog out at beakersoft.co.uk.

I have also recently started writing software with a couple of guys i work with, check out our website at www.we3soft.com

Comments and Discussions