@GeoTrail
I have played with this a bit more. See the updated code following.
I still have problems with the POP3_GetMsgBody(I, @myCallBack)
command giving Invalid Page Faults
In this version the buffered
POP3_BufferMsgBody(I, *buffer, @messageCallBack())
is working. But the #POP3MSG_DOWNLOADPROGRESS message seems
to kill getting the message properly for me.
Code: Select all
Global Err$
Global Msg.s
Global password.s
Global pop_server.s
Global Temp$
Global Tmp.s
Global username.s
Global *buffer
Global Error
Global inTransaction.b : inTransaction = 0
Global LogonStatus.b : LogonStatus = #FALSE
Global MsgLen.l
Global msgSize.l
Global I.w : I = 0
*buffer = AllocateMemory(10000)
; Replace the following three items with your POP3 server information.
pop_server.s = "pop.secret.net" ; Server's address
username.s = "secret" ; User's ID
password.s = "secret" ; User's pasword
Procedure FixUpMessage()
; --- fix up message ----
; I only want to see the text portion of the message, no HTML, etc.
Temp$ = PeekS(*buffer)
Pos = FindString(Temp$,"quoted-printable",1)
; --- strip message type description ---
Temp$ = Trim(Mid(Temp$,Pos+20,Len(Temp$)))
; --- trim message text trailer from the first ?? lines ---
Pos = FindString(Temp$,"------=_",1)
If pos
Temp$ = Mid(Temp$,1,Pos-1)
EndIf
; --- end of message fix up ---
EndProcedure
Procedure TranslateError()
Error = POP3_GetLastError()
Select Error
Case #POP3_ERR_UNINITIALISED; The library is uninitialised - call POP3_Init()
Case #POP3_ERR_MEMORY
Err$ = "Unable To allocate sufficient memory For task."
Case #POP3_ERR_NETWORK
Err$ = "Unable to initialise the network."
Case #POP3_ERR_CONNECTION
Err$ = "Unable to connect to POP3 server."
Case #POP3_ERR_LOGINFAILED
Err$ = "Login failed."
Case #POP3_ERR_NOSUCHMSG
Err$ = "Message specified does not exist."
Case #POP3_ERR_ALREADYCONNECTED
Err$ = "Trying to connect to server while already connected."
Case #POP3_ERR_TIMEDOUT
Err$ = "No response received in the specified time."
Case #POP3_ERR_FILEERROR
Err$ = "Unable to open/write to file."
Case #POP3_ERR_THREAD
Err$ = "Failed to create new thread."
Case #POP3_ERR_BUSY
Err$ = "The library is busy handling a prior request."
Case #POP3_ERR_LOGOFF
Err$ = "Logoff command failed."
Default
Err$ = "Unrecognized error condition"
EndSelect
Msg.s + Chr(10) + Err$
SetGadgetText(4,Msg.s)
; The following MessageRequester isn't really needed because the
; program's display is showing these errors too, but that display
; may disappear before the user can read it.
MessageRequester("POP3","An error has occurred." + Chr(10) + Chr(10) + "Error code: " + Str(Error) + Chr(10) + Err$,#MB_ICONERROR)
ActivateGadget(4) ; Create a window event to resume loop
EndProcedure
Procedure myCallback(messageLine$)
Msg.s + messageLine$ + Chr(10)
EndProcedure
Procedure messageCallBack(Message, param1, param2)
Select Message
Case #POP3MSG_DOWNLOADCOMPLETE
; Message download is complete, send the message text to your window/text
inTransaction = 0
MsgLen = param1
Case #POP3MSG_ERROR
; check the last error and presume that the connection is lost
TranslateError()
inTransaction = 0
Case #POP3MSG_DOWNLOADPROGRESS
; Show progress percentage (param1) in a progress bar gadget
; The 100% DownloadProgress doesn't seem to ever be issued by library
; When active, I don't get the entire message as requested ????
; SetGadgetState(2, param1)
EndSelect
EndProcedure
Procedure GetMsgText()
If POP3_GetMsgBody(I, @myCallBack)
; Normal end
Else
MessageRequester("POP3","Your POP3_GetMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
EndIf
EndProcedure
Procedure GetMsgBody()
If POP3_BufferMsgBody(I, *buffer, @messageCallBack())
; Normal call
Else
MessageRequester("POP3","Your POP3_BufferMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
EndIf
; While inTransaction = 1
; Wend
Repeat
Delay(1000)
Until inTransacton = 0
; PercentComplete.f = (MsgLen / msgSize) * 100
; SetGadgetState(2, PercentComplete)
SetGadgetState(2, 100) ; Show complete on progress bar
FixUpMessage()
Msg.s + Temp$
; MessageRequester("Debug","MsgLen is: " + Str(MsgLen),0)
EndProcedure
Procedure GetMsgLines()
; Get the 1st 10 lines of a message
MaxLinesToGet.l = 17 ; Allow for header and 10 lines to be received
If POP3_BufferMsgBodyLines(I,*buffer, MaxLinesToGet)
; Successfully downloaded the 10 lines of the message
SetGadgetState(2, 100) ; Show complete on progress bar
FixUpMessage()
Msg.s + Temp$
Else
MessageRequester("POP3","Your POP3_BufferMsgHeader request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
EndIf
EndProcedure
; ----------------------------- Main code --------------------------
If OpenWindow(0, 0, 0, 300, 300,#PB_Window_SystemMenu, "POP3")
AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
If CreateGadgetList(WindowID())
ProgressBarGadget(2, 10, 285, 280, 10, 0, 100, #PB_ProgressBar_Smooth)
ButtonGadget(3,10,260,280,20,"Next")
EditorGadget(4,10,10,280,250)
; Initialize the POP3 LIbrary
Msg.s = "Attempting to initiate the POP3_Library functions..."
SetGadgetText(4,Msg.s)
If POP3_Init()
Msg.s + Chr(10) + "POP3_Library successfully initiated..."
POP3lib_Version.s = POP3_GetLibraryVersionInfo()
Msg.s + Chr(10) + "You are using POP3lib version " + POP3lib_Version
Msg.s + Chr(10) + "Attempting to connect to " + pop_server.s
SetGadgetText(4,Msg.s)
; Try to connect to the server
If POP3_Connect(pop_server.s, 110)
Msg.s + Chr(10) + "Connected successfully to " + pop_server.s
Msg.s + Chr(10) + "Attempting to log on to server..."
SetGadgetText(4,Msg.s)
; Logon to the server
If POP3_Logon(username.s, password.s)
LogonStatus = #TRUE
Msg.s + Chr(10) + "Logged on to server successfully..." + Chr(10)
SetGadgetText(4,Msg.s)
; Check for available messages
Messages = POP3_GetMsgCount()
If Messages = 1
Msg.s + Chr(10) + "There is " + Str(Messages) + " message waiting for you." + Chr(10)
Else
Msg.s + Chr(10) + "There are " + Str(Messages) + " messages waiting for you." + Chr(10)
EndIf
SetGadgetText(4,Msg.s)
Else ; Couldn't logon using POP3_Logon
TranslateError()
Msg.s + Chr(10) + Err$
SetGadgetText(4,Msg.s)
Quit = #TRUE
EndIf
Else ; Couldn't connect using POP3_Connect
TranslateError()
Msg.s + Chr(10) + Err$
SetGadgetText(4,Msg.s)
Quit = #TRUE
EndIf
Else ; Couldn't initiate the POP3_Library
TranslateError()
Msg.s + Chr(10) + Err$
SetGadgetText(4,Msg.s)
Quit = #TRUE
EndIf
; Processing loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow ; Window close box clicked
Quit = #TRUE
Case #PB_Event_Menu
Select EventMenuID() ; See which menu has been selected
Case #PB_Shortcut_Escape ; ESC key pressed
Quit = #TRUE
EndSelect ; End #PB_Event_Menu selection
Case #PB_Event_Gadget
Select EventGadgetID()
Case 3 ; Next button pushed
SetGadgetState(2,0)
SetGadgetText(4,"")
If Messages
; Loop through the messages
I + 1
If I > Messages
ResizeGadget(4,10,10,280,40)
SetGadgetText(4,"There are no more messages")
Else
MsgLeft.l = Messages - I
If MsgLeft = 1
Msg.s = "There is " + Str(MsgLeft) + " remaining message waiting for you." + Chr(10)
Else
Msg.s = "There are " + Str(MsgLeft) + " remaining messages waiting for you." + Chr(10)
EndIf
SetGadgetState(2, 0)
msgSize = POP3_GetMsgSize(I)
ReAllocateMemory(*buffer, msgSize) ; Allocate memory same size as message pluss 1 K extra.
Msg.s + Chr(10) + "Filesize of the message " + Str(I)+ " is: " + Str(msgSize)+" bytes." + Chr(10)
If POP3_ParseMsgHeader(I)
Message_from.s = POP3_HeaderFrom()
Message_date.s = POP3_HeaderDate()
Message_subject.s = POP3_HeaderSubject()
Msg.s + Chr(10) + "Message was sent at " + Message_date + " by " + Message_from
Msg.s + Chr(10) + "Message subject: " + Message_subject + Chr(10)
Msg.s + Chr(10) + "Message content: " + Chr(10)
; ------------------------------------------------------
; Getting the message text - choose one of three methods
; (1) Get the messge text using POP3_GetMsgBody - Failure
; Causes an Invalid Page Fault
;GetMsgText()
; (2) Get the message using POP3_BufferMsgBody - Successful
GetMsgBody()
; (3) Get the first ten lines of the message - Successful
; Limited to the requested number of lines per message
;GetMsgLines()
Else
Msg.s + Chr(10) + "Error parsing message headers."
EndIf
SetGadgetText(4,Msg.s)
EndIf
Else
; No messages available
EndIf
EndSelect ; End #PB_Event_Gadget selection
EndSelect ; End WaitWindowEvent selection
Until Quit ; End processing loop
; If we are logged in, we should logoff before disconnecting
If LogonStatus
; Logoff the server
If POP3_Logoff()
; Success
Else
TranslateError()
Msg.s + Chr(10) + Err$
SetGadgetText(4,Msg.s)
EndIf
EndIf
; Disconnect from the POP server.
POP3_Disconnect() ; No errors are generated by this function
EndIf ; End CreateGadgetList
EndIf ; End Open Window
End
Regards,
Terry