FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to read mails from Gmail Inbox
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
How to read mails from Gmail Inbox
Posted: Tue May 20, 2014 12:23 AM

Hi Friends,

Can anybody help me with some code, to read the mails from Gmail Inbox.

Regards,

-Ramesh Babu P

Posts: 518
Joined: Fri Jun 29, 2012 12:49 PM
Re: How to read mails from Gmail Inbox
Posted: Tue May 20, 2014 02:11 PM
Please, check this:

Code (fw): Select all Collapse
// Testing FiveWin new Internet incoming mail (POP3 protocol) Class

#include "FiveWin.ch"

static oWnd

//----------------------------------------------------------------------------//

function Main()

   local oBar

   DEFINE WINDOW oWnd TITLE "Receiving Internet Mail from FiveWin"

   DEFINE BUTTONBAR oBar _3D OF oWnd

   DEFINE BUTTON OF oBar ACTION GetMail() TOOLTIP "Get Mail"

   SET MESSAGE OF oWnd TO "Ready" NOINSET DATE TIME KEYBOARD

   ACTIVATE WINDOW oWnd

return nil

//----------------------------------------------------------------------------//

function GetMail()

   local oInMail
   local cIp

   if WsaStartup() == 0
      cIp := gethostbyname( "pop.gmail.com" )
      WsaCleanUp()
   endif

   oWnd:SetMsg( "Geting Internet email..." )

   oInMail = TPop4():New( cIp,, "miusuario", "miclave" )

   oInMail:bConnecting = { || oWnd:SetMsg( "Connecting to " + cIp ) }
   oInMail:bConnected  = { || oWnd:SetMsg( "Connected" ) }
   oInMail:bDone       = { || ReadEmails( oInMail ) }

   oInMail:GetMail()

return nil

//----------------------------------------------------------------------------//

function ReadEmails( oInMail )

   local n, cTxt := ""

   MsgInfo( "Total emails: " + Str( Len( oInMail:aMsgs ) ) )

   for n = 1 to Len( oInMail:aMsgs )
      cTxt += oInMail:aMsgs[ n ] + CRLF + "--- FINAL DE ESTE MAIL ---" + CRLF
   next

   memowrit( "email.txt", cTxt )

return nil

//----------------------------------------------------------------------------//


Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to read mails from Gmail Inbox
Posted: Tue May 20, 2014 05:19 PM
Elvira,

elvira wrote:
Code (fw): Select all Collapse
oInMail = TPop4():New( cIp,, "miusuario", "miclave" )


TPop4???

EMG
Posts: 518
Joined: Fri Jun 29, 2012 12:49 PM
Re: How to read mails from Gmail Inbox
Posted: Wed May 21, 2014 08:18 AM
Enrico,

Try tpop3 and this code:

Code (fw): Select all Collapse
// FiveWin Internet incomming mail Class

#include "FiveWin.ch"

// different session status

// CONNECT
#define ST_CONNECTING  1
#define ST_IDENTIFY1   2
#define ST_IDENTIFY2   3
#define ST_STAT       99
#define ST_REQLIST     4
#define ST_IDENTIFY4   5
#define ST_IDENTIFY5   6
#define ST_SENDBODY    7
#define ST_SENDQUIT    8
#define ST_ENDED      10
#define ST_REQNEXT    98
#define ST_REQMSG     96
#define ST_DELMSG     97

// STOR
#define ST_STOR_PORT     "ST_PORT"
#define ST_STOR_OPENCONN "ST_OPENCONN"
#define ST_STOR_START    "ST_START"

#define CONNECT_OK      "220"
#define HELLO_OK        "250"
#define MAILFROM_OK     "250"
#define MAILTO_OK       "250"
#define DATA_OK         "354"
#define LOGIN_OK        "230"
#define MSG_OK          "250"
#define CLOSE_OK        "221"
#define ENDMSG          "."
#define ST_NOOP       6
#define ST_OK           "+OK"

//----------------------------------------------------------------------------//

CLASS TPop4

   DATA   oSocket     // socket used during the Control session
   DATA   nPort
   DATA   User
   DATA   Pass
   DATA   lOk
   DATA   aMsgs
   DATA   nAtMsg
   DATA   cIPServer   // IP of the mail server
   DATA   AllOk
   DATA   nStatus     // Temporary session status
   DATA   lDelMsgs

   DATA   bConnecting // Action to perform while trying to connect to the server
   DATA   bConnected  // Action to perform when already connected to the server
   DATA   bDone       // Action to perform when Msgs have been retrieved

   METHOD New( cIPServer, nPort, User, Pass ) CONSTRUCTOR
   METHOD GetMail()
   METHOD OnRead( oSocket )
   METHOD LogError( cData, oSocket )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cIPServer, nPort, User, Pass ) CLASS TPop4

   DEFAULT nPort := 110

   ::AllOk     := .f.
   ::cIPServer := cIPServer
   ::User      := User
   ::Pass      := Pass
   ::nStatus   := ST_CONNECTING
   ::nPort     := nPort
   ::lDelMsgs  := .t.

