본문 바로가기

Ⅰ. ERP/1. ABAP

[ABAP] 메일보내기

728x90

ABAP 개발을 하다보면 메일을 보내야할 경우가 몇 있는데 아래 소스를 넣고 BATCH를 통해 메일을 전송하면된다.

  DATA lv_data001(255).
  DATA lv_data002(255).
  DATA lv_email TYPE ad_smtpadr.
  DATA: lv_sender TYPE ad_smtpadr,
        sender    TYPE REF TO if_sender_bcs.

  TRY.

*     -------- create persistent send request ------------------------
      send_request = cl_bcs=>create_persistent( ).

*     -------- create and set document with attachment ---------------
*     create document object from internal table with text
*---------------------------------------------------------------------*
* mail-title
*---------------------------------------------------------------------*
      CONCATENATE  '###제목###' INTO main_text.                "#EC NOTEXT

*---------------------------------------------------------------------*
* mail-body
*---------------------------------------------------------------------*
      CONCATENATE  '###메일내용### '  INTO lv_data001.

*
*      CONCATENATE  '<u><b>'lv_data001'</b></u> <br><br>' INTO lv_data002.
*
*
*      APPEND '<html>'   TO main_text_body.
*      APPEND '<body>'   TO main_text_body.
*      APPEND lv_data002 TO main_text_body.
      APPEND lv_data001 TO main_text_body.
*      APPEND '</body>'  TO main_text_body.
*      APPEND '</html>'  TO main_text_body.

      document = cl_document_bcs=>create_document(
        i_type    = 'RAW'
        i_text    = main_text_body
        i_subject = main_text ).                            "#EC NOTEXT

*     add the spread sheet as attachment to document object
      document->add_attachment(
*        i_attachment_type    = 'xls'                        "#EC NOTEXT
        i_attachment_type    = 'zip'                        "#EC NOTEXT
        i_attachment_subject = '[#첨부파일제목#]'                     "#EC NOTEXT
        i_attachment_size    = size
        i_att_content_hex    = binary_content[] ).

*     add document object to send request
      send_request->set_document( document ).
* 보낸사람

      sender = cl_cam_address_bcs=>create_internet_address( lv_sender ).
      send_request->set_sender( sender ).
*     --------- add recipient (e-mail address) -----------------------
*     create recipient object
      CLEAR recipient.
*--- BSH 수정  23.02.2024 09:44:24
      LOOP AT gt_zsdt0160 INTO gs_zsdt0160 WHERE email IS NOT INITIAL AND seqno <> '99'.
        CLEAR lv_email.
        lv_email = gs_zsdt0160-email.
        recipient =
        cl_cam_address_bcs=>create_internet_address( lv_email ).

*     add recipient object to send request
        send_request->add_recipient( recipient ).
      ENDLOOP.

*      recipient = cl_cam_address_bcs=>create_internet_address( p_mailto ).
      ">>>
*     add recipient object to send request
*      send_request->add_recipient( recipient ).

      CHECK  recipient IS NOT INITIAL.
*     ---------- send document ---------------------------------------
      sent_to_all = send_request->send( i_with_error_screen = 'X' ).

      COMMIT WORK.


      SUBMIT rsconn01 WITH mode = 'INT'
      WITH output = 'X'
      AND RETURN.


      IF sent_to_all IS INITIAL.

        MESSAGE s023(so) WITH lv_email.
*        MESSAGE i500(sbcoms) WITH lv_email.
      ELSE.
        MESSAGE s022(so).
      ENDIF.

*   ------------ exception handling ----------------------------------
*   replace this rudimentary exception handling with your own one !!!
    CATCH cx_bcs INTO bcs_exception.
      MESSAGE i865(so) WITH bcs_exception->error_type.
  ENDTRY.
728x90