602SQL Documentation Index  

Example: Receive e-mail

Functions that work with e-mail will not raise an exception and cannot be processed by a handle.

PROCEDURE `RecieveMail`();
BEGIN
   DECLARE err,i  INT;
   DECLARE box    INT;
   DECLARE id_stack,lsize INT;
   DECLARE fname  CHAR(255);
   SET err = InitWBMail('sql602test','');
   IF err = 0 THEN BEGIN
     SET err = MailOpenInBox(box);
     IF err = 0 THEN BEGIN
       CALL MailBoxLoad(box,3);
       FOR Stack AS CURSOR FOR
         SELECT * FROM _Sysext._Inboxmsgs WHERE Subject='Data'
       DO 
       BEGIN
          SET id_stack = Stack.id;
          CALL MailBoxGetMsg(box,id_stack);
          SET lsize = Stack.size;
          IF lsize > 0 THEN
          // ... process the letter text
          END IF;
          CALL MailBoxGetFilInfo(box,id_stack);            
          SET i = 0;
          FOR StackFil AS CURSOR FOR
            SELECT * FROM _Sysext._Inboxfiles WHERE id=id_stack
          DO 
          BEGIN
            SET fname = StackFil.name;
            WHILE POSITION('\' IN fname)>0 DO 
               CALL StrDelete(fname,1,POSITION('\' IN fname));  
            END WHILE;
            SET fname = 'D:\tempdir\wbdata\'||fname;
            SET Err = MailBoxSaveFileAs(box,id_stack,i,'',fname);
            IF Err > 0 THEN
              CALL Log_write('An error occurred while saving the file '+int2str(Err));
            END IF;
            SET i = i+1;
          END;
          END FOR;
          CALL MailBoxDeleteMsg(box,Stack.id,FALSE);
       END;
       END FOR;
       CALL MailCloseInBox(box);
     END; ELSE 
       CALL Log_write('An error occurred while opening the mailbox '+int2str(Err)); 
     END IF;
     CALL CloseWBMail();
   END; ELSE 
     CALL Log_write('Initialization error '+int2str(Err)); 
   END IF;
END