Oppure

Loading
Questo topic e' stato chiuso dal moderatore.
06/04/09 14:51
AlesPalla
Allora ho creato un programma che si connette alla mia casella gmail e che preleva tutti i messaggi.
Il problema è che io non riesco a estrapolare dal messaggio il mittente e il messaggio vero e proprio.
Penso che lo debba fare tramite espressioni regolari ma non ho la più pallida idea di come fare!


PS: Qualcuno conosce una classe che permetta di connettersi ad un server POP3 sensa sorbirsi tutto il protocollo!
(Io uso TCPClient per inviare volta volta i dati x l'autenticazione e controllare se le risposte sono giuste. Sarebbe bello esistesse una classe che faccia tutto in automatico!)


Ah scusate: Il linguaggio è VB.net
Ultima modifica effettuata da AlesPalla 06/04/09 15:01
aaa
07/04/09 13:38
Il Totem
I socket sono l'unico modo. Puoi comunque crearti una classe wrapper molto più comoda, che potrai riusare anche in casi successivi. Io lo faccio sempre.
Espressioni regolari:
totem.altervista.org/guida/versione2/…

Mi piacerebbe anche vedere il codice dato che i miei tentativi sono sempre falliti, a causa del fatto che nessuno dei miei indirizzi email supporta il POP3.
aaa
07/04/09 19:16
AlesPalla
Premetto subito che il codice è ancora moooolto rozzo:
devo aggiungere controlli x le eccezioni, poi ci sono variabili superflue che potrei eliminare, elementi ripetitivi che potrei trasformare in procedure/funzioni ecc...
Inoltre devo ringraziarti x i codici di ricezione e di invio di dati che li ho presi dalla tua guida e che sono i miglioritra quelli che ho testato.



Imports System.Net.Sockets
Imports System.Text.ASCIIEncoding

 Public Function Connetti(ByVal username As String, ByVal pass As String)
        'Provo a connettermi
        Dim client As New TcpClient
        Dim NetStr As NetworkStream
        'x vedere se va a buon fine
        Dim S As String = ""
        client.Connect("pop.gmail.it", 110)

        netstr = client.GetStream()


        '\\Conferma connessione

        Dim A As Boolean = False
        Dim mystring As String = Nothing
        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                netstr.Read(byteServer, 0, byteServer.Length)
                mystring = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False

        '\\\Parte 1 (Username)
        If mystring.StartsWith("+OK") Then
            Dim bytes1() As Byte = ASCII.GetBytes("USER " + username + vbCrLf)
            netstr.Write(bytes1, 0, bytes1.Length)
        Else
            Return S
            client.Close()
            MsgBox("Disconnesso")
            Exit Function
        End If


        '\\\Ricevi conferma username
       

        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                netstr.Read(byteServer, 0, byteServer.Length)
                mystring = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False

        '\\\Parte 2(Pass)
        If mystring.StartsWith("+OK") Then

            Dim bytes2() As Byte = System.Text.Encoding.ASCII.GetBytes("PASS " + pass + vbCrLf)

            netstr.Write(bytes2, 0, bytes2.Length)
        Else
            Return S
            client.Close()
            MsgBox("Disconnesso")
            Exit Function
        End If

        '\\Ricevi conferma pass

        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                netstr.Read(byteServer, 0, byteServer.Length)
                mystring = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False

        '\\Parte 3(Elenco Messaggi)
        If mystring.StartsWith("+OK") Then

            Dim bytes3() As Byte = System.Text.Encoding.ASCII.GetBytes("list " + vbCrLf)

            netstr.Write(bytes3, 0, bytes3.Length)
        Else
            Return S
            client.Close()
            MsgBox("Disconnesso")
            Exit Function
        End If
        '\\Ricevi elenco messaggi
        mystring = ""
        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                netstr.Read(byteServer, 0, byteServer.Length)
                S = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False
        Dim arr() As String
        mystring = S.Replace("+OK POP3 clients that break here, they violate STD53." + vbCrLf, Nothing)
        arr = mystring.Split(vbCrLf)

        For Each I In arr
            Dim h As String() = I.Split(" ")
            If h(0).Contains(".") = True Or h(0) = " " Then
                Exit For
            End If
            Dim x As Char() = h(0).ToCharArray()
            Dim b As String = ""
            For Each T In x
                If (T = "0" Or T = "1" Or T = "2" Or T = "3" Or T = "4" Or T = "5" Or T = "6" Or T = "7" Or T = "8" Or T = "9") Then
                    b += T
                End If
            Next
            MsgBox(b)
            Messaggio(b, client, NetStr)


            'elimina i messaggi
            If b.Contains(" ") = False Then
                Dim by() As Byte =ASCII.GetBytes("dele " + b + vbCrLf)
                NetStr.Write(by, 0, by.Length)
            End If

            A = False

            Do
                Dim byteServer(client.ReceiveBufferSize) As Byte
                If Not (client.ReceiveBufferSize = 0) Then
                    NetStr.Read(byteServer, 0, byteServer.Length)
                    mystring = ASCII.GetString(byteServer)

                    A = True
                End If
            Loop Until A = True

        Next
       
        A = False



        '\\Parte finale(Quit)


        Dim bytes() As Byte = ASCII.GetBytes("quit" + vbCrLf)

        NetStr.Write(bytes, 0, bytes.Length)

        client.Close()
        MsgBox("Disconnesso")

        Return S
    End Function




Private Sub Messaggio(ByVal I As String, ByVal client As TcpClient, ByVal netstr As NetworkStream)
        If I.Contains(" ") = True Then
            Exit Sub
        End If
        Dim S As String = ""
        Dim A As Boolean = False
        
        '\\Parte 4(leggi messaggio)


        Dim bytes() As Byte = ASCII.GetBytes("retr " + I + vbCrLf)

        NetStr.Write(bytes, 0, bytes.Length)


        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                NetStr.Read(byteServer, 0, byteServer.Length)
                S = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False

        Do
            Dim byteServer(client.ReceiveBufferSize) As Byte
            If Not (client.ReceiveBufferSize = 0) Then
                NetStr.Read(byteServer, 0, byteServer.Length)
                S = ASCII.GetString(byteServer)

                A = True
            End If
        Loop Until A = True
        A = False

        TextBox1.Text = S





    End Sub




Ps: Io uso i commenti il meno possibile perchè mi distraggono, quindi se qualche parte è poco chiara chiedi pure.

Comunque il mio problema sta nella procedura messaggio visto che non riesco a distinguere nella stringa S il campo from e il messaggio vero e proprio.
Ultima modifica effettuata da AlesPalla 07/04/09 19:22
aaa
08/04/09 8:32
AlesPalla
Grazie alla guida sono riuscito a isolare il campo from



        Dim Email As New Regex("(From: )\b(\w+)\s*(@|at|\[at\])\s*(\w+)\s*(\.|dot|\[dot\])(\w+)", RegexOptions.Multiline)
        For Each M As Match In Email.Matches(S)
            From = M.Groups(2).Value + "@" + M.Groups(4).Value + "." + M.Groups(6).Value

        Next


Ma per il corpo del messaggio non c'è verso...
aaa
08/04/09 14:00
Il Totem
Beh io non so come viene formattata la risposta. Se mi mandi un esempio posso aiutarti.
aaa
09/04/09 7:33
AlesPalla

Return-Path: <mittente>
X-Original-To: destinatario
Delivered-To: destinatario
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id 26878134C8C9
	for <destinatario>; Tue,  7 Apr 2009 23:46:49 +0200 (CEST)
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id F1464134C8C8
	for <destinatario>; Tue,  7 Apr 2009 23:46:48 +0200 (CEST)
X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on
	km33637.keymachine.de
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=7.0 tests=none autolearn=disabled
	version=3.2.5
Received: from cp-out7.libero.it (cp-out7.libero.it [212.52.84.107])
	by gmail.it (Postfix) with ESMTP
	for <destinatario>; Tue,  7 Apr 2009 23:46:48 +0200 (CEST)
Received: from alessand-1095b8 (151.49.12.238) by cp-out7.libero.it (8.5.016.1)
        id 49C8EC350149E408 for destinatario; Tue, 7 Apr 2009 23:45:28 +0200
Message-ID: <49C8EC350149E408@cp-out7.libero.it> (added by postmaster@cp-out7.libero.it)
MIME-Version: 1.0
From: mittente
To: destinatario
Date: 7 Apr 2009 23:45:24 +0200
Subject: prova
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Virus-Scanned: ClamAV using ClamSMTP


Qui c'è il testo

.


Mittente=indirizzo di partenza
Destinatario= indirizzo finale
Ultima modifica effettuata da AlesPalla 09/04/09 7:35
aaa
09/04/09 7:40
Il Totem
In effetti è un bel problema. Ci dovrebbe essere anche il campo Contet-Length alla fine, perchè qui non c'è?
aaa
11/04/09 12:52
AlesPalla
Allora no il campo Content-Lenght non c'è ma ho parzialmente aggirato il problema.
Mi sono letto tutta la documebntazione sul pop3 e ho trovato il comado top.
Questo necessita di 2 parametri, il numero del messaggio e il numero di righe del corpo del messaggio da visualizzare.
Quindi se io voglio leggere gli header di un messaggio(x esempio il numero 1) faccio top 1 0 e ottengo gli header del messaggio, e fin qui nessun problema...
Per esempio di questo messaggio:

Return-Path: <mittente>
X-Original-To: deestinatario
Delivered-To: destinatario
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id 294602A0200C
	for <destinatario>; Wed,  8 Apr 2009 10:52:19 +0200 (CEST)
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id 00D7B2A02002
	for <destinatario>; Wed,  8 Apr 2009 10:52:19 +0200 (CEST)
X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on
	km33637.keymachine.de
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=7.0 tests=none autolearn=disabled
	version=3.2.5
Received: from cp-out9.libero.it (cp-out9.libero.it [212.52.84.109])
	by gmail.it (Postfix) with ESMTP
	for <destinatario>; Wed,  8 Apr 2009 10:52:18 +0200 (CEST)
Received: from alessand-1095b8 (151.49.49.125) by cp-out9.libero.it (8.5.016.1)
        id 49C8ECEA0152A325 for recivitor@gmail.it; Wed, 8 Apr 2009 10:41:48 +0200
Message-ID: <49C8ECEA0152A325@cp-out9.libero.it> (added by postmaster@cp-out9.libero.it)
MIME-Version: 1.0
From: mittente
To: destinatario
Date: 8 Apr 2009 10:41:44 +0200
Subject: ciao
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Virus-Scanned: ClamAV using ClamSMTP

prova 2
.


ottengo (dopo alcune formattazioni) questo header:

Return-Path: <mittente>
X-Original-To: destinatario
Delivered-To: destinatario
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id 294602A0200C
	for <destinatario>; Wed,  8 Apr 2009 10:52:19 +0200 (CEST)
Received: from gmail.it (localhost.localdomain [127.0.0.1])
	by gmail.it (Postfix) with ESMTP id 00D7B2A02002
	for <destinatario>; Wed,  8 Apr 2009 10:52:19 +0200 (CEST)
X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on
	km33637.keymachine.de
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=7.0 tests=none autolearn=disabled
	version=3.2.5
Received: from cp-out9.libero.it (cp-out9.libero.it [212.52.84.109])
	by gmail.it (Postfix) with ESMTP
	for <destinatario>; Wed,  8 Apr 2009 10:52:18 +0200 (CEST)
Received: from alessand-1095b8 (151.49.49.125) by cp-out9.libero.it (8.5.016.1)
        id 49C8ECEA0152A325 for recivitor@gmail.it; Wed, 8 Apr 2009 10:41:48 +0200
Message-ID: <49C8ECEA0152A325@cp-out9.libero.it> (added by postmaster@cp-out9.libero.it)
MIME-Version: 1.0
From: mittente
To: destinatario
Date: 8 Apr 2009 10:41:44 +0200
Subject: ciao
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Virus-Scanned: ClamAV using ClamSMTP


supponiamo che il messaggio sia una variabile string S e gli header siano una variabile string h.
Quindi per avere il corpo del messaggio io faccio:
S=S.replace(h,nothing)
ma mi da sempre la stringa originale!!!

ho provato pure a comparare tramite hash md5 la tringa header e la sua corrispettiva nel messaggio e danno lo stesso hash!


Ho come la sensazione di perdermi in un bicchier d'acqua...
aaa