Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

POP3 Client

3.10/5 (10 votes)
12 May 2008CPOL1 min read 1   1.4K  
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)