Page 1 of 1

POP3 Email

Posted: Sun Jun 22, 2003 5:18 pm
by Large
Could someone please provide me a bit of code that tells me how many emails are on my pop3 server.

All I need is a simple procedure that will check my mailbox every 5 mins and reports back to my program how many emails into a variable.

So in my program window, I would have something like . . .

You have 3 unread emails.

Then if you click the number 3 in the message it would launch Outlook Express.

If anyone could provide with some help here it would be much appreciated.

Kind regards

Andy ( aka Large )

Re: POP3 Email

Posted: Sun Jun 22, 2003 6:32 pm
by ricardo
Did you check on the FAQ at the top of this begginers post?

Posted: Sun Jun 22, 2003 8:23 pm
by Large
Yes I have checked there, but they are examples on sending and recieving email, all I want is a simple procedure to connect to a pop3 server and return the value of emails in a variable.

I'm sure its possible in about 10 lines of code or so.

Can anyone help me ? :roll:

Kind regards

Andy ( aka Large )

Posted: Sun Jun 22, 2003 9:05 pm
by benny
Hi Large,

I thought about your problem and coded the following snippet. It is for my german gmx-pop-account. The code looks like this - you need to modify it to your account / mailsystem.
Hope it helps you. Works fine here.

Greets.benny!

Code: Select all


; Quick-Email-Tester by benny!
; For Large - english forum

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

EOL$ = Chr(13)+Chr(10)
*Buffer = AllocateMemory(0, 10000)

ConnectionID = OpenNetworkConnection("pop.gmx.net", 110)

If ConnectionID

  Delay (2000)
  
  SendNetworkString(ConnectionID, "USER fullemailadress"+eol$)

  Delay (2000)
  
  SendNetworkString(ConnectionID, "PASS yourpassword"+eol$)
  
  Delay (2000)
  
  Repeat
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)
  Until RequestLength.l > 3
  
  Command$ = PeekS(*Buffer)
  Position = FindString(Command$, "mailbox", 1)

  If Position > 0
    Result$ = Mid(Command$, Position, 22)
  EndIf
  
  Debug Result$
  
EndIf

CloseNetworkConnection(ConnectionID)

Posted: Sun Jun 22, 2003 9:24 pm
by Large
Hi Benny,

I tried that, I filled in my pop3 server, username and password in the correct places.

When I run the code result$ is null, although there is 1 email on my pop3 server that I sent as a test !

Any suggestions.

Kind regards

Andy ( aka Large )

Posted: Sun Jun 22, 2003 9:46 pm
by Max.
Large wrote:Hi Benny,

I tried that, I filled in my pop3 server, username and password in the correct places.
It might be that your email provider uses a different scheme for the account name than the email-address, i.e. you need the account name.

Just one hint; but

you could add a

Code: Select all

debug Command$
after the line

Code: Select all

 Command$ = PeekS(*Buffer)
to get some output.

Posted: Sun Jun 22, 2003 9:49 pm
by benny
@Large:

Hmm .. if you are not at gmx, maybe it doesn't work, because of another pop-service is installed.
My snippet checks, if the gmx-mail-server sends a sting like "mailbox has X messages".

What you could do, is to telnet your server and check the responses.

Simply type [dos-box]

TELNET pop.gmx.net 110

USER fullemailadress

PASS yourpassword

then there should be the respone about your mailbox.
Maybe then you could modify your code.

Good luck,

benny!

Posted: Sun Jun 22, 2003 9:54 pm
by Max.
benny wrote: Simply type [dos-box]

TELNET pop.gmx.net 110

USER fullemailadress

PASS yourpassword
There is an official command to retrieve the number of messages...

Code: Select all

LIST

Posted: Sun Jun 22, 2003 10:04 pm
by benny
@MAX:

Yep, you're right, although most pop-servers print out the same msg after you logged in. However, you could add the following 2 lines after the DELAY(2000) of the PASS-SendNetworkString:

Code: Select all

  SendNetworkString(ConnectionID, "LIST"+eol$)

  Delay (2000)
Greetz, benny

Posted: Mon Jun 23, 2003 8:28 pm
by Large
Thanks Guys, I will try what you suggested.

Kind regards

Andy ( aka Large )

Posted: Mon Jun 23, 2003 10:14 pm
by TerryHough
Mine is a bit different. But upon review it actually does about what the previous replies have suggested.

This works well for me with two different POP3 servers.

