*& Report ZSENDEMAIL
*&
*&———————————————————————*
*& Example of sending external email via SAPCONNECT
*& Prueba de envio de mail
*&———————————————————————*
REPORT zsendemail.
perform SEND_MAIL.
perform initiate_mail_execute_program.
*———————————————————————–
*———————————————————————–
*———————————————————————–
*&———————————————————————*
*& Form SEND_MAIL
*&———————————————————————*
*&
*&———————————————————————*
FORM SEND_MAIL.
DATA: send_request TYPE REF TO cl_bcs.
DATA: text TYPE bcsy_text.
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: bcs_exception type ref to cx_bcs.
DATA: sent_to_all type os_boolean.
try.
" ——– create persistent send request ————————
send_request = cl_bcs=>create_persistent( ).
"——– create and set document ——————————-
" create document from internal table with text
APPEND ‘Hello world!’ TO text.
document = cl_document_bcs=>create_document(
i_type = ‘RAW’
i_text = text
i_length = ’12’
i_subject = ‘test created by BCS_EXAMPLE_4’ ).
" add document to send request
CALL METHOD send_request->set_document( document ).
"——— set sender ——————————————-
" note: this is necessary only if you want to set the sender
" different from actual user (SY-UNAME). Otherwise sender is
" set automatically with actual user.
sender = cl_sapuser_bcs=>create( sy-uname ).
CALL METHOD send_request->set_sender
EXPORTING i_sender = sender.
" enviar a una direccion email
" create recipient – please replace e-mail address !!!
recipient = cl_cam_address_bcs=>create_internet_address(
‘poncio_piloto@gmail.com’ ).
" add recipient with its respective attributes to send request
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = ‘X’.
" enviar a un shared distribution list
" https://www.sdn.sap.com/irj/sdn/thread?messageID=4220548
recipient = cl_distributionlist_bcs=>getu_persistent(
i_dliname = ‘KRONOS’
i_private = ‘ ‘).
" add recipient with its respective attributes to send request
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = ‘X’.
" esto permite que se a~adan o cambien las direcciones de mail y se envie en correo
"———- send document ———————————————————
"CALL METHOD send_request->edit(
" i_hide_note = ‘X’
" i_starting_at_x = 12
" i_starting_at_y = 5
" i_ending_at_x = 97
" i_ending_at_y = 22 ).
" y esto envia directamente
call method send_request->send.
COMMIT WORK.
"———————————————————–
" exception handling
"———————————————————–
" replace this very rudimentary exception handling
" with your own one !!!
"———————————————————–
catch cx_bcs into bcs_exception.
write: ‘Fehler aufgetreten.'(001).
write: ‘Fehlertyp:'(002), bcs_exception->error_type.
exit.
endtry.
endform.
form initiate_mail_execute_program.
*&———————————————————————*
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
*&———————————————————————*
* Instructs mail send program for SAPCONNECT to send email.
*———————————————————————-*
* http://www.sapdevelopment.co.uk/reporting/email/email_mbody.htm
data: gd_error type sy-subrc.
wait up to 3 seconds.
if gd_error eq 0.
submit rsconn01 with mode = ‘INT’ with output = ‘X’ and return.
endif.
endform.
* INITIATE_MAIL_EXECUTE_PROGRAM