Sunday, 19 April 2009

Send email in ABAP using class cl_bcs

Recently, I got many developments related to email sending on ABAP.
Mostly I used function module SO_NEW_DOCUMENT_SEND_API1 but now, because of the demanding
and trying to write a better coding, I use object oriented style for sending email.

Today's higlight is about class cl_bcs for writing and sending email. One advantage I get is
by using class cl_bcs, I can menipulate (modify) the sender email address, while I can't if use
function module SO_NEW_DOCUMENT_SEND_API1.

I will give some line of codes that show you how to create simple email, use html formated email,
and how to create attachment inn the email.
These codes below will give some example how to drive this class. You can check the results using
tcode SOST.

Codes:

" these three examples show how email can be sent in ABAP.
" all of the three are similar except when they are creating
" documents in "email contents" and "create documents" section

PERFORM simple_send_email.
PERFORM send_email_in_html_format.
PERFORM send_email_with_attachments.

*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
FORM simple_send_email.

DATA : l_o_send_request TYPE REF TO cl_bcs, " email request object
l_o_document TYPE REF TO cl_document_bcs, " documents object
l_o_sender TYPE REF TO cl_cam_address_bcs, " sender object
l_o_recipient TYPE REF TO cl_cam_address_bcs, " recipient object
bcs_exception TYPE REF TO cx_bcs, " exceptions
l_v_ret javascript:void(0) TYPE os_boolean, " boolean return value

" document contents
l_it_contents TYPE bcsy_text,
l_wa_contents TYPE LINE OF bcsy_text.


TRY.
" create email objects
l_o_send_request = cl_bcs=>create_persistent( ).


" sender
l_o_sender = cl_cam_address_bcs=>create_internet_address( 'emailaddress@sender.com' ).
l_o_send_request->set_sender( i_sender = l_o_sender ).


" recipient TO
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailto@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = '' " CC indicator
).

" recipient CC
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailcc@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = 'X' " CC indicator
).

**********************************************************************
" email contents
CLEAR l_wa_contents.
l_wa_contents-line = 'Dear Recipients,'.
APPEND l_wa_contents TO l_it_contents.

CLEAR l_wa_contents.
l_wa_contents-line = 'This in the contents'.
APPEND l_wa_contents TO l_it_contents.

" create documents
l_o_document = cl_document_bcs=>create_document(
i_type = 'RAW' " RAW document format
i_text = l_it_contents
i_subject = 'This Is The Subject for Simple Email'
).
l_o_send_request->set_document( l_o_document ).
**********************************************************************

" send email
l_v_ret = l_o_send_request->send( ).

CATCH cx_bcs INTO bcs_exception.
* exceptions, do something

ENDTRY.

" never forget this one
COMMIT WORK.

ENDFORM. " FORM simple_send_email.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
FORM send_email_in_html_format.

DATA : l_o_send_request TYPE REF TO cl_bcs, " email request object
l_o_document TYPE REF TO cl_document_bcs, " documents object
l_o_sender TYPE REF TO cl_cam_address_bcs, " sender object
l_o_recipient TYPE REF TO cl_cam_address_bcs, " recipient object
bcs_exception TYPE REF TO cx_bcs, " exceptions
l_v_ret TYPE os_boolean, " boolean return value

" document contents
l_it_contents TYPE bcsy_text,
l_wa_contents TYPE LINE OF bcsy_text.


TRY.
" create email objects
l_o_send_request = cl_bcs=>create_persistent( ).


" sender
l_o_sender = cl_cam_address_bcs=>create_internet_address( 'emailaddress@sender.com' ).
l_o_send_request->set_sender( i_sender = l_o_sender ).


" recipient TO
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailto@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = '' " CC indicator
).

" recipient CC
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailcc@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = 'X' " CC indicator
).

**********************************************************************
" email contents
CLEAR l_wa_contents.
l_wa_contents-line = 'Dear Recipients,
'.
APPEND l_wa_contents TO l_it_contents.

CLEAR l_wa_contents.
l_wa_contents-line = 'This is the contents in bold format
'.
APPEND l_wa_contents TO l_it_contents.

CLEAR l_wa_contents.
l_wa_contents-line = 'This is the contents in italic format'.
APPEND l_wa_contents TO l_it_contents.

" create documents
l_o_document = cl_document_bcs=>create_document(
i_type = 'HTM' " HTML document format
i_text = l_it_contents
i_subject = 'This Is The Subject for Email With HTML format'
).
l_o_send_request->set_document( l_o_document ).
**********************************************************************

