Send mail using BCS class

Send mail using BCS class

This is a basic example to sent mail using BCS class.


FORM send_mail.

  CLASS cl_bcs DEFINITION LOAD.

  DATA: lo_send_request TYPE REF TO cl_bcs,
        lx_document_bcs TYPE REF TO cx_document_bcs,
        lo_sender       TYPE REF TO if_sender_bcs VALUE IS INITIAL,
        lo_recipient    TYPE REF TO if_recipient_bcs VALUE IS INITIAL,
        lo_distlist     TYPE REF TO cl_distributionlist_bcs,
        lo_document     TYPE REF TO cl_document_bcs VALUE IS INITIAL,

        lv_receiver     TYPE adr6-smtp_addr,
        lt_message_body TYPE bcsy_text VALUE IS INITIAL,
        lv_sent         TYPE char1.

  " DATA: t_att_content_hex TYPE solix_tab. " Type of attachement table

  lo_send_request = cl_bcs=>create_persistent( ).

* Message body and subject:
* -------------------------

  APPEND 'Dear,' TO lt_message_body.
  APPEND ' ' TO lt_message_body.
  APPEND '----------------------------------' TO lt_message_body.
  APPEND 'Please fill here the mail content.' TO lt_message_body.
  APPEND '----------------------------------' TO lt_message_body.
  APPEND ' ' TO lt_message_body.
  APPEND 'Regards.' TO lt_message_body.


  lo_document = cl_document_bcs=>create_document( i_type = 'RAW'  " Use 'HTM' for HTML messages
                                                  i_text = lt_message_body
                                                  i_subject = 'Your mail subject' ).

* Add attachment is exist:
* ------------------------
  IF t_att_content_hex IS NOT INITIAL.
    TRY.
        lo_document->add_attachment( i_attachment_type = 'PDF'              " file extention
                                     i_attachment_subject = 'PDF attached'  " file name
*                                    I_ATTACHMENT_SIZE =
*                                    I_ATTACHMENT_LANGUAGE = SPACE
*                                    I_ATT_CONTENT_TEXT =
*                                    I_ATTACHMENT_HEADER =
                                     i_att_content_hex = t_att_content_hex ).

      CATCH cx_document_bcs INTO lx_document_bcs.
        " Manage error here
    ENDTRY.
  ENDIF.


* Pass the document to send request
* ---------------------------------
  lo_send_request->set_document( lo_document ).


* Set sender:
* -----------

* From mail addresse
* ~ ~ ~ ~ ~ ~ ~ ~ ~ 
  lo_sender = cl_cam_address_bcs=>create_internet_address( 'mail@domain.com ). 

* From SAP user
* ~ ~ ~ ~ ~ ~ ~ 
  lo_sender = cl_sapuser_bcs=>create( sy-uname ). 


  lo_send_request->set_sender( i_sender = lo_sender ).



* Set recipient:
* --------------

* From SAP user
* ~ ~ ~ ~ ~ ~ ~ 
  lo_recipient = cl_sapuser_bcs=>create( sy-uname ). 

  lo_send_request->add_recipient( i_recipient = lo_recipient
                                     i_express = 'X' ).

* From mail addresse
* ~ ~ ~ ~ ~ ~ ~ ~ ~ 
  lv_receiver = 'your_mail@domain.com'.
  lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_receiver ). 

  lo_send_request->add_recipient( i_recipient = lo_recipient
                                     i_express = 'X' ).

* From distribution list (SO15 / SO23)
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
  lo_distlist = cl_distributionlist_bcs=>getu_persistent( i_dliname = 'D_LIST_NAME'  " Limited to CHAR12
                                                           i_private = space ).
  lo_send_request->add_recipient( lo_distlist ).



* Send email:
* -----------
  TRY.
      lv_sent = lo_send_request->send( i_with_error_screen = 'X' ).

    CATCH cx_send_req_bcs.
      " Manage error here
  ENDTRY.
  COMMIT WORK.

  MESSAGE 'Mail sent' TYPE 'S'.

ENDFORM.

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply