Enviar email com anexos usando classe standard CL_BCS
Autor: Mauricio Lauffer | Publicado: 2014-05-07T11:24:00-03:00

Enviar email usando a classe CL_BCS é muito fácil, correto?
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:



*&amp;---------------------------------------------------------------------*

*&amp; Report  ZSEND_EMAIL_ATTACHMENT

*&amp;

*&amp;---------------------------------------------------------------------*

*&amp; ABAP Sample Code

*&amp; Send email with attachments using CL_BCS

*&amp;

*&amp; Maurício Lauffer - Brazil - 04.16.2014

*&amp; http://www.linkedin.com/in/mauriciolauffer

*&amp;

*&amp; This sample explains how to send an email with attachment

*&amp; using class: CL_BCS

*&amp;

*&amp;---------------------------------------------------------------------*



REPORT zsend_email_attachment.





CONSTANTS:

&nbsp;&nbsp;gc_subject     TYPE so_obj_des VALUE 'ABAP Email (attachment) with CL_BCS', " Email subject

&nbsp;&nbsp;gc_email_to    TYPE adr6-smtp_addr VALUE 'frodo.baggins@lotr.com', " Valid email

&nbsp;&nbsp;gc_text        TYPE soli VALUE 'Hello world! My first ABAP email with attachment!', " Text used into the email body

&nbsp;&nbsp;gc_type_raw    TYPE so_obj_tp VALUE 'RAW', " Email type

&nbsp;&nbsp;gc_att_type    TYPE soodk-objtp VALUE 'PDF', " Attachment type

&nbsp;&nbsp;gc_att_subject TYPE sood-objdes VALUE 'Document in PDF'. " Attachment title



DATA:

&nbsp;&nbsp;gt_text           TYPE soli_tab, " Table which contains email body text

&nbsp;&nbsp;gt_attachment_hex TYPE solix_tab, " Table which contains the attached file

&nbsp;&nbsp;gv_sent_to_all    TYPE os_boolean, " Receive the information if email was sent

&nbsp;&nbsp;gv_error_message  TYPE string, " Used to get the error message

&nbsp;&nbsp;go_send_request   TYPE REF TO cl_bcs, " Email object

&nbsp;&nbsp;go_recipient      TYPE REF TO if_recipient_bcs, " Who will receive the email

&nbsp;&nbsp;go_sender         TYPE REF TO cl_sapuser_bcs, " Who is sending the email

&nbsp;&nbsp;go_document       TYPE REF TO cl_document_bcs, " Email body

&nbsp;&nbsp;gx_bcs_exception  TYPE REF TO cx_bcs.





TRY.

&nbsp;&nbsp;&nbsp;&nbsp;"Create send request

&nbsp;&nbsp;&nbsp;&nbsp;go_send_request = cl_bcs=&gt;create_persistent( ).



&nbsp;&nbsp;&nbsp;&nbsp;"Email FROM...

&nbsp;&nbsp;&nbsp;&nbsp;go_sender = cl_sapuser_bcs=&gt;create( sy-uname ).

&nbsp;&nbsp;&nbsp;&nbsp;"Add sender to send request

&nbsp;&nbsp;&nbsp;&nbsp;go_send_request-&gt;set_sender( i_sender = go_sender ).



&nbsp;&nbsp;&nbsp;&nbsp;"Email TO...

&nbsp;&nbsp;&nbsp;&nbsp;go_recipient = cl_cam_address_bcs=&gt;create_internet_address( gc_email_to ).

&nbsp;&nbsp;&nbsp;&nbsp;"Add recipient to send request

&nbsp;&nbsp;&nbsp;&nbsp;go_send_request-&gt;add_recipient(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EXPORTING

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_recipient = go_recipient

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_express   = abap_true

&nbsp;&nbsp;&nbsp;&nbsp;).



&nbsp;&nbsp;&nbsp;&nbsp;"Email BODY

&nbsp;&nbsp;&nbsp;&nbsp;APPEND gc_text TO gt_text.

&nbsp;&nbsp;&nbsp;&nbsp;go_document = cl_document_bcs=&gt;create_document(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_type    = gc_type_raw

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_text    = gt_text

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_length  = '12'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_subject = gc_subject ).





&nbsp;&nbsp;&nbsp;&nbsp;"You should populate the table gt_attachment_hex

&nbsp;&nbsp;&nbsp;&nbsp;"with the binary data from your file!

&nbsp;&nbsp;&nbsp;&nbsp;"For example: gt_attachment_hex = binary_data_from_file.



&nbsp;&nbsp;&nbsp;&nbsp;"Attachment

&nbsp;&nbsp;&nbsp;&nbsp;go_document-&gt;add_attachment(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EXPORTING

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_attachment_type    = gc_att_type

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_attachment_subject = gc_att_subject

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_att_content_hex    = gt_attachment_hex

&nbsp;&nbsp;&nbsp;&nbsp;).



&nbsp;&nbsp;&nbsp;&nbsp;"Add document to send request

&nbsp;&nbsp;&nbsp;&nbsp;go_send_request-&gt;set_document( go_document ).



&nbsp;&nbsp;&nbsp;&nbsp;"Send email and get the result

&nbsp;&nbsp;&nbsp;&nbsp;gv_sent_to_all = go_send_request-&gt;send( i_with_error_screen = abap_true ).

&nbsp;&nbsp;&nbsp;&nbsp;IF gv_sent_to_all = abap_true.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WRITE 'Email sent!'.

&nbsp;&nbsp;&nbsp;&nbsp;ENDIF.



&nbsp;&nbsp;&nbsp;&nbsp;"Commit to send email

&nbsp;&nbsp;&nbsp;&nbsp;COMMIT WORK.



&nbsp;&nbsp;&nbsp;&nbsp;"Exception handling

&nbsp;&nbsp;CATCH cx_bcs INTO gx_bcs_exception.

&nbsp;&nbsp;&nbsp;&nbsp;gv_error_message = gx_bcs_exception-&gt;get_text( ).

&nbsp;&nbsp;&nbsp;&nbsp;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