Inicio > ABAP/4 > Enviar E-Mail – Mejor forma

Enviar E-Mail – Mejor forma

Caso: buscando encontré este código para envío de mail, muy correcto y simple.

Solución: Lo hice en una función

FUNCTION z_envio_mail.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_SUBJECT) TYPE  SO_OBJ_DES OPTIONAL
*"     REFERENCE(I_BODY) TYPE  BCSY_TEXT OPTIONAL
*"     REFERENCE(I_TYPE_DOC) TYPE  SO_OBJ_TP OPTIONAL
*"     REFERENCE(I_SENDER) TYPE  AD_SMTPADR OPTIONAL
*"     REFERENCE(I_RECEIVERS) TYPE  BCSY_SMTPA
*"----------------------------------------------------------------------
 
  DATA: lo_send_request   TYPE REF TO cl_bcs,
        lo_bcs_exception  TYPE REF TO cx_bcs,
        lo_document       TYPE REF TO cl_document_bcs,
        lo_sender         TYPE REF TO if_sender_bcs,
        lo_recipient      TYPE REF TO if_recipient_bcs,
        lit_message_body  TYPE bcsy_text,
        lx_document_bcs   TYPE REF TO cx_document_bcs,
        lv_sent_to_all    TYPE os_boolean,
        lv_sender         TYPE adr6-smtp_addr,
        lv_uname          TYPE uname,
        lv_emailadd       TYPE adr6-smtp_addr,
        wa_receivers      TYPE LINE OF bcsy_smtpa,
        lv_sub            TYPE so_obj_des,
        lv_subrc          TYPE sy-subrc,
        lv_error          TYPE string.
 
  TRY.
      lo_send_request = cl_bcs=>create_persistent( ).
*************** Subject      *****************************************
      CLEAR lv_sub.
      lv_sub = i_subject. " Asunto
*************** Message Body *****************************************
      REFRESH:lit_message_body.
      lit_message_body[] = i_body[].
 
*     put your text into the document
      IF i_type_doc IS NOT INITIAL.
        lo_document = cl_document_bcs=>create_document(
                         i_type     = i_type_doc
                         i_text     = lit_message_body
                         i_subject  = lv_sub ).
      ELSE.
        lo_document = cl_document_bcs=>create_document(
                         i_type     = 'RAW'
                         i_text     = lit_message_body
                         i_subject  = lv_sub ).
      ENDIF.
*************** Create sender *****************************************
      lv_sender = i_sender.                               " 'remitente@mail.com'.
      IF lv_sender IS INITIAL.
        lo_sender = cl_sapuser_bcs=>create( sy-uname ).
      ELSE.
        lo_sender = cl_cam_address_bcs=>create_internet_address( lv_sender ).
      ENDIF.
 
*         Set sender
      lo_send_request->set_sender( lo_sender ).
 
*************** Create Receiver *****************************************
      LOOP AT i_receivers INTO wa_receivers.
        lv_emailadd = wa_receivers. " destinatario@mail.com
        lo_recipient = cl_cam_address_bcs=>create_internet_address(
                                lv_emailadd  ).
 
*           Add recipient object to send request
        CALL METHOD lo_send_request->add_recipient
          EXPORTING
            i_recipient = lo_recipient
            i_express   = 'X'.
      ENDLOOP.
*************** Send Email *****************************************
      lo_send_request->set_document( lo_document ).
 
      lo_send_request->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result = lv_sent_to_all ).
 
      COMMIT WORK.
 
      IF lv_sent_to_all IS INITIAL.
        lv_subrc = '1'.
      ELSE.
        CLEAR lv_subrc.
      ENDIF.
 
    CATCH cx_bcs  INTO lo_bcs_exception.
      lv_error = lo_bcs_exception->get_text( ).
      lv_subrc = '1'.
 
  ENDTRY.
 
ENDFUNCTION.

Link: http://www.patelashish.com/wordpress/2013/08/abap-code-best-way-series-send-email/

Categorías:ABAP/4 Etiquetas: , , , ,
  1. Aún no hay comentarios.
  1. No trackbacks yet.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: