SMTPMailService Example
This is a simple function to compose and send an email. This function does not support adding attachments to the email.
* Uses Integrator Services: SMTPMAILSERVICE
* Loads SMTPMailService service.
* Allows message detail to be entered
* and sends an email.
* 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(#TXT) TYPE(*CHAR) LENGTH(78)
DEFINE FIELD(#TXT1) REFFLD(#TXT) COLHDG('Message')
DEFINE FIELD(#TXT2) REFFLD(#TXT) LABEL(' ')
DEFINE FIELD(#TXT3) REFFLD(#TXT) LABEL(' ')
DEFINE FIELD(#TXT4) REFFLD(#TXT) LABEL(' ')
DEF_LIST NAME(#MESSAGE) FIELDS(#TXT) TYPE(*WORKING)
DEFINE FIELD(#TOADDRESS) TYPE(*CHAR) LENGTH(50) LABEL(' To')
DEFINE FIELD(#CCADDRESS) REFFLD(#TOADDRESS)
DEFINE FIELD(#C1ADDRESS) REFFLD(#TOADDRESS) LABEL(' CC')
DEFINE FIELD(#C2ADDRESS) REFFLD(#TOADDRESS) LABEL(' ')
DEFINE FIELD(#SUBJECT) REFFLD(#TOADDRESS) LABEL(' Subject')
DEF_LIST NAME(#CCLIST) FIELDS(#CCADDRESS) COUNTER(#LISTCOUNT) TYPE(*WORKING)
* 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(SMTPMAILSERVICE)') TO_GET(#JSMSTS #JSMMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)
MESSAGE MSGTXT('SMTPMailService loaded')
BEGIN_LOOP
* Request information
REQUEST FIELDS((#TOADDRESS *L003 *P002) (#C1ADDRESS *L004 *P002) (#C2ADDRESS *L005 *P002) (#SUBJECT *L6 *P2) (#TXT1 *L007 *P002 *COLHDG) (#TXT2 *L009 *P002 *NOID) (#TXT3 *L010 *P002 *NOID) (#TXT4 *L011 *P002 *NOID)) EXIT_KEY(*NO) MENU_KEY(*YES *NEXT) PROMPT_KEY(*NO)
IF_KEY WAS(*MENU)
* Close service
EXECUTE SUBROUTINE(DISCONNECT)
MENU
ENDIF
EXECUTE SUBROUTINE(SENDEMAIL)
END_LOOP
SUBROUTINE NAME(SENDEMAIL)
* Set from and to addresses
USE BUILTIN(TCONCAT) WITH_ARGS('SET TO(' #TOADDRESS ') FROM([email protected])') TO_GET(#JSMCMD)
USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)
* Set CC addresses
IF COND('#C1ADDRESS *NE *BLANK')
CHANGE FIELD(#CCADDRESS) TO(#C1ADDRESS)
ADD_ENTRY TO_LIST(#CCLIST)
ENDIF
IF COND('#C2ADDRESS *NE *BLANK')
CHANGE FIELD(#CCADDRESS) TO(#C2ADDRESS)
ADD_ENTRY TO_LIST(#CCLIST)
ENDIF
IF COND('#LISTCOUNT *NE 0')
USE BUILTIN(JSM_COMMAND) WITH_ARGS('SET CC(*LIST) SERVICE_LIST(CCADDRESS)') TO_GET(#JSMSTS #JSMMSG #CCLIST)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)
ENDIF
* Create message body
CHANGE FIELD(#TXT) TO(#TXT1)
ADD_ENTRY TO_LIST(#MESSAGE)
CHANGE FIELD(#TXT) TO(#TXT2)
ADD_ENTRY TO_LIST(#MESSAGE)
CHANGE FIELD(#TXT) TO(#TXT3)
ADD_ENTRY TO_LIST(#MESSAGE)
CHANGE FIELD(#TXT) TO(#TXT4)
ADD_ENTRY TO_LIST(#MESSAGE)
* Send mail
USE BUILTIN(TCONCAT) WITH_ARGS('SEND SUBJECT(' #SUBJECT ') SERVICE_LIST(TXT)') TO_GET(#JSMCMD)
USE BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS #JSMMSG #MESSAGE)
EXECUTE SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)
MESSAGE MSGTXT('Email has been sent')
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 **********