Wednesday, January 25, 2017

Sending emails from ABAP / E-Mail mit ABAP versenden

* https://wiki.scn.sap.com/wiki/display/Snippets/Sending+mail+with+attachment+using+Object+Oriented+Approach
report zjso_send_email.

class lcl_email definition final.
  public section.
    class-methods send
      importing
                iv_subject      type so_obj_des
                it_message_body type bcsy_text
                it_attachment   type rmps_t_post_content optional
                iv_sender_email type adr6-smtp_addr
                it_recipient    type uiyt_iusr
      raising   cx_send_req_bcs cx_document_bcs cx_address_bcs.
endclass.

start-of-selection.
  try.

      lcl_email=>send(
        iv_subject 'Betreff der E-Mail'(001)
        it_message_body value #(
          line 'Hallo,'(002)
          line '  diese E-Mail wurde aus einem SAP-System verschickt.'(003)
          line '' )
          line 'Mit freundlichen Grüßen'(004)
        )
        iv_sender_email 'marvin.maybe@p1zz4.com'
        it_recipient value #(
          mandt sy-mandt
          email 'michael.laender@deutsch.de' )
          email 'ablink@usa.com' )
        )
      ).
      write 'E-Mail erfolgreich versendet'(005).
    catch cx_send_req_bcs cx_document_bcs cx_address_bcs into data(go_x).
      write / go_x->get_text).
  endtry.

class lcl_email implementation.
  method send.
    data lo_send_request type ref to cl_bcs.
    data lo_document type ref to cl_document_bcs.
    data lo_sender type ref to if_sender_bcs.
    data lv_attachment_subject type so_obj_des.
    data lo_recipient type ref to if_recipient_bcs.

    lo_send_request cl_bcs=>create_persistent).
    lo_document cl_document_bcs=>create_document(
      i_type 'RAW'
      i_text it_message_body
      i_subject iv_subject
    ).
    loop at it_attachment into data(ls_attachment).
      lv_attachment_subject ls_attachment-subject.
      lo_document->add_attachment(
          i_attachment_type    ls_attachment-objtp
          i_attachment_subject lv_attachment_subject
          i_att_content_hex    ls_attachment-cont_hex
      ).
    endloop.
    lo_send_request->set_documentlo_document ).
    lo_sender cl_cam_address_bcs=>create_internet_addressiv_sender_email ).
    lo_send_request->set_senderlo_sender ).
    loop at it_recipient into data(ls_recipientwhere email is not initial.
      lo_recipient cl_cam_address_bcs=>create_internet_addressls_recipient-email ).
      lo_send_request->add_recipientexporting
        i_recipient lo_recipient
        i_express 'X'
      ).
    endloop.
    lo_send_request->sendabap_true ).
  endmethod.
endclass.


Check transaction SOST after sending an email. 

No comments:

Post a Comment

SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...