teledextri
teledextri

Reputation: 125

Receive emails using pop3

I am trying to receive emails using pop3. I can not use "OpenPop.dll" because I can only use .net 4.0.

I can get to the reader but I do not know how to make a printout of what the reader does.

What I want is a way to calculate these four items: From, Subject, Received and Size for each email.

The email itself I would also like to calculate.

    Dim client As TcpClient
    Dim clientStream As Stream
    Dim writer As System.IO.StreamWriter
    Dim reader As System.IO.StreamReader
    Dim host, email, pass As String
    Dim port As Integer
    Dim ssl As Boolean
    host = "pop3.email.com"
    email = "[email protected]"
    pass = "pass1"
    port = 995 
    If client Is Nothing Then
        client = New TcpClient()
    End If
    If Not client.Connected Then
        client.Connect(host, port)
    End If
    If ssl = True Then
        Dim secureStream As New System.Net.Security.SslStream(client.GetStream())
        secureStream.AuthenticateAsClient(host)
        clientStream = secureStream
        secureStream = Nothing
    Else
        clientStream = client.GetStream()
    End If
    writer = New StreamWriter(clientStream)
    reader = New StreamReader(clientStream)

Upvotes: 0

Views: 1013

Answers (2)

jstedfast
jstedfast

Reputation: 38538

It's a fair bit of work to write not only a POP3 client (which isn't that hard, really) but also a MIME parser (which is a ton of work).

I'm not sure what you mean by "The email itself I would also like to calculate.", but I suppose that means you want to parse the whole thing? Or do you only care about parsing the headers to get the From, Subject, and Received headers?

For a list (and links to) the MIME specifications you'll need to review in order to implement a MIME parser, take a look at this page.

You'll also want to review the POP3 specification.

To start, POP3 has a command to get the size of each message (read up on the "STAT" command). There are no commands to get each individual header value, but there is a command to just download the headers if that's all you want (read up on the "TOP" command). You'll need to parse them yourself, though.

Basic parsing of headers isn't too difficult, but it starts getting complicated when you need to handle header values which aren't simple ASCII (i.e. if they are unencoded 8-bit text, encoded according to the rules of rfc2047, or some combination of the 2).

You might not care about decoding header values perfectly and just accept the fact that even if you implement the MIME specifications perfectly, you'll be unable to decode a lot of headers... but if you are interested in knowing what some of the more common/egregious violations of the standard are, I wrote a blog post about it entitled Why decoding rfc2047-encoded headers is hard.

There is a recording of a presentation by Richard Signes that I think is very valuable to watch if you want to learn about parsing email as well. You can find that here: https://www.youtube.com/watch?v=JENdgiAPD6c

Hopefully this is enough to get you started.

Upvotes: 0

Kevin Sandow
Kevin Sandow

Reputation: 4033

Even though it's written in C# there is an open source POP3 implementation that might give you some pointers on how to implement something like that:

http://www.codeproject.com/Articles/21377/NET-POP-MIME-Client

If you want to implement it yourself than I suggest you brush up your POP3 knowledge:

http://www.ietf.org/rfc/rfc1939.txt

Upvotes: 1

Related Questions