POP3MailService Example
This example program will perform the following steps:
1. It performs a series of calls necessary to load the POP3MailService.
2. It opens a user mailbox on the POP3 mail server, using the POP3 server and the user and password details provided (you need to modify the source code to provide details appropriate for your environment).
3. It retrieves a count of messages available in the mailbox. If there is more than one message, it retrieves various details of the first message and writes them to the joblog and then saves the message body text to file body1.txt in folder mailbody in the JSM instance directory.
Note: as supplied the example program does NOT remove the message from the server mailbox, so if you run the example more than once you may receive the same message each time.
4. It closes the mailbox, unloads the service and closes the connection to the JSM server.
Note:- To test this example fully, you need to be sure there is at least one e-mail message in the mailbox on the POP3 server for the specified user. If necessary, use your e-mail client program to send a test e-mail to the specified user.
- You must specify your own values for the remote server name, user id and password before compiling and running the example. These are all contained in constants near the beginning of the source code.
- You may change the directory and file names used in the example if you wish.
Refer to the comments and code in the example for more information.
Create and run the ILE RPG example program
Copy and paste the source provided below into a source file member. Then modify the constant values for server, user id and password as directed above and in the source code.
To create the program, you need to use the CRTRPGMOD and CRTPGM commands. Make sure that you use the parameter values specified in the source member.
*************************************************
* POP3: example in RPG ILE of using the LANSA Integrator
* POP3MailService
*
* Note: This is an example program containing only
* rudimentary exception handling
*
* To create this program you must execute the following commands,
* supplying the indicated parameter values and any others that are
* necessary in your installation:
*
* CRTRPGMOD MODULE(<modlib>/POP3)
* SRCFILE(<srclib>/<srcfil>)
*
* CRTPGM PGM(<pgmlib>/POP3)
* MODULE(<modlib>/POP3)
* BNDSRVPGM(<jsmpgmlib>/DCXS882X)
* ACTGRP(*CALLER)
*************************************************
* You MUST replace the value of these constants
* before compiling and running this example
* - for pop3_server, specify the network name or address of
* your POP3 mail server (this might not be an IBM i or
* IBM i server). However, it must be addressable from the
* system where the LANSA Integrator JSM server is running
* - for pop3_user, specify the user name used to login to the
* POP3 server
* - for pop3_password, specify the password for the pop3_user
* specified that is used to login to the POP3 server
* NB: user and password might be case sensitive, depending on
* the POP3 server you are using
d pop3_server c '<your server>'
d pop3_user c '<user id>'
d pop3_password c '<password>'
* These constants specify the location that this example program
* will save the e-mail body text - change them if required
* - because a relative path is specified, the shipped default
* will save to folder "mailbody" in the jsm instance folder
d pop3_savefil c 'body1.txt'
d pop3_savedir c 'mailbody'
* Declare variables for the JSM calls
d jsmsrv s 50a inz(*blanks)
d jsmsts s 20a inz(*blanks)
d jsmmsg s 255a inz(*blanks)
d jsmcmd s 255a inz(*blanks)
d bytelength s 10i 0 inz(*zero)
* Completion messages
d CompMsg01 c 'JSMOPEN call completed.'
d CompMsg02 c ' SERVICE_LOAD call completed.'
d CompMsg10 c ' OPEN call completed.'
d CompMsg20 c ' GET OBJECT(*MESSAGECOUNT) call +
d completed'
d CompMsg21 c 'Message count is: '
d CompMsg30 c ' GET OBJECT(*FIRSTMESSAGE) call +
d completed'
d CompMsg40 c ' GET OBJECT(*SUBJECT) call +
d completed'
d CompMsg41 c 'Subject is: '
d CompMsg50 c ' GET OBJECT(*FROMADDRESS) call +
d completed'
d CompMsg51 c 'FROM address is: '
d CompMsg60 c ' GET OBJECT(*SENTDATE) call +
d completed'
d CompMsg61 c 'Sent date is: '
d CompMsg70 c ' SAVE OBJECT(*TEXT) call +
d completed'
d CompMsg80 c ' DELETE call completed.'
d CompMsg90 c ' CLOSE call completed.'
d CompMsg98 c ' SERVICE_UNLOAD call completed.'
d CompMsg99 c 'JSMCLOSE call completed.'
* Procedure prototypes
d CheckResult pr
d crjsts const like(jsmsts)
d crjmsg const like(jsmmsg)
d SendMessage pr
d smText 512a VALUE
d smType 10a VALUE
* Prototypes for the JSM calls
/COPY QRPGLESRC,JSM_PROC.H
* Open a connection to the default JSM server
* - because the server parameter is blank, details of the default
* JSM server are obtained from the data area JSMCLTDTA on IBM i
* or from the file jsmcltdta.txt on other supported platforms)
c callp p_jsmopen(jsmsrv:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg01:'*COMP')
* Load the Pop3MailService
* - this example explicitly turns tracing on, overriding the
* settings in the manager.properties file
c eval jsmcmd = 'SERVICE_LOAD'
c + ' SERVICE(POP3MAILSERVICE) TRACE(*YES)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg02:'*COMP')
* Open the post office (login to the POP3 server)
* - you MUST alter the constant definitions at the beginning of
* this program to specify values appropriate for your mail server
* for server, user id and password
* - this assumes the mail server listens on port 110.
* If your mail server uses a different port then you will need
* to specify the PORT keyword too
c eval jsmcmd = 'OPEN'
c + ' SERVER(' + %trim(pop3_server) + ')'
c + ' USER(' + %trim(pop3_user) + ')'
c + ' PASSWORD(' + %trim(pop3_password) + ')'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg10:'*COMP')
* Get the count of available e-mail messages
c eval jsmcmd = 'GET OBJECT(*MESSAGECOUNT)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg20:'*COMP')
* Output the count to the joblog, proceed if one or more messages ...
c callp SendMessage(CompMsg21 + %trim(jsmmsg)
c :'*COMP')
c if %int(jsmmsg) > 0
* ... set the current message to the first available message ...
c eval jsmcmd = 'GET OBJECT(*FIRSTMESSAGE)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg30:'*COMP')
* ... get the message subject and write to joblog ...
c eval jsmcmd = 'GET OBJECT(*SUBJECT)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg40:'*COMP')
c callp SendMessage(CompMsg41 + %trim(jsmmsg)
c :'*COMP')
* ... get the FROM address and write to joblog ...
c eval jsmcmd = 'GET OBJECT(*FROMADDRESS)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg50:'*COMP')
c callp SendMessage(CompMsg51 + %trim(jsmmsg)
c :'*COMP')
* ... get the sent date and write to joblog ...
c eval jsmcmd = 'GET OBJECT(*SENTDATE)'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg60:'*COMP')
c callp SendMessage(CompMsg61 + %trim(jsmmsg)
c :'*COMP')
* ... save the body text to a file
* - as supplied, the body text will be saved to file "body1.txt"
* in directory "mailbody" within the jsm instance directory
c eval jsmcmd = 'SAVE OBJECT(*TEXT)'
c + ' FILE(' + %trim(pop3_savefil) + ')'
c + ' DIR(' + %trim(pop3_savedir) + ')'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg70:'*COMP')
* ... after successfully processing a message, many applications
* would wish to remove the message from the mailbox on the POP3
* server. As shipped, this example program does not do that but
* you can uncomment the following code to do so if you wish ...
c*******************eval jsmcmd = 'DELETE'
c*******************callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c*******************callp CheckResult(jsmsts:jsmmsg)
c*******************callp SendMessage(CompMsg80:'*COMP')
c endif
* Close the post office
c eval jsmcmd = 'CLOSE'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg90:'*COMP')
* Unload the Pop3MailService
c eval jsmcmd = 'SERVICE_UNLOAD'
c callp p_jsmcmd(jsmcmd:jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg98:'*COMP')
* Close the connection to the JSM server and finish
c callp p_jsmclose(jsmsts:jsmmsg)
c callp CheckResult(jsmsts:jsmmsg)
c callp SendMessage(CompMsg99:'*COMP')
c eval *inlr = *on
c return
*************************************************
* Procedure to check the result of a Java Service Manager call
*************************************************
p CheckResult b
d CheckResult pi
d crjsts const like(jsmsts)
d crjmsg const like(jsmmsg)
d crText s 512a
d crMsg1 c const('JSM Status : ')
d crMsg2 c const('JSM Message: ')
d crMsg3 c const('JSM Service error has +
d occurred')
c if crjsts <> 'OK'
c eval crText = crMsg1 + crjsts
c callp SendMessage(crText:'*DIAG')
c eval crText = crMsg2 + crjmsg
c callp SendMessage(crText:'*DIAG')
c callp SendMessage(crMsg3:'*ESCAPE')
c endif
p CheckResult e
*************************************************
* Procedure to send a program message
*************************************************
p SendMessage b
d SendMessage pi
d smText 512a VALUE
d smMsgT 10a VALUE
d smMsgI s 7a inz('CPF9897')
d smMsgF s 20a inz('QCPFMSG *LIBL ')
d smDtaL s 10i 0 inz(%size(smText))
d smStkE s 10a inz('*')
d smStkC s 10i 0 inz(1)
d smMsgK s 4a
d smErrC s 10i 0 inz(0)
c if smMsgT = '*ESCAPE'
c eval smMsgI = 'CPF9898'
c endif
c call 'QMHSNDPM'
c parm smMsgI
c parm smMsgF
c parm smText
c parm smDtaL
c parm smMsgT
c parm smStkE
c parm smStkC
c parm smMsgK
c parm smErrC
p e
intengbr_RPG_STMP