Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB/CC 2.0 Example: Getting email from a POP3 server

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PB/CC 2.0 Example: Getting email from a POP3 server

    Various example code that did not make it into the 2.0 release:
    Code:
    #COMPILE CON
    #INCLUDE "WIN32API.INC"
    #INCLUDE "F:\PBCC3\SAMPLES\TCP\TCP32.INC"
    #INCLUDE "RASHANG.BAS"
     
    $Pop3Server = "pop3.domain.com"         ' Your pop3 address
    $ISPName    = "INTERNET_ISP_NAME"       ' The DIALUP Networking Name
    $UserName   = "UserName"                ' Your user name
    $Password   = "password"                ' Your password
     
    FUNCTION PBMAIN()
        DIM Message(1:1000) AS STRING, hTcp AS LONG, Result AS LONG
        DIM MsgQty AS LONG, MsgSize AS LONG, x AS LONG, y AS LONG
        DIM Message(1:1) AS STRING
     
        CONSOLE NAME "Pop3 E-mail Server Check Example"
     
        PRINT "Logging in to pop3 server... " CHR$(34) $Pop3Server CHR$(34)
        hTcp = Pop3Connect($Pop3Server, $UserName, $Password)
        IF ISFALSE hTcp THEN
            PRINT "Error logging in to server (Error code " FORMAT$(Err) ")"
        ELSE
            PRINT "Logged in Ok. Handle = " FORMAT$(hTcp)
        END IF
     
        Result = Pop3GetStat(hTcp, MsgQty, MsgSize)
     
        IF Result THEN
            PRINT "No Messages on server, or an error ocurred! (Error code " FORMAT$(Result) ")"
        ELSE
            PRINT "Server reports " FORMAT$(MsgQty) " Message, Total " FORMAT$(MsgSize) " bytes!"
            PRINT "Getting message(s)..."
     
            FOR x = 1 TO MsgQty
                PRINT "Getting message (" FORMAT$(x) ")"
                Result = Pop3RetrMessage(hTcp, x, Message())
                IF Result THEN
    '                LPRINT ATTACH "LPT2"
                    ARRAY SCAN Message(), FROM 1 TO 8, COLLATE UCASE, = "SUBJECT:", TO y
    '                IF y THEN
    '                    LPRINT "Email " TRIM$(message(y)), "Received " DATE$ "  " TIME$
    '                    LPRINT STRING$(78,45)
    '                END IF
     
                    FOR y = 1 TO Result
                        PRINT Message(y)
    '                    LPRINT Message(y)
    '#if 0
                        IF (y + 2) MOD 24 = 0 THEN
                            PRINT "Press any key for next page....";
                            MessageBeep &H0FFFFFFFF
                            WAITKEY$
                            LOCATE ,1
                            PRINT SPACE$(80);
                            LOCATE ,1
                        END IF
    '#endif
    '                    IF (y + 2) MOD 64 = 0 THEN LPRINT FORMFEED
                    NEXT y
                    IF y MOD 64 > 1 THEN LPRINT FORMFEED
    '                LPRINT FLUSH
                END IF
            NEXT x
        END IF
     
        Pop3Quit hTCP
        TCP CLOSE #hTcp
     
    '    PRINT
    '    PRINT "Finished Checking POP3 server..."
     
    '    PRINT "Now attempting TO HangUp the connection..."
    '    IF ISFALSE PBRasHangUp($ISPName) THEN PRINT "RASHangUp() success!"
     
        PRINT "Finished! Press any key..."
        MessageBeep %MB_ICONHAND
        WAITKEY$
     
    END FUNCTION
    Lance
    PowerBASIC Support

    Lance
    mailto:[email protected]

  • #2
    Has there been an example for Sending email via an SMTP server? One that would be compatible with the 16 bit PB/DLL (1.x or 2.0)

    Comment


    • #3
      Okay..

      So where is RASHANG.BAS, TCP32.INC,
      and what is PBCC3? I just bot 2 !<g>

      Paul
      Paul

      Comment


      • #4
        that was my 3rd test installation... don't read so much into path names!

        rashang.bas was posted at the same time as this code... it is a small sub that hangs up a ras connection to an isp.



        lance
        powerbasic support

        25th april 2000: updated url
        Lance
        mailto:[email protected]

        Comment


        • #5
          I knew I was pushing it..<g>

          Thanks for the pointer to RASHANG...

          The examples are great.

          Paul

          Paul

          Comment


          • #6
            I've incorporated this into a program I'm writing but it doesn't delete the message from the server when it's fetched them. What's missing?

            ------------------
            Neil Croft (cissp)

            Comment


            • #7
              Neil, in the TCP32.INC ther is a POP3RetrMessage function.

              FUNCTION Pop3RetrMessage(BYVAL hTCP AS LONG, BYVAL MsgNumber AS LONG, _
              Msg() AS STRING) AS LONG

              LOCAL x AS LONG
              LOCAL Buffer AS STRING

              errclear

              ON ERROR GOTO Pop3RetrDone

              REDIM Msg(1 to 100) AS STRING

              TCP PRINT hTCP, "RETR" & STR$(MsgNumber)
              TCP LINE hTCP, Buffer

              IF LEFT$(Buffer, 3) <> "+OK" THEN Pop3RetrDone

              DO
              TCP LINE hTCP, Buffer
              IF Buffer = "." THEN
              EXIT DO
              END IF
              INCR x
              IF LEFT$(Buffer, 2) = ".." THEN
              Buffer = "." + MID$(Buffer, 3)
              END IF
              IF x > UBOUND(Msg(1)) THEN
              REDIM PRESERVE Msg(1 to x + 50) AS STRING
              END IF
              Msg(x) = Buffer
              LOOP
              etc. etc.
              After the loop command you need to add...

              TCP PRINT hTCP, "DELE" & STR$(MsgNumber)
              TCP LINE hTCP, Buffer

              IF LEFT$(Buffer, 3) <> "+OK" THEN Pop3RetrDone

              Be warned though - if there is a problem in retreiving the email you could potentially delete it before seeing it. Just a warning !!!
              Alternatively you could write a Pop3DeleMessage function and call it after a successful retrevial. The DELE comamnd just needs the message index after it, just like RETR.
              Hope this helps

              Adrian Aitken

              ------------------

              Comment

              Working...
              X