send an email

How to send an email using the BCS framework.

DATA: lo_email TYPE REF TO cl_bcs,
 lo_document TYPE REF TO cl_document_bcs,
 lo_recipient TYPE REF TO if_recipient_bcs,
 lo_bcs_exp TYPE REF TO cx_bcs,
 lt_message TYPE soli_tab,
 lt_att_data TYPE soli_tab,
 ls_msg_line LIKE LINE OF lt_message,
 lv_data TYPE string,
 lv_subject TYPE so_obj_des,
 lv_att_name TYPE sood-objdes,
 lv_msg TYPE bapi_msg
 .

TRY.
 lo_email = cl_bcs=>create_persistent( ).

CONCATENATE 'Sammelrechnung' me->mv_opbel INTO lv_subject.

ls_msg_line-line = 'Anbei Ihre Sammelrechnung in elektronischer Form.'.
 INSERT ls_msg_line INTO TABLE lt_message.

lo_document = cl_document_bcs=>create_document(
 i_type = 'HTM'
 i_subject = lv_subject
 i_text = lt_message
 ).

me->transform(
 IMPORTING
 ev_result = lv_data
 ).


 lt_att_data = cl_document_bcs=>string_to_soli( lv_data ).

CONCATENATE me->mv_opbel '.xls' INTO lv_att_name.

lo_document->add_attachment(
 i_attachment_type = 'XLS'
 i_attachment_subject = lv_att_name
 i_att_content_text = lt_att_data
 ) .

lo_email->set_document( lo_document ).

lo_recipient = cl_cam_address_bcs=>create_internet_address( iv_email_address ).
 lo_email->add_recipient( i_recipient = lo_recipient ).

CALL METHOD lo_email->set_status_attributes
 EXPORTING
 i_requested_status = 'E'
 i_status_mail = 'E'.

CALL METHOD lo_email->set_send_immediately( abap_true ).

CALL METHOD lo_email->send(
 EXPORTING
 i_with_error_screen = 'X'
 ).
 COMMIT WORK.

CATCH cx_bcs INTO lo_bcs_exp.
 lv_msg = lo_bcs_exp->get_text( ).
 me->mo_log->add_free_text(
 EXPORTING
 iv_msgty = 'E'
 iv_text = lv_msg
 ).
 RAISE EXCEPTION TYPE zcx_sare_exception.
 ENDTRY.