Click here to Skip to main content
15,883,603 members
Articles / Desktop Programming / Windows Forms
Article

POP3 Client

Rate me:
Please Sign up or sign in to vote.
3.10/5 (10 votes)
12 May 2008CPOL1 min read 50.7K   1.4K   20   7
Use of TCP client to get emails from POP3 server, tray icon user, Web browser control use
ComcastTrayIcon

Introduction

I was faced with a problem recently. I was trying to find a way to get a notification, similar to Outlook, when I get a new message in my Comcast Inbox. There was no software available to do this. As a result, I started a project that would enable me to do so. Slowly but surely, the project has grown to include the following features:

  • Connect to POP3 server (defaults to Comcast)
  • Get total number of messages from the server
  • Get actual message text from the server
  • Include tray notification icon

The project provided has several key classes. The main one is EmailClient. It connects to the server using TCPClient class. All communication occurs using basic .NET classes: NetworkStream and StreamReader. To login, the client is sending basic POP3 commands, including USER and PASS(word). Here is method used to connect and login:

VB.NET
Private _stream As NetworkStream
Private _reader As StreamReader
Private _server As String
Private _user As String
Private _password As String
Private _tcpClient As TcpClient

Private Sub ConnectToServer()
    Try
        _tcpClient.Connect(_server, Port)
        _stream = _tcpClient.GetStream()
        _reader = New StreamReader(_stream)
        _isConnected = True
    Catch ex As Exception
        _isConnected = False
    End Try
End Sub

Private Sub Login()
    _isLoggedIn = False
    If _isConnected Then
        ' send user ID
        Dim data As String = "USER " + _user.Trim + Environment.NewLine
        WriteToServer(data)

        'discard response
        If ReadFromServer(1).Substring(0, GoodResponse.Length) = GoodResponse Then
            ' send password
            data = "PASS " & _password & Environment.NewLine
            WriteToServer(data)
            If ReadFromServer(1).Substring(0, GoodResponse.Length) = GoodResponse Then
                _isLoggedIn = True
            End If
        End If
    End If
End Sub

Additional tray icon functionality is provided via the NotifyIcon class. This class when dropped onto the form provides an easy to use model to get tray icon notifications. For example, this is how we can maximize the form using a double-click event:

VB.NET
Private Sub EmailNotifyIcon_DoubleClick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles EmailNotifyIcon.DoubleClick
    Me.Show()
    Me.WindowState = FormWindowState.Normal
    EmailTimer.Enabled = True
End Sub

To use the client, create an instance and pass in credentials:

VB.NET
Using email As New EmailClient(Me.txtServer.Text.Trim, _
    Me.txtUser.Text.Trim, Me.txtPassword.Text.Trim)
    If email.IsConnected Then
        If email.IsLoggedIn Then
            messages = email.TotalMessages()
            _messages = email.GetMessageList()
        Else
            Me.EmailNotifyIcon.Text = "Email - Invalid login/server."
            EmailNotifyIcon.ShowBalloonTip(10000, "Email", _
                "Invalid server/login", ToolTipIcon.Error)
            messages = -2
        End If
        btnPreview.Enabled = (messages > 0)
    Else
        Me.EmailNotifyIcon.Text = "Email - Invalid login/server."
        EmailNotifyIcon.ShowBalloonTip(10000, "Email", _
                "Invalid server/login", ToolTipIcon.Error)
        messages = -2
    End If
End Using

In addition to that, browser control was used to provide the preview functionality. To achieve that, email plain text is parsed and HTML is extracted out. The HTML is then output to a file in the executable folder. Finally, the browser control is created, sized to the preview form size and its URL is set to email file:

VB.NET
Dim url As Uri = New System.Uri(oneMessage.CreateHTMLFile())

    Public Function CreateHTMLFile() As String
        Dim folder As String = My.Application.Info.DirectoryPath
        If Not folder.EndsWith(IO.Path.DirectorySeparatorChar) Then
            folder = folder & IO.Path.DirectorySeparatorChar
        End If
        Dim fileName As String = folder & "email_" & _messageNumber.ToString & ".html"
        IO.File.WriteAllText(fileName, _html)
        Return fileName
    End Function

Please download and review the project for more details.

History

  • 12th May, 2008: Initial post

License

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


Written By
Software Developer (Senior) Magenic
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood News !! Visual Studio now has support for POP3, SMTP, IMAP, etc Pin
Tino Fourie21-Feb-14 10:01
Tino Fourie21-Feb-14 10:01 
GeneralAttachments Pin
Smithers92921-May-11 13:59
Smithers92921-May-11 13:59 
GeneralRe: Attachments Pin
Sergey Barskiy22-May-11 2:40
Sergey Barskiy22-May-11 2:40 
QuestionHow to Read folder lik "junk" or any customized folder on mailserver? Pin
jymitra19-Jul-10 21:45
jymitra19-Jul-10 21:45 
GeneralTo get code in to C# Pin
NitinMakwana23-Feb-10 2:58
NitinMakwana23-Feb-10 2:58 
AnswerRe: To get code in to C# Pin
thatraja28-Jan-12 6:02
professionalthatraja28-Jan-12 6:02 
GeneralSsl Pin
blacer27-May-09 23:19
blacer27-May-09 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.