The actual work in carried out in this method: The objects required for a mail request are created, linked together, and the mail is sent.
Parameters
Parameter Name |
Type |
Typing |
Reference Type |
Description |
MAIL_ADDRESS |
Importing |
Type |
String |
E-mail address |
MESSAGES |
Changing |
Type |
CL_BSP_MESSAGES |
Message list (optional) |
The parameter
MESSAGES is defined so that you can catch error situations.Code
You need classes
CA_SAPUSER_BCS and CL_CAM_ADDRESS_BCS .CLASS ca_sapuser_bcs DEFINITION LOAD.
CLASS cl_cam_address_bcs DEFINITION LOAD.
You require the following local variables:
DATA: send_request TYPE REF TO cl_bcs.
DATA: document TYPE REF TO cl_document_bcs.
DATA: sender TYPE REF TO cl_sapuser_bcs.
DATA: recipient TYPE REF TO if_recipient_bcs.
DATA: exception_info TYPE REF TO if_os_exception_info,
bcs_exception type ref to cx_bcs.
DATA: num_rows type i.
DATA: textlength type SO_OBJ_LEN.
First create a mail request:
send_request = cl_bcs=>create_persistent( ).
Create the mail sender:
* Get sender object
sender = cl_sapuser_bcs=>create( sy-uname ).
* Add sender
CALL METHOD send_request->set_sender
EXPORTING i_sender = sender.
You can define your mail address in this Web Application Server in the user maintenance.
The mail can be sent to one or more addresses:
* Create recipient
data: c_address TYPE ADR6-SMTP_ADDR.
move mail_address to c_address.
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
EXPORTING
i_recipient = recipient
i_express = ' '
i_copy = ' '
i_blind_copy = ' '.
You receive the mail address as a string and you have to convert in into the required data type.
The mail is sent to one address only without any additional attributes.
You can now send the mail and write everything to the database.
* Send document
COMMIT WORK.
You still have not taken any precautions against error situations.
You can take care of this by embedding the following code in a
TRY – CATCH block:try.
... Now the code comes after the creation of
send_request up to the COMMIT WORK: catch cx_bcs into bcs_exception.
if messages is not initial.
messages->add_message( condition = 'mail'
message =
'Error occurred while sending the mail' )."#EC NOTEXT
endif.
exit
endtry.