Mas e enviar email com arquivos anexos? Tem como fazer com a CL_BCS?
Sim, tem como e é barbada também. Precisamos apenas de um método a mais para anexar arquivos no nosso email. Ah, e claro, precisamos preencher uma tabela interna com o conteúdo do arquivo que será anexado ao email.
Segue exemplo abaixo:
*&---------------------------------------------------------------------**& Report ZSEND_EMAIL_ATTACHMENT*&*&---------------------------------------------------------------------**& ABAP Sample Code*& Send email with attachments using CL_BCS*&*& Maurício Lauffer - Brazil - 04.16.2014*& http://www.linkedin.com/in/mauriciolauffer*&*& This sample explains how to send an email with attachment*& using class: CL_BCS*&*&---------------------------------------------------------------------*REPORT zsend_email_attachment.CONSTANTS:gc_subject TYPE so_obj_des VALUE 'ABAP Email (attachment) with CL_BCS', " Email subjectgc_email_to TYPE adr6-smtp_addr VALUE 'frodo.baggins@lotr.com', " Valid emailgc_text TYPE soli VALUE 'Hello world! My first ABAP email with attachment!', " Text used into the email bodygc_type_raw TYPE so_obj_tp VALUE 'RAW', " Email typegc_att_type TYPE soodk-objtp VALUE 'PDF', " Attachment typegc_att_subject TYPE sood-objdes VALUE 'Document in PDF'. " Attachment titleDATA:gt_text TYPE soli_tab, " Table which contains email body textgt_attachment_hex TYPE solix_tab, " Table which contains the attached filegv_sent_to_all TYPE os_boolean, " Receive the information if email was sentgv_error_message TYPE string, " Used to get the error messagego_send_request TYPE REF TO cl_bcs, " Email objectgo_recipient TYPE REF TO if_recipient_bcs, " Who will receive the emailgo_sender TYPE REF TO cl_sapuser_bcs, " Who is sending the emailgo_document TYPE REF TO cl_document_bcs, " Email bodygx_bcs_exception TYPE REF TO cx_bcs.TRY."Create send requestgo_send_request = cl_bcs=>create_persistent( )."Email FROM...go_sender = cl_sapuser_bcs=>create( sy-uname )."Add sender to send requestgo_send_request->set_sender( i_sender = go_sender )."Email TO...go_recipient = cl_cam_address_bcs=>create_internet_address( gc_email_to )."Add recipient to send requestgo_send_request->add_recipient(EXPORTINGi_recipient = go_recipienti_express = abap_true)."Email BODYAPPEND gc_text TO gt_text.go_document = cl_document_bcs=>create_document(i_type = gc_type_rawi_text = gt_texti_length = '12'i_subject = gc_subject )."You should populate the table gt_attachment_hex"with the binary data from your file!"For example: gt_attachment_hex = binary_data_from_file."Attachmentgo_document->add_attachment(EXPORTINGi_attachment_type = gc_att_typei_attachment_subject = gc_att_subjecti_att_content_hex = gt_attachment_hex)."Add document to send requestgo_send_request->set_document( go_document )."Send email and get the resultgv_sent_to_all = go_send_request->send( i_with_error_screen = abap_true ).IF gv_sent_to_all = abap_true.WRITE 'Email sent!'.ENDIF."Commit to send emailCOMMIT WORK."Exception handlingCATCH cx_bcs INTO gx_bcs_exception.gv_error_message = gx_bcs_exception->get_text( ).WRITE gv_error_message.ENDTRY.
Easy like a sunday morning...
Link do código no GitHub:
https://github.com/mauriciolauffer/ABAP/blob/master/Email/send_email_with_attachment.abap
Um comentário:
Fala Mauricio!
Excelente exemplo. Ajudou muito.
Abraço
Postar um comentário