CL_BCS
Business Communication Service
SAP에서 SMTP 서비스를 이용한 E-Mail 발송, SAP사서함으로 보내거나 외부 E-Mail로 발송 할 수 있다.
#사용 예시
REPORT ysandbox.
CLASS lcl_mail DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF ts_recipient,
recv_type TYPE string, "수신자 형태 ( TO : 수신, CC : 참조, BCC : 숨은참조 )
recv_email TYPE string, "수신자 메일주소
END OF ts_recipient .
TYPES: tt_recipient TYPE STANDARD TABLE OF ts_recipient WITH EMPTY KEY .
TYPES: BEGIN OF ts_fileinfo,
filename TYPE string,
bindata TYPE xstring,
END OF ts_fileinfo .
TYPES: tt_fileinfo TYPE STANDARD TABLE OF ts_fileinfo WITH EMPTY KEY .
TYPES: BEGIN OF ts_input,
subject TYPE string,
content TYPE string,
sender TYPE string,
isimportant TYPE abap_bool,
recipient TYPE tt_recipient,
attach_file TYPE tt_fileinfo,
END OF ts_input .
CLASS-METHODS: send_mail IMPORTING is_input TYPE ts_input
RETURNING VALUE(rv_result) TYPE string
RAISING cx_document_bcs.
ENDCLASS.
CLASS lcl_mail IMPLEMENTATION.
METHOD send_mail.
TRY.
DATA(lo_send_request) = cl_bcs=>create_persistent( ).
"Subject, Contents
DATA(lo_document) = cl_document_bcs=>create_document(
i_type = 'HTM'
i_subject = CONV #( is_input-subject )
i_importance = COND #( WHEN is_input-isimportant EQ abap_true THEN 1 )
i_text = cl_bcs_convert=>string_to_soli( is_input-content ) ).
"Attach files
LOOP AT is_input-attach_file ASSIGNING FIELD-SYMBOL(<lfs_attach_file>).
DATA(lv_file_size) = xstrlen( <lfs_attach_file>-bindata ).
lo_document->add_attachment(
i_attachment_type = 'BIN'
i_attachment_subject = CONV #( <lfs_attach_file>-filename )
i_attachment_filename = CONV #( <lfs_attach_file>-filename )
i_attachment_size = condense( CONV string( lv_file_size ) )
i_att_content_hex = cl_bcs_convert=>xstring_to_solix( <lfs_attach_file>-bindata ) ).
ENDLOOP.
"Add document to send request
lo_send_request->set_document( lo_document ).
"Sender
IF is_input-sender CA '@'.
lo_send_request->set_sender( cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( is_input-sender )
i_incl_sapuser = abap_true ) ).
ELSE.
lo_send_request->set_sender( cl_sapuser_bcs=>create( to_upper( COND #( WHEN is_input-sender IS NOT INITIAL
THEN is_input-sender ELSE sy-uname ) ) ) ).
ENDIF.
"발신함에 추가
lo_send_request->send_request->set_link_to_outbox( abap_true ).
"Recipients
LOOP AT is_input-recipient ASSIGNING FIELD-SYMBOL(<lfs_recipient>).
IF <lfs_recipient>-recv_email IS INITIAL.
CONTINUE.
ENDIF.
DATA(lv_is_email) = xsdbool( <lfs_recipient>-recv_email CA '@' ).
lo_send_request->add_recipient(
i_recipient = COND #( WHEN lv_is_email EQ abap_true
THEN cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( <lfs_recipient>-recv_email )
i_incl_sapuser = abap_true )
ELSE cl_sapuser_bcs=>create( CONV #( to_upper( <lfs_recipient>-recv_email ) ) ) )
i_express = xsdbool( lv_is_email EQ abap_false )
i_copy = xsdbool( <lfs_recipient>-recv_type EQ 'CC' )
i_blind_copy = xsdbool( <lfs_recipient>-recv_type EQ 'BCC' ) ).
ENDLOOP.
IF lo_send_request->recipients( ) IS INITIAL.
RETURN.
ENDIF.
"Set that you don't need a Return Status E-mai
lo_send_request->set_status_attributes(
i_requested_status = 'N' " Requested Status
i_status_mail = 'N' " Setting for Which Statuses Are Reported by Mail
).
"set send immediately flag
lo_send_request->set_send_immediately( abap_true ).
"Send Email
IF lo_send_request->send( ).
COMMIT WORK.
rv_result = cl_system_uuid=>create_uuid_c36_static( ).
ENDIF.
CATCH cx_root.
RAISE EXCEPTION TYPE cx_document_bcs.
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"파일 첨부
DATA(lt_files) = VALUE lcl_mail=>tt_fileinfo( ).
DATA(lt_recipients) = VALUE lcl_mail=>tt_recipient( ).
DATA: lt_filetable TYPE filetable,
lv_rc TYPE i.
cl_gui_frontend_services=>file_open_dialog(
EXPORTING
multiselection = abap_true
CHANGING
file_table = lt_filetable
rc = lv_rc
EXCEPTIONS
OTHERS = 5
).
DATA: lt_solix TYPE solix_tab,
lt_string TYPE STANDARD TABLE OF string.
LOOP AT lt_filetable INTO DATA(ls_filetable).
CLEAR lt_solix.
cl_gui_frontend_services=>gui_upload(
EXPORTING
filetype = 'BIN'
IMPORTING
filelength = DATA(lv_file_size)
CHANGING
data_tab = lt_solix
EXCEPTIONS
OTHERS = 19
).
APPEND INITIAL LINE TO lt_files ASSIGNING FIELD-SYMBOL(<lfs_file>).
SPLIT ls_filetable-filename AT '\' INTO TABLE lt_string.
<lfs_file>-filename = CONV #( VALUE #( lt_string[ lines( lt_string ) ] OPTIONAL ) ).
<lfs_file>-bindata = cl_bcs_convert=>solix_to_xstring(
it_solix = lt_solix
iv_size = CONV #( lv_file_size ) ).
ENDLOOP.
"수신자
lt_recipients = VALUE #( ( recv_type = `TO`
recv_email = `email_address@email.com` ) ).
"메일 발송
TRY.
DATA(lv_result_id) = lcl_mail=>send_mail( VALUE #( subject = `제목`
content = `내용`
sender = 'asdasd'
isimportant = abap_false
recipient = lt_recipients
attach_file = lt_files ) ).
CATCH cx_document_bcs INTO DATA(lo_cx). " BCS: Document Exceptions
WRITE:/ lo_cx->get_text( ).
RETURN.
ENDTRY.
WRITE:/ lv_result_id.
'ABAP > Function|Class' 카테고리의 다른 글
[Class] CL_SALV_TABLE - SALV 제어 (2) | 2024.10.31 |
---|---|
[Class] CL_AUTH_OBJECTS_TO_SQL - 사용자의 보유 권한으로 SQL 조건문 생성 (1) | 2024.10.29 |
[Class] CL_GUI_TEXTEDIT (1) | 2024.10.24 |
[Function] POPUP_GET_VALUES_DB_CHECKED - 필드값 입력 팝업 (0) | 2023.12.18 |
[Function] POPUP_GET_VALUES - 필드값 입력 팝업 (0) | 2023.12.18 |
댓글