none
TCP Client Pop3 decode the downlowded email RRS feed

  • Question

  • hi, i hope someone guide me,
    several days am working on a TCP Client to connect to my hotmail account and with the POP3 commands to retreive the emails that are in the server
    well i was able to succesfully download any email of my choice with the command
    RETR 1 , and am saving this all data in a variable of type string, my questions is follows,
    1 how can i split the sections of that data that represents an email? e.g header , body , attachaments , embedded images ?
    is there any vb net functions that are "easily" allow me to use them and split them ? or i need to implement my own code to analyse all those string and extract from them the body header attachments ? how can i do that? how can i work with that email string after download it from the server.

    2nd. some where i read that i need to decode the body before trying to use it?

    P.S what i have try is from that string that represent an email  to cut over all the string and leave only string where it starts with an <html ... and ends with </html>
     but when i send this string to the WebBrowser control the web browser presents me the body of the email but not in a well formatted . it displays some symbols like = and others.

    please show merci :)))) am new on this TCPclient .


    stelios ----------

    Friday, July 20, 2012 11:15 PM

Answers

  • The email is an HTML Document.  You can use the metnod  GetElementsByTagName() to get the different sections of the email.  You can also save the results to a Word coument, a Rich Text  object, Wrod Pad,  or a HTML file.  All these file type can read a HTML formated object.

    jdweng

    Saturday, July 21, 2012 12:41 AM
  • i have read somewhere that if you save raw email as it is with the extension .eml this will be saved as an email object in your computer and therefor you can open it throught an internet explorer or windows live email ,or outlook , i have try this and seems that is working perfectly,

    i just thought to say that for others than may experience the same issue with me
    is not the answer but is an alternative solution to what i was looking for.
    a small example code here

     Function SaveEmail(ByVal TheDownloadedData As String) As Boolean
            Dim MyReader As IO.StringReader = New IO.StringReader(TheDownloadedData )
            Dim MsgID As String = Nothing
            Do
                Dim Tem As String = MyReader.ReadLine
    
                If Tem.Contains("Message-ID: <") Then
                    MsgID = Tem.Remove(0, Tem.IndexOf("Message-ID: <") + 13)
                    MsgID = MsgID.Remove(MsgID.IndexOf(">"))
                    Exit Do
                End If
            Loop
    
            Dim FILE_NAME As String = "C:\Users\user_name\Desktop\" & MsgID & ".eml"
            Dim writethis As IO.StreamWriter = New IO.StreamWriter(FILE_NAME)
            writethis.Write(TheDownloadedData)
            writethis.Flush()
            writethis.Close()
            writethis.Dispose()
        End Function

    i also have some other issues with the TCP but i will open a new thread for those
    P.S if someone knows how to parse an eml object to      net.mail.mailmessage in VB NET will appreciated


    stelios ----------



    Tuesday, July 24, 2012 6:45 AM