" send email
l_v_ret = l_o_send_request->send( ).

CATCH cx_bcs INTO bcs_exception.
* exceptions, do something

ENDTRY.

" never forget this one
COMMIT WORK.

ENDFORM. " FORM send_email_in_html_format.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
FORM send_email_with_attachments.

DATA : l_o_send_request TYPE REF TO cl_bcs, " email request object
l_o_document TYPE REF TO cl_document_bcs, " documents object
l_o_sender TYPE REF TO cl_cam_address_bcs, " sender object
l_o_recipient TYPE REF TO cl_cam_address_bcs, " recipient object
bcs_exception TYPE REF TO cx_bcs, " exceptions
l_v_ret TYPE os_boolean, " boolean return value

" document contents
l_it_contents TYPE bcsy_text,
l_wa_contents TYPE LINE OF bcsy_text,

" documents attachment
l_i_attachment TYPE solix_tab.


TRY.
" create email objects
l_o_send_request = cl_bcs=>create_persistent( ).


" sender
l_o_sender = cl_cam_address_bcs=>create_internet_address( 'emailaddress@sender.com' ).
l_o_send_request->set_sender( i_sender = l_o_sender ).


" recipient TO
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailto@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = '' " CC indicator
).

" recipient CC
l_o_recipient = cl_cam_address_bcs=>create_internet_address( 'emailcc@recipient.com' ).
l_o_send_request->add_recipient(
i_recipient = l_o_recipient
i_copy = 'X' " CC indicator
).

**********************************************************************
" email contents
CLEAR l_wa_contents.
l_wa_contents-line = 'Dear Recipients,'.
APPEND l_wa_contents TO l_it_contents.

CLEAR l_wa_contents.
l_wa_contents-line = 'This in the contents attachments'.
APPEND l_wa_contents TO l_it_contents.

" create documents
l_o_document = cl_document_bcs=>create_document(
i_type = 'RAW' " RAW document format
i_text = l_it_contents
i_subject = 'This Is The Subject for Email With Attachment'
).

l_o_document->add_attachment(
i_attachment_type = 'PDF' " add PDF attachment
i_attachment_subject = 'PDF attachment'
i_att_content_hex = l_i_attachment
).
l_o_send_request->set_document( l_o_document ).
**********************************************************************

" send email
l_v_ret = l_o_send_request->send( ).

CATCH cx_bcs INTO bcs_exception.
* exceptions, do something

ENDTRY.

" never forget this one
COMMIT WORK.

ENDFORM. " FORM simple_send_email.

37 comments:

  1. Where do I add a PDF?
    I use:
    - 'SSF_FUNCTION_MODULE_NAME'
    - 'CONVERT_OTF'
    - And I have the PDF in the internal table li_tline
    Now, How do I add the email?

    Thank you very much, this code is very useful for my

    ReplyDelete
  2. Hi, monoceros. This is an interesting post. What version is this code applicable? I am trying to use it in our 4.6C system but the clas CL_BCS is not found.

    ReplyDelete
  3. CL_BCS is available on 5.0, I don't know if any latest version has this or not.

    ReplyDelete
  4. Thank You very much for this code.
    Can one change the length of a line? can the line length be dynamic?
    Now it is 255 chars long and if I make a .txt or .csv file I get many space chars in each line after my data.

    ReplyDelete
  5. greate thanks mate............:)

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. This comment has been removed by a blog administrator.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. This comment has been removed by a blog administrator.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. This comment has been removed by a blog administrator.

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete
  22. This comment has been removed by a blog administrator.

    ReplyDelete
  23. This comment has been removed by a blog administrator.

    ReplyDelete
  24. This comment has been removed by a blog administrator.

    ReplyDelete
  25. This comment has been removed by a blog administrator.

    ReplyDelete
  26. This comment has been removed by a blog administrator.

    ReplyDelete
  27. This comment has been removed by a blog administrator.

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. This comment has been removed by a blog administrator.

    ReplyDelete
  30. This comment has been removed by a blog administrator.

    ReplyDelete
  31. This comment has been removed by a blog administrator.

    ReplyDelete
  32. This comment has been removed by a blog administrator.

    ReplyDelete
  33. This comment has been removed by a blog administrator.

    ReplyDelete
  34. This comment has been removed by a blog administrator.

    ReplyDelete
  35. This comment has been removed by a blog administrator.

    ReplyDelete
  36. Thanks a lot
    I'll try it
    best regards
    Marco

    ReplyDelete