POP3MailService

This function retrieves any mail messages for the nominated user from the mail server.  For each message:

1.  Any attachments are saved to the JSM instance directory

2.  The body text is saved to a unique text file on the JSM instance directory.

3.  Details of each email are also display to the user.

4.  After processing each email is deleted.

* Uses Integrator Services: POP3MAILSERVICE

 

* Loads POP3MailService service.

* Opens the post office and reads all messages.

* For each message: get and display the from addresses,

* subject and content type,

* and save body and any attachments as files.

* Finally deletes the message.

 

* Beginning of RDML commands **********

 

FUNCTION OPTIONS(*DIRECT)

 

DEFINE FIELD(#JSMSTS) TYPE(*CHAR) LENGTH(20)

DEFINE FIELD(#JSMMSG) TYPE(*CHAR) LENGTH(255)

DEFINE FIELD(#JSMCMD) TYPE(*CHAR) LENGTH(255)

 

DEFINE FIELD(#JSMSERVER) TYPE(*char) LENGTH(20)

DEFINE FIELD(#JSMUSER) TYPE(*CHAR) LENGTH(010) LABEL('User') INPUT_ATR(LC FE)

DEFINE FIELD(#JSMPSSWRD) TYPE(*char) LENGTH(10)

 

DEFINE FIELD(#EMAILCNT) TYPE(*DEC) LENGTH(2) DECIMALS(0)

DEFINE FIELD(#EMAILNO) TYPE(*CHAR) LENGTH(2) COLHDG('')

DEFINE FIELD(#ADDRESS) TYPE(*CHAR) LENGTH(35) COLHDG('Address')

DEFINE FIELD(#SUBJECT) TYPE(*CHAR) LENGTH(35) COLHDG('Subject')

 

DEF_LIST NAME(#FROMLST) FIELDS(#ADDRESS) TYPE(*WORKING)

DEF_LIST NAME(#MESSAGES) FIELDS(#EMAILNO #SUBJECT #ADDRESS)

 

* ==========================================

 

* set default values for screen

CHANGE FIELD(#STD_INSTR) TO('''Set your appropriate values then press ENTER to get email messages. ''')

CHANGE FIELD(#JSMSERVER) TO('''99.99.99.99''')

CHANGE FIELD(#JSMUSER) TO('''user''')

CHANGE FIELD(#JSMPSSWRD) TO('''password''')

 

* Open service

USE BUILTIN(JSM_OPEN) TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

* Load service

USE BUILTIN(JSM_COMMAND) WITH_ARGS('SERVICE_LOAD SERVICE(POP3MAILSERVICE)') TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

MESSAGE MSGTXT('POP3MailService loaded')

 

BEGIN_LOOP

 

* Request information

REQUEST FIELDS((#STD_INSTR *L003 *P002 *OUTPUT *NOID) (#JSMSERVER *L005 *P022) (#JSMUSER *L007 *P022) (#JSMPSSWRD *L009 *P022)) BROWSELIST(#MESSAGES) EXIT_KEY(*NO) MENU_KEY(*YES *NEXT) PROMPT_KEY(*NO)

 

IF_KEY WAS(*MENU)

 

* Close service

EXECUTE SUBROUTINE(DISCONNECT)

MENU

 

ENDIF

 

EXECUTE SUBROUTINE(GETMESSAGE)

 

END_LOOP

 

SUBROUTINE NAME(GETMESSAGE)

 

CLR_LIST NAMED(#MESSAGES)

CHANGE FIELD(#EMAILCNT) TO(0)

 

* Open post office

USE BUILTIN(TCONCAT) WITH_ARGS('OPEN SERVER(' #JSMSERVER ') USER(' #JSMUSER ') PASSWORD(') TO_GET(#JSMCMD)

USE BUILTIN(TCONCAT) WITH_ARGS(#JSMCMD #JSMPSSWRD ')') TO_GET(#JSMCMD)

USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

* Get count of messages

USE BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*MESSAGECOUNT)') TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

USE BUILTIN(BCONCAT) WITH_ARGS(#JSMMSG 'messages in mail box') TO_GET(#SUBJECT)

ADD_ENTRY TO_LIST(#MESSAGES)

 

* loop through all the messages

BEGIN_LOOP

 

* Get message

USE BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*NEXTMESSAGE)') TO_GET(#JSMSTS #JSMMSG)

 

IF COND('#JSMSTS *EQ NOMAIL')

LEAVE

ENDIF

 

CHANGE FIELD(#EMAILCNT) TO('#EMAILCNT + 1')

SUBSTRING FIELD(#EMAILCNT) INTO_FIELD(#EMAILNO)

 

* Get subject

USE BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*SUBJECT)') TO_GET(#JSMSTS #JSMMSG)

CHANGE FIELD(#SUBJECT) TO(#JSMMSG)

 

* Get from addresses

USE BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*FROMADDRESS) SERVICE_LIST(ADDRESS)') TO_GET(#JSMSTS #JSMMSG #FROMLST)

 

SELECTLIST NAMED(#FROMLST)

ADD_ENTRY TO_LIST(#MESSAGES)

CHANGE FIELD(#SUBJECT) TO(*BLANK)

ENDSELECT

 

 

* Save text

USE BUILTIN(TCONCAT) WITH_ARGS('SAVE OBJECT(*TEXT) FILE(message' #EMAILNO '.txt)') TO_GET(#JSMCMD)

USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG)

USE BUILTIN(TCONCAT) WITH_ARGS('Email' #EMAILNO ' saved to file message' #EMAILNO '.txt') TO_GET(#STD_TEXTL)

MESSAGE MSGID(DCM9899) MSGF(DC@M01) MSGDTA(#STD_TEXTL)

 

* Save attachments

CHANGE FIELD(#JSMCMD) TO('SAVE OBJECT(*ATTACHMENTS)')

USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG)

CHANGE FIELD(#STD_TEXTL) TO('''Attachments saved''')

MESSAGE MSGID(DCM9899) MSGF(DC@M01) MSGDTA(#STD_TEXTL)

 

* read text

CHANGE FIELD(#JSMCMD) TO('READ OBJECT(*TEXT) SERVICE_LIST(ADDRESS) TRUNCATE(*BOTH)')

USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG #FROMLST)

 

SELECTLIST NAMED(#FROMLST)

ADD_ENTRY TO_LIST(#MESSAGES)

CHANGE FIELD(#SUBJECT) TO(*BLANK)

 

ENDSELECT

 

* Delete message

USE BUILTIN(JSM_COMMAND) WITH_ARGS('DELETE') TO_GET(#JSMSTS #JSMMSG)

 

* Get next message

 

END_LOOP

 

* Close post office

USE BUILTIN(JSM_COMMAND) WITH_ARGS('CLOSE') TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

ENDROUTINE

 

SUBROUTINE NAME(DISCONNECT)

 

* Unload service

USE BUILTIN(JSM_COMMAND) WITH_ARGS('SERVICE_UNLOAD') TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

 

* Close service

USE BUILTIN(JSM_CLOSE) TO_GET(#JSMSTS #JSMMSG)

EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)

 

ENDROUTINE

 

* Check the JSM return status

SUBROUTINE NAME(CHECK) PARMS((#JSMSTS *RECEIVED) (#JSMMSG *RECEIVED))

 

IF COND('#JSMSTS *NE OK')

 

USE BUILTIN(TCONCAT) WITH_ARGS(#JSMSTS ' : ' #JSMMSG) TO_GET(#STD_TEXTL)

MENU MSGID(DCM9899) MSGF(DC@M01) MSGDTA(#STD_TEXTL)

 

ENDIF

 

ENDROUTINE

 

* End of RDML commands **********