Steps to Send Email with Attachment in ABAP (Background)
- Create document content (subject, body).
- Create an attachment (text, CSV, PDF, etc.).
- Build the BCS object and send.
REPORT z_send_email_attachment.
DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO cl_sapuser_bcs,
lo_recipient TYPE REF TO if_recipient_bcs.
DATA: lt_message TYPE bcsy_text, ” Body of email
lt_attach TYPE soli_tab, ” Attachment content
lv_subject TYPE so_obj_des.
START-OF-SELECTION.
“1. Create Send Request
lo_send_request = cl_bcs=>create_persistent( ).
“2. Set Document (Email Content)
APPEND ‘Hello,’ TO lt_message.
APPEND ‘This is a test email from ABAP background job.’ TO lt_message.
APPEND ‘Regards,’ TO lt_message.
APPEND ‘SAP System’ TO lt_message.
lv_subject = ‘ABAP Email with Attachment’.
lo_document = cl_document_bcs=>create_document(
i_type = ‘RAW’
i_text = lt_message
i_subject = lv_subject ).
“3. Create Attachment (simple TXT file here)
CLEAR lt_attach.
APPEND ‘Material;Plant;MRP Type’ TO lt_attach.
APPEND ‘MAT001;1000;PD’ TO lt_attach.
APPEND ‘MAT002;1100;VB’ TO lt_attach.
lo_document->add_attachment(
i_attachment_type = ‘TXT’ ” TXT, XLS, PDF etc.
i_attachment_subject = ‘MARA_MARC_List’
i_att_content_text = lt_attach ).
“4. Add Document to Send Request
lo_send_request->set_document( lo_document ).
“5. Sender (SAP user)
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_send_request->set_sender( lo_sender ).
“6. Recipient (email address)
lo_recipient = cl_cam_address_bcs=>create_internet_address(
‘testuser@example.com’ ).
lo_send_request->add_recipient(
i_recipient = lo_recipient
i_express = ‘X’ ).
“7. Send Email
lo_send_request->send( i_with_error_screen = ‘X’ ).
COMMIT WORK.
WRITE: ‘Email sent successfully’.
Key Points
cl_bcs
framework works in background and supports all formats.- You can attach:
- TXT/CSV → use
i_att_content_text
- Binary (PDF, Excel, Word) → use
i_att_content_hex
- TXT/CSV → use
- Run in background → email will still be sent (no popup).
- Use
COMMIT WORK
→ ensures email is queued in SAPconnect. - Check status in SOST transaction.