As you know, I’m rather keen on compact code. So I was rather happy to see the “Send mail / fax with ~100 lines from a BSP application” Send mail / fax with ~100 lines from a BSP application by Dezso Pap . It proves that simple things like sending an e-mail can be programmed in an easy manner. The only point of ‘criticism’ from my end is that the well known SO_DOCUMENT_SEND_API1 is used. I see this FM, together with SO_NE_DOCUMENT_ATT_SEND_AP1, appearing everywhere in the forums as THE method/solution for sending mail, instead of the BCS classes, in order to prevent problems. This might be the case in some circumstances, but for this simple application it is certainly not.
Thomas already covered this method 1.5 years ago in his Sending E-Mail from ABAP – Version 610 and Higher – BCS Interface with his usual thoroughness and in-depth explanations. The method even has a help and a BSP tutorial application (BSPTUTORIALMAIL). The problem with the help and tutorial is that they give the impression that you need to put your code in a method of the application class, and they make it all look a bit more complicated than it needs to be. From an educational point of view I do not consider this to be a good way to help users understand a new(er) way of working. I therefore thought that it was time to kind of reintroduce this method.
Less is more
I’m not going to rewrite Thomas’ web log on this though. I am only going to provide the absolute minimum code which is needed to send an e-mail. You can always fall back on Thomas’ web log to extend things. I won’t explain each line either, since they have already been thoroughly commented by Thomas. The additional comment lines should be sufficient.
I’ve used the parameters that Dezso used so that when you want to implement the code it is simply a matter of doing a copy/paste.
* some essential class defs
DATA: send_request TYPE REF TO cl_bcs,
document TYPE REF TO cl_document_bcs,
sender TYPE REF TO cl_sapuser_bcs,
recipient TYPE REF TO if_recipient_bcs,
exception_info TYPE REF TO if_os_exception_info,
bcs_exception TYPE REF TO cx_bcs,
l_mailtext TYPE BCSY_TEXT,
c_address TYPE ADR6-SMTP_ADDR.
* split the string and put it in a table, see also
CALL FUNCTION 'Z_EU_STRING_TO_TABLE'
text = body
line_length = 255
text_tab = l_mailtext.
* Create persistent send request
send_request = cl_bcs=>create_persistent( ).
* Create document
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = l_mailtext
i_subject = subject ).
* Add document to send request
CALL METHOD send_request->set_document( document ).
* Get sender object
sender = cl_sapuser_bcs=>create( sy-uname ).
* Add sender
CALL METHOD send_request->set_sender
EXPORTING i_sender = sender.
* Create recipient
c_address = receiver.
recipient = cl_cam_address_bcs=>create_internet_address( c_address ).
* Add recipient with its respective attributes to send request
CALL METHOD send_request->add_recipient
i_recipient = recipient
i_express = ' '
i_copy = ' '
i_blind_copy = ' '.
* Send document
CALL METHOD send_request->send( ).
CATCH cx_bcs INTO bcs_exception.
RAISE EXCEPTION bcs_exception.
As you can see, no rocket science, or even application classes, are involved. If you crave more info. you can find it in the above mentioned references. Additionally, you can take a look at this help too.