POP3MailService Example
This function retrieves all the email messages from the mail server. Details of each message are displayed to the user and, in addition, the body text and any attachments are saved to appropriate directories under the JSM server instance directory.
* Uses Integrator Services: POP3MailService
* This forms retrieves messages from a mail server and then deletes them from the mail server.
*
* The following fields must be defined in your data dictionary to support this function:
* jsmserver alpha 20
* jsmpsswrd alpha 10
* jsmuserid alpha 10
* jsmfrom alpha 150
* jsmemail char 250
* jsmstring string 1000
* jsmsts alpha 20
* jsmmsg alpha 255
* jsmhdle char 500
* jsmcmd alpha 4
* Beginning of RDML commands **********
function options(*DIRECT)
begin_com role(*EXTENDS #PRIM_FORM) clientheight(414) clientwidth(585) height(448) left(471) top(168) width(593)
define_com class(#PRIM_STBR) name(#STBR_1) displayposition(6) height(24) left(0) messageposition(1) parent(#COM_OWNER) tabposition(6) tabstop(False) top(390) width(585)
define_com class(#JSMSERVER.Visual) name(#jsmserver) caption('Server') displayposition(1) height(19) labeltype(Caption) left(184) marginleft(80) parent(#COM_OWNER) tabposition(1) top(8) usepicklist(False) width(170)
define_com class(#JSMUSERID.Visual) name(#jsmuser) caption('User') displayposition(2) labeltype(Caption) left(184) marginleft(80) parent(#COM_OWNER) tabposition(2) top(32) width(170)
define_com class(#JSMpsswrd.Visual) name(#jsmpassword) caption('Password') displayposition(3) labeltype(Caption) left(184) marginleft(80) parent(#COM_OWNER) tabposition(3) top(56) width(170)
define_com class(#PRIM_PHBN) name(#Get) caption('Get Messages') displayposition(4) left(24) parent(#COM_OWNER) tabposition(4) top(360) width(100)
define_com class(#PRIM_PHBN) name(#Reset) caption('Reset') displayposition(5) left(136) parent(#COM_OWNER) tabposition(5) top(360) width(100)
define_com class(#PRIM_LTVW) name(#message) componentversion(2) displayposition(7) fullrowselect(True) height(265) left(8) parent(#COM_OWNER) showsortarrow(True) tabposition(7) top(88) width(569)
define_com class(#PRIM_LVCL) name(#From) caption('From') captiontype(Caption) displayposition(1) parent(#message) source(#JSMFROM) width(30)
define_com class(#PRIM_LVCL) name(#subject) caption('Subject') captiontype(Caption) displayposition(2) parent(#message) source(#JSMEMAIL) width(30)
define_com class(#PRIM_LVCL) name(#received) caption('Message') captiontype(Caption) displayposition(3) parent(#message) source(#JSMSTRING) width(40) widthtype(Remainder)
def_list name(#fromlst) fields(#jsmfrom) type(*working)
def_list name(#textlst) fields(#jsmstring) type(*working)
evtroutine handling(#com_owner.Initialize)
set com(#com_owner) caption(*component_desc)
#jsmhdle := *default
#jsmserver := '99.99.99.99'
#jsmuser := 'user'
#jsmpassword := 'password'
* Start JSM Server on IBM i
* use builtin(jsmx_open) with_args('ISERIES01:9990') to_get(#jsmsts #jsmmsg #jsmhdle)
* Start local JSM server
use builtin(jsmx_open) with_args('localhost:9980') to_get(#jsmsts #jsmmsg #jsmhdle)
#com_owner.check( #jsmsts #jsmmsg )
* Load the service
#jsmcmd := 'Service_Load Service(POP3MailService) trace(*yes)'
use builtin(jsmx_command) with_args(#jsmhdle #jsmcmd) to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
endroutine
mthroutine name(GetEmail)
if (*Not #jsmhdle.isnull)
clr_list named(#message)
* open the post office
#jsmcmd := 'open server(' + #jsmserver + ') user(' + #jsmuser + ') password(' + #jsmpassword + ')'
use builtin(jsmx_command) with_args(#jsmhdle #jsmcmd) to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
* Get count of messages
use builtin(jsmx_command) with_args(#jsmhdle 'get object(*messagecount)') to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
message msgtxt(#jsmsts + ' : ' + #jsmmsg + ' messages in mail box')
* loop through all the messages
begin_loop
* Get message
use builtin(jsmx_command) with_args(#jsmhdle 'get object(*nextmessage)') to_get(#jsmsts #jsmmsg)
if (#jsmsts = NOMAIL)
leave
endif
* Get subject
use builtin(jsmx_command) with_args(#jsmhdle 'get object(*subject)') to_get(#jsmsts #jsmmsg)
#jsmemail := #jsmmsg
* Get from addresses
clr_list named(#fromlst)
use builtin(jsmx_command) with_args(#jsmhdle 'get object(*fromaddress)') to_get(#jsmsts #jsmmsg #fromlst)
#com_owner.check( #jsmsts #jsmmsg )
get_entry number(1) from_list(#fromlst)
* Read text
clr_list named(#textlst)
use builtin(jsmx_command) with_args(#jsmhdle 'read object(*text)') to_get(#jsmsts #jsmmsg #textlst)
#com_owner.check( #jsmsts #jsmmsg )
selectlist named(#textlst)
add_entry to_list(#message)
#jsmemail #jsmfrom := *blank
endselect
* save attachments
use builtin(jsmx_command) with_args(#jsmhdle 'save object(*attachments) dir(emailattch)') to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
* text
use builtin(jsmx_command) with_args(#jsmhdle 'save object(*text) file(body1.txt) dir(emailbody) ') to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
* Delete message
use builtin(jsmx_command) with_args(#jsmhdle 'delete') to_get(#jsmsts #jsmmsg)
* Get next message
end_loop
* Close post office
use builtin(jsmx_command) with_args(#jsmhdle 'close') to_get(#jsmsts #jsmmsg #fromlst)
#com_owner.check( #jsmsts #jsmmsg )
endif
endroutine
* send the email
evtroutine handling(#Get.Click)
#com_owner.GetEmail
endroutine
* reset the email variables
evtroutine handling(#Reset.Click)
clr_list named(#message)
endroutine
* check the JSM return status
mthroutine name(check)
define_map for(*input) class(#jsmsts) name(#i_status)
define_map for(*input) class(#jsmmsg) name(#i_message)
message msgtxt(#i_status + ' : ' + #i_message)
endroutine
evtroutine handling(#com_owner.closing) options(*noclearmessages *noclearerrors)
* Unload service
use builtin(jsmx_command) with_args(#jsmhdle 'Service_Unload') to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
* Close the thread
use builtin(jsmx_close) with_args(#jsmhdle) to_get(#jsmsts #jsmmsg)
#com_owner.check( #jsmsts #jsmmsg )
endroutine
end_com