Enviar email usando classe standard CL_BCS
Autor: Mauricio Lauffer | Publicado: 2014-04-17T17:58:00-03:00

Para enviar email com um programa ABAP é muito fácil, basta utilizar uma classe standard da SAP, a classe CL_BCS.
Ela é uma classe simples de utilizar, até mesmo para aqueles que ainda não estão acostumados com OO (ainda existe???).

Bom, vamos ao exemplo:





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

*&amp; Report  ZSEND_EMAIL

*&amp;

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

*&amp; ABAP Sample Code

*&amp; Send email with CL_BCS

*&amp;

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

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

*&amp;

*&amp; This sample explains how to send an email using class: CL_BCS

*&amp;

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



REPORT zsend_email.





CONSTANTS:

&nbsp;&nbsp;gc_subject  TYPE so_obj_des VALUE 'ABAP Email 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!', " Text used into the email body

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



DATA:

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

&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;"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.





Agora é copiar/colar e começar a brincar.
Barbadinha!

Link do código no GitHub:
https://github.com/mauriciolauffer/ABAP/blob/master/Email/send_email.abap