cl_abap_zip & cl_bcs example

REPORT  ztest.

TYPE-POOLS: sscr.
TABLES: adr6.

*--------------------------------------------------------------------*
*SELECTION SCREEN
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.
PARAMETERS p_indi(1) NO-DISPLAY.
SELECT-OPTIONS s_addr FOR adr6-smtp_addr NO INTERVALS.
SELECTION-SCREEN END OF BLOCK blk.

*--------------------------------------------------------------------*
*INTERNAL TABLE
*--------------------------------------------------------------------*
DATA: itab_list TYPE STANDARD TABLE OF abaplist,
      itab_html TYPE STANDARD TABLE OF w3html.

*--------------------------------------------------------------------*
*AT SELECTION SCREEN OUTPUT
*--------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  DATA: l_restrict TYPE sscr_restrict,
        l_option   TYPE sscr_opt_list,
        l_ass      TYPE sscr_ass.
  l_option-name = 'KEY1'.
  l_option-options-eq = 'X'.
  APPEND l_option TO l_restrict-opt_list_tab.

  l_ass-kind = 'S'.
  l_ass-name = 'S_ADDR'.
  l_ass-sg_main = 'I'.
  l_ass-sg_addy = space.
  l_ass-op_main = 'KEY1'.
  APPEND l_ass TO l_restrict-ass_tab.

  CALL FUNCTION 'SELECT_OPTIONS_RESTRICT'
    EXPORTING
      restriction            = l_restrict
    EXCEPTIONS
      too_late               = 1
      repeated               = 2
      selopt_without_options = 3
      selopt_without_signs   = 4
      invalid_sign           = 5
      empty_option_list      = 6
      invalid_kind           = 7
      repeated_kind_a        = 8
      OTHERS                 = 9.

*--------------------------------------------------------------------*
*START-OF-SELECTION
*--------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM write_report.
  PERFORM convert_2_html.
  PERFORM send_email.

*&---------------------------------------------------------------------*
*&      Form  WRITE_REPORT
*&---------------------------------------------------------------------*
FORM write_report.
  WRITE: / 'Hello World!' COLOR 1 INTENSIFIED ON.
ENDFORM.                    "WRITE_REPORT

*&---------------------------------------------------------------------*
*&      Form  convert_2_html
*&---------------------------------------------------------------------*
FORM convert_2_html.
  CHECK p_indi IS INITIAL.
  SUBMIT zncsi528 WITH p_indi = 'X'
                  EXPORTING LIST TO MEMORY
                  AND RETURN.
  CALL FUNCTION 'LIST_FROM_MEMORY'
    TABLES
      listobject = itab_list
    EXCEPTIONS
      not_found  = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'
      EXPORTING
        report_name = 'ZTEST'
      TABLES
        html        = itab_html
        listobject  = itab_list.
  ENDIF.
ENDFORM.                    "convert_2_html

*&---------------------------------------------------------------------*
*&      Form  send_email
*&---------------------------------------------------------------------*
FORM send_email.
  DATA: l_send_request  TYPE REF TO cl_bcs,
        l_zip           TYPE REF TO cl_abap_zip,
        l_sender        TYPE REF TO cl_sapuser_bcs,
        l_recipient     TYPE REF TO cl_cam_address_bcs,
        l_body_text     TYPE bcsy_text,
        l_obj_len       TYPE so_obj_len,
        l_subject       TYPE so_obj_des,
        l_exception     TYPE REF TO cx_bcs,
        l_message       TYPE string,
        l_content       TYPE solix_tab,
        l_html_xstr     TYPE xstring,
        l_zip_xstr      TYPE xstring,
        l_len           TYPE i,
        l_document      TYPE REF TO cl_document_bcs.
  CHECK p_indi IS INITIAL AND itab_html[] IS NOT INITIAL.
  CHECK itab_html[] IS NOT INITIAL AND s_addr[] IS NOT INITIAL.
  TRY.
      l_send_request = cl_bcs=>create_persistent( ).
      l_subject = 'cl_abap_zip & cl_bcs example'.
      APPEND:  '<HTML><BODY>'                                              TO l_body_text,
                 'Hi,'                                                     TO l_body_text,
                 '<p>Pls refer to the attached. Thanks.'                   TO l_body_text,
               '</BODY><HTML>'                                             TO l_body_text.
      l_document = cl_document_bcs=>create_document(
                            i_type        = 'HTM'
                            i_text        = l_body_text
                            i_subject     = l_subject
                    ).
      CALL METHOD cl_bcs_convert=>txt_to_xstring
        EXPORTING
          it_soli    = itab_html[]
        RECEIVING
          ev_xstring = l_html_xstr.
      CREATE OBJECT l_zip.
      CALL METHOD l_zip->add
        EXPORTING
          name    = 'cl_abap_zip & cl_bcs example.html'
          content = l_html_xstr.
      CALL METHOD l_zip->save
        RECEIVING
          zip = l_zip_xstr.
      CALL METHOD cl_bcs_convert=>xstring_to_solix
        EXPORTING
          iv_xstring = l_zip_xstr
        RECEIVING
          et_solix   = l_content.
      l_obj_len = XSTRLEN( l_zip_xstr ).
      CALL METHOD l_document->add_attachment
        EXPORTING
          i_attachment_size    = l_obj_len
          i_attachment_type    = 'ZIP'
          i_attachment_subject = l_subject
          i_att_content_hex    = l_content.
      l_send_request->set_document( l_document ).
      l_sender = cl_sapuser_bcs=>create( sy-uname ).
      LOOP AT s_addr.
        l_recipient = cl_cam_address_bcs=>create_internet_address( s_addr-low ).
        CALL METHOD l_send_request->add_recipient
          EXPORTING
            i_recipient = l_recipient.
      ENDLOOP.
      CALL METHOD l_send_request->set_send_immediately( 'X' ).
      CALL METHOD l_send_request->send(
      EXPORTING
        i_with_error_screen  = 'X'
        ).
      COMMIT WORK.
    CATCH cx_bcs INTO l_exception.
      l_message = l_exception->get_text( ).
      MESSAGE l_message TYPE 'E'.
  ENDTRY.
ENDFORM.                    "send_email

你可能感兴趣的:(example)