Пример отправки email из программы.
При отправке обязательно надо указать размер вложения. Без этого будет ошибка при открытии.
Входная структура типа:
1 2 3 4 5 6 |
TYPES: BEGIN OF gty_s_result, mail TYPE text100, "Адрес получателя text TYPE string, " Тело письма file TYPE xstring, "Сгенерированное вложение DOC\DOCX file_name TYPE text50, "Имя вложения END OF gty_s_result, |
Метод отправки письма c вложением:
1 |
METHODS: send IMPORTING is_result TYPE gty_s_result. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
METHOD send. CONSTANTS: lc_subject TYPE so_obj_des VALUE 'Уведомление об отпуске', lc_raw TYPE char03 VALUE 'RAW'. DATA: lv_mlrec TYPE so_obj_nam, lv_sent_to_all TYPE os_boolean, lv_subject TYPE so_obj_des, lt_text TYPE bcsy_text, lo_send_request TYPE REF TO cl_bcs, lo_bcs_exception TYPE REF TO cx_bcs, lo_recipient TYPE REF TO if_recipient_bcs, lo_sender TYPE REF TO cl_sapuser_bcs, lo_document TYPE REF TO cl_document_bcs, lt_attachment TYPE solix_tab . TRY. "Create send request lo_send_request = cl_bcs=>create_persistent( ). "От кого lo_sender = cl_sapuser_bcs=>create( sy-uname ). "Добавить отправителя CALL METHOD lo_send_request->set_sender EXPORTING i_sender = lo_sender. "Адресат lo_recipient = cl_cam_address_bcs=>create_internet_address( CONV #( is_result-mail ) ). "Добавить адресатов CALL METHOD lo_send_request->add_recipient EXPORTING i_recipient = lo_recipient i_express = 'X'. "Тело письма CALL METHOD cl_document_bcs=>string_to_soli( EXPORTING ip_string = is_result-text RECEIVING rt_soli = lt_text ). CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = is_result-file TABLES binary_tab = lt_attachment. lo_document = cl_document_bcs=>create_document( i_type = lc_raw i_text = lt_text i_subject = lc_subject ). DATA(lv_bytecount) = xstrlen( is_result-file ). TRY. lo_document->add_attachment( EXPORTING i_attachment_type = 'DOC' i_attachment_subject = conv #( is_result-file_name ) "'Уведомление' i_attachment_size = CONV #( lv_bytecount ) i_att_content_hex = lt_attachment ). CATCH cx_document_bcs. ENDTRY. "Добавить письмо CALL METHOD lo_send_request->set_document( lo_document ). "Отправить письмо CALL METHOD lo_send_request->send( EXPORTING i_with_error_screen = 'X' RECEIVING result = lv_sent_to_all ). "Commit и отправка письма COMMIT WORK. CATCH cx_bcs INTO lo_bcs_exception. ENDTRY. ENDMETHOD. |