Lettura mails

Moderator: Enrico Maria Giordano

Lettura mails

Postby Romeo » Fri Jan 25, 2019 5:52 pm

Ciao,

qualcuno ha qualche esempio minimale per leggere le mails con harbour/fw ?

Ho trovato questo esempio nel forum, ma non va, forse perchè è per xHarbour: (qualche dritta ?)

(ho notato che sta fermo su: aMail = oCli:RetrieveAll() per ore e continua a scaricare dati da internet)
(ho usato pop.tiscali.it, userid,password)


STATIC FUNCTION GETMAIL( cServer, cUser, cPassword )

LOCAL lOk := .F.

LOCAL oCli

LOCAL aMail, aPar, cMsg, oAtt

LOCAL i

cUser := StrTran( cUser, "@", "&at;" )
oCli = TIPClientPOP():New( "pop://" + cUser + ":" + cPassword + "@" + cServer )

IF EMPTY( oCli ); BREAK; ENDIF

oCli:oUrl:cUserid = Strtran( cUser, "&at;", "@" )

IF !oCli:Open(); BREAK; ENDIF

aMail = oCli:RetrieveAll()

FOR i = 1 TO LEN( aMail ) - 1
aPar = aMail[ i ]:GetMultiParts()

IF LEN( aPar ) = 0
cMsg = "Data: " + aMail[ i ]:GetFieldPart( "Date" ) + CRLF +;
"Oggetto: " + aMail[ i ]:GetFieldPart( "Subject" ) + CRLF +;
"Mittente: " + aMail[ i ]:GetFieldPart( "From" ) + CRLF + CRLF +;
"Messaggio: " + CRLF + CRLF + aMail[ i ]:GetBody()
ELSE
cMsg = "Data: " + aMail[ i ]:GetFieldPart( "Date" ) + CRLF +;
"Oggetto: " + aMail[ i ]:GetFieldPart( "Subject" ) + CRLF +;
"Mittente: " + aMail[ i ]:GetFieldPart( "From" ) + CRLF + CRLF +;
"Messaggio: " + CRLF + CRLF + BODY( aPar )
ENDIF

WHILE ( oAtt := aMail[ i ]:NextAttachment() ) != NIL
IF !EMPTY( oAtt:GetFileName() )
cMsg += CRLF + "Allegato: " + oAtt:GetFileName()
// IF !oAtt:DetachFile(); BREAK; ENDIF
ENDIF
ENDDO

? cMsg
NEXT

lOk = .T.

IF !EMPTY( oCli ); oCli:Close(); ENDIF

RETURN lOk


STATIC FUNCTION BODY( aPar )

LOCAL cMsg := ""

LOCAL i

FOR i = 1 TO LEN( aPar )
IF EMPTY( aPar[ i ]:GetFieldPart( "Content-Disposition" ) )
cMsg += aPar[ i ]:GetBody()
ENDIF
NEXT

RETURN ALLTRIM( cMsg )
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)

Re: Lettura mails

Postby Enrico Maria Giordano » Fri Jan 25, 2019 5:57 pm

L'esempio è mio ma non è corretto. Eccoti la versione attuale.

Code: Select all  Expand view
STATIC FUNCTION GETMAIL( cServer, cUser, cPassword )

    LOCAL lOk := .F.

    LOCAL oCli

    LOCAL aMail, aPar, cMsg, oAtt

    LOCAL i, j

    TRY
        oCli = TIPClientPOP():New( "pop://" + STRTRAN( cUser, "@", "&at;" ) + ":" + cPassword + "@" + cServer )

        IF EMPTY( oCli ); BREAK; ENDIF

        oCli:oUrl:cUserid = cUser

        IF !oCli:Open(); BREAK; ENDIF

        aMail = oCli:RetrieveAll()

        FOR i = 1 TO LEN( aMail ) - 1
            cMsg = "Data: " + aMail[ i ]:GetFieldPart( "Date" ) + CRLF +;
                   "Oggetto: " + aMail[ i ]:GetFieldPart( "Subject" ) + CRLF +;
                   "Mittente: " + aMail[ i ]:GetFieldPart( "From" ) + CRLF + CRLF

            aPar = aMail[ i ]:GetMultiParts()

            IF LEN( aPar ) = 0
                cMsg += "Messaggio: " + CRLF + CRLF + aMail[ i ]:GetBody()
            ELSE
                cMsg += "Messaggio: " + CRLF + CRLF + BODY( aPar )
            ENDIF

            WHILE ( oAtt := aMail[ i ]:NextAttachment() ) != NIL
                IF !EMPTY( oAtt:GetFileName() )
                    cMsg += CRLF + "Allegato: " + oAtt:GetFileName()
//                    IF !oAtt:DetachFile() THEN BREAK
                ENDIF
            ENDDO

            FOR j = 1 TO LEN( aPar )
                WHILE ( oAtt := aPar[ j ]:NextAttachment() ) != NIL
                    IF !EMPTY( oAtt:GetFileName() )
                        cMsg += CRLF + "Allegato: " + oAtt:GetFileName()
//                        IF !oAtt:DetachFile(); BREAK; ENDIF
                    ENDIF
                ENDDO
            NEXT

            ? cMsg
        NEXT

        lOk = .T.
    CATCH
    END

    IF !EMPTY( oCli ); oCli:Close(); ENDIF

    RETURN lOk


STATIC FUNCTION BODY( aPar )

    LOCAL cMsg := ""

    LOCAL i

    FOR i = 1 TO LEN( aPar )
        IF EMPTY( aPar[ i ]:GetFieldPart( "Content-Disposition" ) )
            cMsg += aPar[ i ]:GetBody()
        ENDIF
    NEXT

    RETURN ALLTRIM( cMsg )


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Romeo » Fri Jan 25, 2019 6:24 pm

Grazie Enrico, ma si ferma sull'istruzione:

aMail = oCli:RetrieveAll()

e scarica mega e mega e non esce più !

Scaricherà mica tutta la posta ?

Proverò a lasciarlo acceso tutta la notte e vediamo che succede !

:-(

Ciao e buon WE
R
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)

Re: Lettura mails

Postby Enrico Maria Giordano » Fri Jan 25, 2019 6:32 pm

Certo che scarica tutta la posta! Cosa ti aspettavi, che scaricasse un solo messaggio? Sicuramente si può fare ma non so come. Perché non lo provi con una casella con pochi messaggi?

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Enrico Maria Giordano » Fri Jan 25, 2019 6:35 pm

E comunque in realtà non scarica niente, ma legge in memoria. Immagino che sia per quello che si blocca. Tieni presente che quello è solo un test dal quale poi uno si sviluppa ciò che gli serve realmente.

In ogni caso, con poche email funziona perfettamente. Ma solo POP3 senza SSL. Per SSL bisogna linkare le librerie OpenSSL ma non ho ancora capito come (vedi altro thread).

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Romeo » Fri Jan 25, 2019 9:12 pm

Ok

Provata su una casella con poche mail e funziona.

Certo che se la casella contiene 10000 msg....... ci vuole un giorno. E poi che senso ha rileggere sempre tutto ?


Mai possibile che non esistono parametri per leggere solo alcune mails, magari per data ricezione ?

Pare imposssibile !

Grazie cmq

A ri-ciao e b we
R
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)

Re: Lettura mails

Postby Enrico Maria Giordano » Fri Jan 25, 2019 10:45 pm

No, il metodo c'è di sicuro. Solo che bisognerebbe studiarsi la classe...

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Enrico Maria Giordano » Fri Jan 25, 2019 10:50 pm

Ho dato uno sguardo e a quanto pare c'è il metodo


Code: Select all  Expand view
oMail = oCli:Retrieve( n )


dove n è il numero del messaggio da scaricare e oMail è l'oggetto tramite il quale si può accedere ai campi della email e ai suoi eventuali allegati.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Romeo » Sat Jan 26, 2019 7:57 pm

Grande Enrico,

vedi che "ravanando" qualcosa si trova.

Certo, sarebbe "ezzziunale verament" riuscire a legge le PEC..

Ma questa è un'altra storia !


Ciao e buon WE

R
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)

Re: Lettura mails

Postby Enrico Maria Giordano » Sat Jan 26, 2019 10:56 pm

Nell'altro thread ho riportato anche la soluzione per leggere le PEC.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Lettura mails

Postby Romeo » Mon Jan 28, 2019 4:33 pm

Scusa Enrico dovè l'altro thread per leggere le PEC ?

Tks
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)


Re: Lettura mails

Postby Romeo » Wed Jan 30, 2019 4:34 pm

Grazie caro,

appena ho una giornata "SI", ci provo !

Saluti
Romeo
 
Posts: 340
Joined: Thu Jan 25, 2007 3:53 pm
Location: Milan (Italy)


Return to All products support

Who is online

Users browsing this forum: No registered users and 23 guests