All replies

  • The email is an HTML Document.  You can use the metnod  GetElementsByTagName() to get the different sections of the email.  You can also save the results to a Word coument, a Rich Text  object, Wrod Pad,  or a HTML file.  All these file type can read a HTML formated object.

    jdweng

    Saturday, July 21, 2012 12:41 AM
  • hi joel am really appreciating your help , can you please help me a little more how to succed on this?
    i have the following code.
      Pop3Account = New TcpClient
            Pop3Account.Connect(MyServer, MyPort)
            Dim MySslStream As New SslStream(Pop3Account.GetStream)
            MySslStream.AuthenticateAsClient(MyServer, Nothing, Security.Authentication.SslProtocols.Tls, False)
            Dim StrReader As New IO.StreamReader(MySslStream)
            Dim StrWriter As New IO.StreamWriter(MySslStream)
            Dim StatusString As String = Nothing
            Dim StrReturn As String = Nothing
            StrReturn = StrReader.ReadLine
    
            StrWriter.WriteLine("USER " & MyUserName) 
            StrWriter.Flush()
            StrReturn = StrReader.ReadLine
            StrWriter.WriteLine("PASS " & MyPassword)
            StrWriter.Flush()
            StrReturn = StrReader.ReadLine
            StrWriter.WriteLine("RETR 1")
            StrWriter.Flush()
    
            Do
                StatusString = StrReader.ReadLine
                If StatusString = "." Or Left(StatusString, 4) = "-ERR" Then
    
                    Exit Do
                End If
                StrReturn &= StatusString & vbNewLine
            Loop
    
            StrWriter.WriteLine("QUIT")
            StrWriter.Flush()
            Pop3Account.Close()
            StrReader.Close()
            StrWriter.Close()
            MySslStream.Close()
            strReturn = ? what should i do here in order to get the body ? 



    stelios ----------

    Sunday, July 22, 2012 8:46 AM
  • I'm not sure how you are retrieving the emails.  If the emails are in HTML format than the body of the email will be in a tag that look like this

    <body>  html text        </body>

    To extract the body portion of the email you can put the HTML into a document and then use     GetElementsByTagName() method to get the body portion of the email.  

    Imports System.Net.Sockets
    Imports System.Net.Security
    Imports System.Windows.Forms
    Module Module1
        Sub Main()
            Dim Pop3Account As TcpClient
            Dim MyServer As String = ""
            Dim MyPort As String = ""
            Dim MyUserName As String = ""
            Dim MyPassword As String = ""
            Pop3Account = New TcpClient
            Pop3Account.Connect(MyServer, MyPort)
            Dim MySslStream As New SslStream(Pop3Account.GetStream)
            MySslStream.AuthenticateAsClient(MyServer, Nothing, Security.Authentication.SslProtocols.Tls, False)
            Dim StrReader As New IO.StreamReader(MySslStream)
            Dim StrWriter As New IO.StreamWriter(MySslStream)
            Dim StatusString As String = Nothing
            Dim StrReturn As String = Nothing
            StrReturn = StrReader.ReadLine
            StrWriter.WriteLine("USER " & MyUserName)
            StrWriter.Flush()
            StrReturn = StrReader.ReadLine
            StrWriter.WriteLine("PASS " & MyPassword)
            StrWriter.Flush()
            StrReturn = StrReader.ReadLine
            StrWriter.WriteLine("RETR 1")
            StrWriter.Flush()
            Do
                StatusString = StrReader.ReadLine
                If StatusString = "." Or Left(StatusString, 4) = "-ERR" Then
                    Exit Do
                End If
                StrReturn &= StatusString & vbNewLine
            Loop
            Dim doc As HtmlDocument = Nothing
            doc.OpenNew(True)
            doc.Write(StrReader.ReadToEnd())
            Dim body As HtmlElementCollection
            body = doc.GetElementsByTagName("body")
            Dim bodystring As String
            bodystring = body.Item(0).ToString()
            StrWriter.WriteLine("QUIT")
            StrWriter.Flush()
            Pop3Account.Close()
            StrReader.Close()
            StrWriter.Close()
            MySslStream.Close()
        End Sub
    End Module


    jdweng

    Sunday, July 22, 2012 11:35 AM
  • hi joel , am appreciating your suggestion but am afraid this will not help me a lot since the downloaded emails is a raw e-mails where contains a mixed of strings and i should split them . i can not simply parse this as html, i need to seperate the parts, headers,body,attachaments, embeeded images in the email, or to check if is a plain text of an html type etc..


    stelios ----------

    Monday, July 23, 2012 7:00 AM
  • I can't help without know more about the format of the raw emails.  I don't think there is a standard format, and will very from POP3 servers (not sure).  I'm concerned about the the method you are using to download the data.  You are using a string class which may be filtering some of the non ascii characters that is in the data.  I would use an array of bytes and/or use UTF8 encoding.  I'm not sure that defaults (encoding) the the SslStream class and the StreamReader class uses.

    jdweng

    Monday, July 23, 2012 8:27 AM
  • i have read somewhere that if you save raw email as it is with the extension .eml this will be saved as an email object in your computer and therefor you can open it throught an internet explorer or windows live email ,or outlook , i have try this and seems that is working perfectly,

    i just thought to say that for others than may experience the same issue with me
    is not the answer but is an alternative solution to what i was looking for.
    a small example code here

     Function SaveEmail(ByVal TheDownloadedData As String) As Boolean
            Dim MyReader As IO.StringReader = New IO.StringReader(TheDownloadedData )
            Dim MsgID As String = Nothing
            Do
                Dim Tem As String = MyReader.ReadLine
    
                If Tem.Contains("Message-ID: <") Then
                    MsgID = Tem.Remove(0, Tem.IndexOf("Message-ID: <") + 13)
                    MsgID = MsgID.Remove(MsgID.IndexOf(">"))
                    Exit Do
                End If
            Loop
    
            Dim FILE_NAME As String = "C:\Users\user_name\Desktop\" & MsgID & ".eml"
            Dim writethis As IO.StreamWriter = New IO.StreamWriter(FILE_NAME)
            writethis.Write(TheDownloadedData)
            writethis.Flush()
            writethis.Close()
            writethis.Dispose()
        End Function

    i also have some other issues with the TCP but i will open a new thread for those
    P.S if someone knows how to parse an eml object to      net.mail.mailmessage in VB NET will appreciated


    stelios ----------



    Tuesday, July 24, 2012 6:45 AM