POP3MailServiceの例

LANSA Integrator

POP3MailServiceの例


このプログラム例では以下のステップを実行します。

1.   POP3MailServiceのロードに必要な一連の呼び出しを実行します。

2.   POP3サーバーと、指定したユーザーおよびパスワードの詳細を使用して、POP3メール・サーバーでユーザー・メールボックスを開きます(使用する環境に適した詳細を指定するには、ソース・コードを修正する必要があります)。

3.   メールボックスで使用可能なメッセージを取得します。メッセージが複数ある場合は、最初のメッセージの個々の詳細を取得してジョブログに書き込み、JSMインスタンス・ディレクトリのmailbodyフォルダーのbody1.txtファイルにメッセージの本文を保存します。

      注:示されているように、プログラム例ではサーバー・メールボックスからメッセージは削除されません。そのため、例を複数回実行する場合は、毎回同じメッセージを受け取る場合があります。

4.   メールボックスを閉じ、サービスをアンロードしてJSMサーバーとの接続を閉じます。

注:

·         この例を完全にテストするには、指定ユーザーのPOP3サーバー上のメールボックスに少なくとも1つのメール・メッセージが必要です。必要に応じて、メール・クライアント・プログラムを使用して、指定ユーザーにテスト・メールを送信します。

·         例をコンパイルして実行する前に、リモート・サーバー名、ユーザーID、パスワードの各自の値を指定する必要があります。これらはすべてソース・コードの開始付近にある定数に含まれています。

·         希望する場合は、例で使用されるディレクトリ名とファイル名を変更できます。

詳細については、例のコメントとコードを参照してください。

ILE RPGプログラム例を作成して実行する

以下のソースをコピーしてソース・ファイル・メンバーに貼り付けます。次に、ソース・コードで上記のサーバー、ユーザーID、パスワードの定数の値を修正します。

プログラムを作成するには、CRTRPGMODコマンドとCRTPGMコマンドを使用する必要があります。必ずソース・メンバーに指定したパラメータ値を使用します。

 *************************************************                  

 * 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 a 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