return self

//----------------------------------------------------------------------------//

METHOD GetMail() CLASS TPop4

   ::oSocket := TSocket():New( ::nPort )
   ::oSocket:bRead = { | o | ::OnRead( o ) }
   ::oSocket:Connect( ::cIpServer )

return Self

//----------------------------------------------------------------------------//

METHOD OnRead( oSocket ) CLASS TPop4

   local cData    := oSocket:GetData()
   local rCode    := SubStr( cData, 1 ,3 )
   local cMsg     := SubStr( cData, 3 )
   local cCommand := ""
   local n

//msginfo( rCode    )
//msginfo( cMsg     )

   do case
      case ::nStatus == ST_CONNECTING
           if rCode == ST_OK
              cCommand  = "USER " + ::User + CRLF
              ::nStatus = ST_IDENTIFY1
              if ::bConnecting != nil
                 Eval( ::bConnecting, Self )
              endif
           else
              ::LogError( cData, oSocket )
           endif

      case ::nStatus == ST_IDENTIFY1
           if rCode == ST_OK
              cCommand  = "PASS " + ::Pass + CRLF
              ::nStatus = ST_IDENTIFY2
           else
              ::LogError( cData, oSocket )
           endif

      case ::nStatus == ST_IDENTIFY2
           if rCode == ST_OK
              cCommand  = "STAT " + CRLF
              ::nStatus = ST_STAT
           else
              ::LogError( cData, oSocket )
           endif

      case ::nStatus == ST_STAT
           if rCode == ST_OK
              if ::bConnected != nil
                 Eval( ::bConnected, Self )
              endif
              cData = StrTran( cData, " ", "&" ) + "&"
              ::aMsgs := Array( Val( StrToken( cData, 2, "&" ) ) )
              for n = 1 to Len( ::aMsgs )
                  ::aMsgs[ n ] = ""
              next
              if Len( ::aMsgs ) == 0
                 cCommand  = "QUIT" + CRLF
                 ::nStatus = ST_SENDQUIT
              else
                 ::nAtMsg  = 1
                 cCommand  = "RETR " + AllTrim( Str( 1, 4, 0 ) )+ CRLF
                 ::nStatus = ST_REQMSG
              endif
           else
              ::LogError( cData, oSocket )
           endif

      case ::nStatus == ST_DELMSG
           if rCode == ST_OK
              if ::nAtMsg < Len( ::aMsgs )
                 cCommand  = "RETR " + AllTrim( Str( ++::nAtMsg, 4, 0 ) ) + CRLF
                 ::nStatus = ST_REQMSG
              else
                 cCommand  = "QUIT" + CRLF
                 ::nStatus = ST_SENDQUIT
              endif
           else
              ::LogError( cData, oSocket )
           endif

      case ::nStatus == ST_REQMSG
           if ::nAtMsg <= Len( ::aMsgs )
              ::aMsgs[ ::nAtMsg ] += cData
              if At( Chr( 13 ) + Chr( 10 ) + "." + Chr( 13 ) + Chr( 10 ), cData ) > 0
                 if ::lDelMsgs
                    cCommand  = "DELE " + AllTrim( Str( ::nAtMsg, 4, 0 ) ) + CRLF
                    ::nStatus = ST_DELMSG
                 else
                    cCommand  = "RETR " + AllTrim( Str( ++::nAtMsg, 4, 0 ) ) + CRLF
                    ::nStatus = ST_REQMSG
                 endif
              else
                 ::nStatus = ST_REQMSG
              endif
           else
              cCommand  = "QUIT" + CRLF
              ::nStatus = ST_SENDQUIT
              ::AllOk   = .t.
           endif

    case ::nStatus == ST_SENDQUIT
           if rCode == ST_OK
              cCommand = ""
              oSocket:End()
              ::nStatus = ST_ENDED
              ::AllOk   = .t.
           else
              ::LogError( cData, oSocket )
           endif
     endcase

msginfo( cCommand )
     if ! Empty( cCommand )
        oSocket:SendData( cCommand )
        if cCommand == "QUIT" + CRLF
           if ::bDone != nil
              Eval( ::bDone, Self )
           endif
        endif
     endif

return nil

//----------------------------------------------------------------------------//

METHOD LogError( cData, oSocket ) CLASS TPop4

   oSocket:End()
   LogFile( "MailErr.log", { ::nStatus,cData } )
   ::AllOk := .f.

return nil

//----------------------------------------------------------------------------//

Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: How to read mails from Gmail Inbox
Posted: Wed May 21, 2014 10:19 PM

Mr.Elvira,

Thank you for your code. I could build EXE with your code and after Clicking on
the button, it is not displaying any thing. I tried with TPop4 Class and TPop3 also.

Can you please guide me where I am going wrong or anything else I need.

Regards,
-Ramesh Babu P

Continue the discussion