Code: Select all

;-----------------------------------------------------------------------
; POP3 - Check number of messages
; Written by TerryHough - last updated 06/23/2003 
;-----------------------------------------------------------------------

EOL$ = Chr(13)+Chr(10) 
*Buffer = AllocateMemory(0, 5000) 

If InitNetwork() = 0 
  MessageRequester("Error", "Can't initialize the network !", #MB_ICONSTOP) 
  End 
Else
  ConnectionID = OpenNetworkConnection("pop2.server.???", 110) 
  If ConnectionID
    ; most POP3 servers send an ID message back upon connection
    ; check if OK and respond with USER ID 
    Reply$ = ""
    *Buffer = AllocateMemory(0, 5000) 
    RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
    Reply$ = PeekS(*Buffer)
    Result$ = Reply$
    If Mid(Reply$,1,3) = "+OK"
      ; Send the USER ID now.  Some POP3 servers don't prompt for it,
      ; but expect it to be sent here.
      SendNetworkString(ConnectionID, "USER userid"+eol$)
      Reply$ = ""
      *Buffer = AllocateMemory(0, 5000)
      ; Should get back a password request if USER ID is valid 
      RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
      Reply$ = PeekS(*Buffer)
      Result$ = Result$ + Reply$ 
      If Mid(Reply$,1,3) = "+OK"
        Reply$ = ""
        *Buffer = AllocateMemory(0, 5000)
        ; Send the PASSWORD now 
        SendNetworkString(ConnectionID, "PASS password"+eol$) 
        Reply$ = ""
        *Buffer = AllocateMemory(0, 5000) 
        ; Should get back a welcome message if password is accepted
        RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
        Reply$ = PeekS(*Buffer)
        Result$ = Result$ + Reply$ 
        If Mid(Reply$,1,3) = "+OK"
          ; Send the LIST command to get number of available messages
          SendNetworkString(ConnectionID, "LIST"+eol$)
          Reply$ = ""
          *Buffer = AllocateMemory(0, 5000)
          ; Should get back a number of available messages 
          ; Interpret based on the response receive, they vary and may
          ; include info about the message ids 
          RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
          Reply$ = PeekS(*Buffer)
;          Result$ = Result$ + Reply$      ; not generally needed
          Position = FindString(Reply$, "messages", 1) 
          If Position > 0 
            Result$ = Result$ + Reply$ 
            messages = Val(Trim(Mid(Reply$, position +2 ,Len(Reply$)))) 
            MessageRequester("Info",Result$ +Chr(10)+"You have " + Str(messages) + " messages.",#MB_ICONINFORMATION)
          EndIf
        Else
          MessageRequester("Error","POP3 server didn't accept the PASSWORD.",#MB_ICONERROR)
        EndIf   
      Else
        MessageRequester("Error","POP3 server didn't accept the USER ID.",#MB_ICONERROR)
      EndIf
    EndIf 
    CloseNetworkConnection(ConnectionID) 
  Else 
    MessageRequester("Error","Unable to connect to POP3 server.",#MB_ICONERROR)
  EndIf 
EndIf 
End
;-----------------------------------------------------------------------
; End of code for POP3 - Check number of messages 
;-----------------------------------------------------------------------
It probably could use a bit more error checking, but I hope it gets you
started.

Terry

Posted: Mon Jun 23, 2003 10:41 pm
by ricardo
Hi,

Mine is a little complete app that checks my pop3 every 5 minutes and tell me if i have new mail, also let me delete unwanted mail.
It can be improved a lot because was coded in a few hours just like an excercise.
You need to have a "\Mail" folder in the same path.

Just Put your servername, username, and password in places where are required.

Code: Select all

If InitNetwork() = 0 
  MessageRequester("Error", "Can't initialize the network !", 0) 
  End 
EndIf 


Global ConnectionID.l
Global Conectado.l
Global *Buffer.l
Global eol$
Global Dir$

Dir$ = Space(500)
GetCurrentDirectory_(500,@Dir$)
eol$ = Chr(13)+Chr(10) 
*Buffer = AllocateMemory(0, 10000)

Procedure CleanMails()
If ExamineDirectory(0,Dir$ + "\Mail\" , "*.*")
  Repeat
    FileType = NextDirectoryEntry()
      If FileType
        FileName$ = DirectoryEntryName()
          If FileType = 1
            DeleteFile(Dir$ + "\Mail\" + FileName$)  
          EndIf
      EndIf
  Until FileType = 0 
EndIf
EndProcedure

Procedure ComparaMails(Mail.s, Content.s)
If ExamineDirectory(0,Dir$ + "\Mail\" , "*.*")
  Repeat
    FileType = NextDirectoryEntry()
      If FileType
        FileName$ = DirectoryEntryName()
          If FileType = 1
            If FileName$ = Mail
              If ReadFile(0,Dir$ + "\Mail\" + FileName$)
                Contenido$ = Space(Lof())
                ReadData(@Contenido$,Lof())
                CloseFile(0)
                If Contenido$ = Content
                  ProcedureReturn 1
                EndIf
              EndIf
            EndIf  
          EndIf
      EndIf
  Until FileType = 0 
  ProcedureReturn 0
EndIf
EndProcedure

Procedure ChecaMail()
SysTrayIconToolTip(0, "Connecting...")
KillTimer_(WindowID(),0)
ClearGadgetItemList(1) 
InitNetwork()
ConnectionID = OpenNetworkConnection("pop3.yourserver.com", 110)
  If ConnectionID
  Delay(5)
Repeat
Delay(10)
    Result = NetworkClientEvent(ConnectionID)
    Select Result
      Case 2
        Repeat 
        Delay(10)
        RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000) 
        Until RequestLength.l > 3
        Command$ = PeekS(*Buffer)
        Var$ = Space(RequestLength)
        PokeS(*Buffer,Var$)
        If FindString(Command$,"+OK hello from popgate",0)
          SendNetworkString(ConnectionID, "USER yourusername"+eol$)
        ElseIf FindString(Command$,"+OK password required",0)
          SendNetworkString(ConnectionID, "PASS yourpassword"+eol$)
        ElseIf FindString(Command$,"+OK maildrop ready",0)
          espacio = FindString(Command$," ",22) - 21
          Cuanto$ = Mid(Command$,21,espacio)
          Cuantos = Val(Cuanto$)
          If Cuantos > 0
            Cuanto = 1
            SendNetworkString(ConnectionID, "TOP 1 0"+eol$)
            Delay(150)
          Else
            SendNetworkString(ConnectionID, "QUIT"+eol$)
            SysTrayIconToolTip(0, "You dont have new emails")
            CloseNetworkConnection(ConnectionID)
            Final = 1
          EndIf
        ElseIf FindString(Command$,"octets",0) ;And FindString(Command$,"MIME-Version",0)
          Beep_(1000,1)
          from = FindString(Command$,"From:",0) + 6
          endfrom = FindString(Command$,eol$,from + 1) - from
          From$ = Mid(Command$,from,endfrom)
          subject = FindString(Command$,"Subject:",0) + 9
          endsubject = FindString(Command$,eol$,subject + 1) - subject
          Subject$ = Mid(Command$,subject,endsubject)
          AddGadgetItem(1, Cuanto - 1, From$ + Chr(10) + Subject$)
          File$ = From$
          For i = 1 To 47
          File$ = ReplaceString(File$,Chr(i),"",0)
          Next i
          For i = 58 To 64
          File$ = ReplaceString(File$,Chr(i),"",0)
          Next i
          For i = 122 To 255
          File$ = ReplaceString(File$,Chr(i),"",0)
          Next i
            If Len(File$) > 10
              ;File$ = Right(File$,10)
            EndIf
            File$ = File$ + ".txt"
         If Cuanto < Cuantos
          Cuanto = Cuanto + 1
          Delay(10)
          SetWindowText_(WindowID(),Str(Cuanto))
          SysTrayIconToolTip(0, "Downloading " + Str(Cuanto) + " from " + Str(Cuantos))
          SendNetworkString(ConnectionID, "TOP " + Str(Cuanto) + " 0"+eol$)
            If ComparaMails(File$,Command$ ) = 0
              Nuevo = 1
              If OpenFile(0,Dir$ + "\Email\" + File$)
                WriteString(Command$)
                CloseFile(0)
              EndIf
            EndIf
         Else
          SendNetworkString(ConnectionID, "QUIT"+eol$)
            If ComparaMails(File$,Command$ ) = 0
              Nuevo = 1
              If OpenFile(1,Dir$ + "\Mail\" + File$)
                WriteString(Command$)
                CloseFile(1)
              Else 
                Beep_(1000,3)
              EndIf
            EndIf
          If Nuevo
            SysTrayIconToolTip(0, "You have " + Str(Cuantos) + " new emails")
            HideWindow(0, 0)
          Else
            SysTrayIconToolTip(0, "You dont have new emails")
          EndIf
          CloseNetworkConnection(ConnectionID)
          Final = 1
         EndIf
        EndIf
    EndSelect
Until Final = 1
  EndIf
  SetTimer_(WindowID(),0,300000,@ChecaMail());180000
  SysTrayIconToolTip(0, "Waiting...")
EndProcedure

Procedure DeleteMail(Cual)
ConnectionID = OpenNetworkConnection("pop.yourserver.com", 110)
If ConnectionID
Delay(5)
Repeat
Delay(10)
    Result = NetworkClientEvent(ConnectionID)
    Select Result
      Case 2
        Repeat 
        Delay(10)
        RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000) 
        Until RequestLength.l > 3
        Command$ = PeekS(*Buffer)
        Var$ = Space(RequestLength)
        PokeS(*Buffer,Var$)
        If FindString(Command$,"+OK hello from popgate",0)
          SendNetworkString(ConnectionID, "USER yourusername"+eol$)
        ElseIf FindString(Command$,"+OK password required",0)
          SendNetworkString(ConnectionID, "PASS yourpassword"+eol$)
        ElseIf FindString(Command$,"marked deleted",0)
        SysTrayIconToolTip(0,Str(ii+1) + " e mail deleted")
        SendNetworkString(ConnectionID, "QUIT"+eol$)
        ElseIf FindString(Command$,"+OK maildrop ready",0)
           SendNetworkString(ConnectionID, "DELE " + Str(Cual) + " 0"+eol$)
        ElseIf FindString(Command$,"-ERR",0)
           SendNetworkString(ConnectionID, "QUIT"+eol$)
        ElseIf FindString(Command$,"+OK server signing off",0) 
          CloseNetworkConnection(ConnectionID)
            Final = 1
       EndIf
    EndSelect
Until Final = 1
Beep_(1555,10)
ProcedureReturn 1
Else
ProcedureReturn 2
EndIf
EndProcedure

#List = 1
#Check = 4
#Delete = 2
#Close = 3

If OpenWindow(0,100,150,450,230,#PB_Window_SystemMenu|#PB_Window_Invisible,"MailCheck")
  CreateGadgetList(WindowID())
  ListIconGadget(#List,10,10,430,180,"From",250,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(1, 1, "Subject", 150) 
  ButtonGadget(#Check,190,200,50,25,"Check")
  ButtonGadget(#Delete,290,200,50,25,"Delete")
  ButtonGadget(#Close,390,200,50,25,"Minimize",#PB_Button_Default)
  AddSysTrayIcon(0, WindowID(), LoadImage(0,"Windows 98-Mi PC.ico"))
  SysTrayIconToolTip(0, "PopUp Mail Checker" + Chr(13) + Chr(10) + "ok")
  CleanMails()
  SetTimer_(WindowID(),0,1,@ChecaMail())
  Repeat
    EventID=WaitWindowEvent()
    Delay(10)
    Select EventID
    
      Case #PB_EventGadget
        Select EventGadgetID()
          Case #Check
          KillTimer_(WindowID(),0)
          SetTimer_(WindowID(),0,1,@ChecaMail())
          SysTrayIconToolTip(0, "Checking...")
          Case #Close
            HideWindow(0, 1) 
          Case #Delete
          KillTimer_(WindowID(),0)
          SysTrayIconToolTip(0, "Deleting...")
            For i = 0 To CountGadgetItems(1)
            WindowEvent()
            ii = CountGadgetItems(1) - i
            WindowEvent()
              If GetGadgetItemState(1, ii) <> 0
                If DeleteMail(ii + 1) = 1
                  RemoveGadgetItem(1, ii)
                  WindowEvent()
                  Delay(100)
                EndIf 
              EndIf
            Next i
            SetTimer_(WindowID(),0,180000,@ChecaMail())
            WindowEvent()
            Beep_(1777,50)
        EndSelect
      Case #PB_Event_SysTray
            HideWindow(0, 0)
    EndSelect
    
  Until EventID=#PB_EventCloseWindow
EndIf
RemoveSysTrayIcon(0)
CloseNetworkConnection(ConnectionID)
KillTimer_(WindowID(),0)
CleanMails()
End