Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Send email - set sr_state

marcela_martinez
Participant
0 Likes
2,272

Hi friends,

I'm trying to send emails with BCS_EXAMPLE_1 program code. I have to send the same email to a list of recipients.

For the first recipient it works fine, but when in the same execution I try to send the same email to another one, I get a dump. The dump is because I'm not catching the exception, but the problem is another. I'll try to explain it:

I debug the code and I realized that in add_recipient METHOD (from cl_bcs CLASS) there is a variable: sr_state. For the first recipient (when I call add_recipient the first time) sr_state = ' '. For the second recipient (when I call add_recipient the second time) sr_state = 'S'.

I think I have to initialize sr_state before calling add_recipient METHOD but I don't know how. Can you help me with this issue please?

I'm so sorry bothering you with this, but I'm getting in OO programs by myself.

Thanks in advance and kind regards.

M.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,811

Hi,

Instead of trying with the standard program, try to generate your own using CL_BCS.

Sharing one of my codes with you where I created a function module which could be used to send emails with attachments supporting all formats and to any number of recipients.

Let me know if you still need help.

Thanks

Anubhav

*******************************************************************************************************

FUNCTION z_edh_send_mail.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     REFERENCE(IM_SUBJECT) TYPE  SO_OBJ_DES

*"     REFERENCE(IM_MESSAGE_BODY) TYPE  BCSY_TEXT

*"     REFERENCE(IM_ATTACHMENTS) TYPE  RMPS_T_POST_CONTENT OPTIONAL

*"     REFERENCE(IM_SENDER_UID) TYPE  SYUNAME OPTIONAL

*"     REFERENCE(IM_RECIPIENT_UID) TYPE  SYUNAME OPTIONAL

*"     REFERENCE(IM_SENDER_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL

*"     REFERENCE(IM_RECIPIENT_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL

*"  EXPORTING

*"     REFERENCE(EX_RESULT) TYPE  CHAR100

*"  TABLES

*"      IT_RECIPIENTS STRUCTURE  UIYS_IUSR OPTIONAL

*"----------------------------------------------------------------------

************************************************************************

*                   D A T A   D E C L A R A T I O N

************************************************************************

   DATA:

         lx_bcs_exception    TYPE REF TO cx_bcs            VALUE IS INITIAL,

         lo_send_request     TYPE REF TO cl_bcs            VALUE IS INITIAL,

         lo_sender           TYPE REF TO if_sender_bcs     VALUE IS INITIAL,

         lo_document         TYPE REF TO cl_document_bcs   VALUE IS INITIAL,

         lx_document_bcs     TYPE REF TO cx_document_bcs   VALUE IS INITIAL,

         lx_send_req_bcs     TYPE REF TO cx_send_req_bcs   VALUE IS INITIAL,

         lo_recipient        TYPE REF TO if_recipient_bcs  VALUE IS INITIAL,

******************

*& Internal Tables

******************

         gt_contents   TYPE soli_tab,

         gt_hex        TYPE solix_tab,

         gt_hex2       TYPE solix_tab,

*************

*& Work Areas

*************

         gs_text       TYPE string,

         gs_recipient  LIKE LINE OF it_recipients,

         gs_attachment LIKE LINE OF im_attachments,

*************

*& Varianbles

*************

         v_line           TYPE i,

         v_recipient_uid  TYPE uname,

         v_xstring        TYPE xstring,

         v_att_type       TYPE so_obj_tp,

         v_attachment_sub TYPE so_obj_des,

         v_size           TYPE sood-objlen,

         v_recipient_mail TYPE adr6-smtp_addr.

*************

*& Constants

*************

   CONSTANTS c_255 TYPE int3 VALUE '255'.

************************************************************************

*                 P R O C E S S I N G    L O G I C

************************************************************************

   TRY.

**********************

*& Prepare Mail Object

**********************

       CLASS cl_bcs DEFINITION LOAD.

       lo_send_request = cl_bcs=>create_persistent( ).

***************************

*& Message Body and Subject

***************************

       lo_document = cl_document_bcs=>create_document(

       i_type = 'RAW'

       i_text =  im_message_body

       i_subject = im_subject ).

*********************

*& Adding Attachments

*********************

       IF im_attachments[] IS NOT INITIAL.

         READ TABLE im_attachments INTO gs_attachment INDEX 1.

*** Saving the attachment type like CSV, TXT, PDF etc

         v_att_type = gs_attachment-objtp.

*** Conversion of text data to finally Binary data, if no Binary data is provided

         IF gs_attachment-cont_hex[] IS INITIAL.

           LOOP AT gs_attachment-cont_text INTO gs_text.

             CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

               EXPORTING

                 text   = gs_text

               IMPORTING

                 buffer = v_xstring

               EXCEPTIONS

                 failed = 1

                 OTHERS = 2.

             IF sy-subrc = 0.

               CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

                 EXPORTING

                   buffer     = v_xstring

                 TABLES

                   binary_tab = gt_hex.

               APPEND LINES OF gt_hex TO gt_hex2.

               REFRESH gt_hex.

               CLEAR : gs_text,

                       v_xstring.

             ENDIF.

           ENDLOOP.

         ENDIF.      "IF gs_attachment-cont_hex[] IS INITIAL.

*** Document size

         CLEAR v_size.

         CLEAR v_line.

         v_line = lines( gt_contents ).

         v_size = v_line * c_255.

         CONDENSE v_size.

         v_attachment_sub = gs_attachment-subject.

         TRY.

             lo_document->add_attachment(

             EXPORTING

             i_attachment_type     = v_att_type

             i_attachment_subject  = v_attachment_sub

             i_attachment_size     = v_size

             i_att_content_hex     = gt_hex2 ).

           CATCH cx_document_bcs INTO lx_document_bcs.

             ex_result = lx_document_bcs->get_text( ).

         ENDTRY.

       ENDIF.      "IF im_attachments[] IS NOT INITIAL.

************************************

*& Pass the document to send request

************************************

       lo_send_request->set_document( lo_document ).

       TRY.

           IF im_sender_mail IS NOT INITIAL.

             lo_sender = cl_cam_address_bcs=>create_internet_address( im_sender_mail ).

           ELSEIF im_sender_uid IS NOT INITIAL.

             lo_sender = cl_sapuser_bcs=>create( im_sender_uid ).

           ENDIF.

************

* Set Sender

************

           lo_send_request->set_sender(

           EXPORTING

           i_sender = lo_sender ).

         CATCH cx_address_bcs.

           RETURN.

       ENDTRY.

*****************

*& Set Recipients

*****************

       IF it_recipients[] IS INITIAL.

         IF im_recipient_mail IS NOT INITIAL.

           lo_recipient = cl_cam_address_bcs=>create_internet_address( im_recipient_mail ).

         ELSEIF im_recipient_uid IS NOT INITIAL.

           lo_recipient = cl_sapuser_bcs=>create( im_recipient_uid ).

         ENDIF.

         lo_send_request->add_recipient(

         EXPORTING

         i_recipient = lo_recipient

         i_express = 'X' ).

       ELSE.

         LOOP AT it_recipients INTO gs_recipient.

           IF gs_recipient-iusrid IS NOT INITIAL.

             v_recipient_uid = gs_recipient-iusrid.

             lo_recipient = cl_sapuser_bcs=>create( v_recipient_uid ).

           ELSEIF gs_recipient-email IS NOT INITIAL.

             v_recipient_mail = gs_recipient-email .

             lo_recipient = cl_cam_address_bcs=>create_internet_address( v_recipient_mail ).

           ENDIF.

           lo_send_request->add_recipient(

               EXPORTING

               i_recipient = lo_recipient

               i_express = 'X' ).

         ENDLOOP.

       ENDIF.      "IF it_recipients[] IS INITIAL... ELSE..

       TRY.

*************

*& Send Email

*************

           lo_send_request->send(

           EXPORTING

           i_with_error_screen = 'X'

           RECEIVING

           result = ex_result ).

           COMMIT WORK AND WAIT.

         CATCH cx_send_req_bcs INTO lx_send_req_bcs.

           ex_result = lx_send_req_bcs->get_text( ).

       ENDTRY.

*** Exception Handling (generic exception message)

     CATCH cx_bcs INTO lx_bcs_exception.

       ex_result = lx_bcs_exception->get_text( ).

   ENDTRY.

ENDFUNCTION.

Hi friends,

I'm trying to send emails with BCS_EXAMPLE_1 program code. I have to send the same email to a list of recipients.

For the first recipient it works fine, but when in the same execution I try to send the same email to another one, I get a dump. The dump is because I'm not catching the exception, but the problem is another. I'll try to explain it:

I debug the code and I realized that in add_recipient METHOD (from cl_bcs CLASS) there is a variable: sr_state. For the first recipient (when I call add_recipient the first time) sr_state = ' '. For the second recipient (when I call add_recipient the second time) sr_state = 'S'.

I think I have to initialize sr_state before calling add_recipient METHOD but I don't know how. Can you help me with this issue please?

I'm so sorry bothering you with this, but I'm getting in OO programs by myself.

Thanks in advance and kind regards.

M.

1 REPLY 1
Read only

Former Member
0 Likes
1,812

Hi,

Instead of trying with the standard program, try to generate your own using CL_BCS.

Sharing one of my codes with you where I created a function module which could be used to send emails with attachments supporting all formats and to any number of recipients.

Let me know if you still need help.

Thanks

Anubhav

*******************************************************************************************************

FUNCTION z_edh_send_mail.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     REFERENCE(IM_SUBJECT) TYPE  SO_OBJ_DES

*"     REFERENCE(IM_MESSAGE_BODY) TYPE  BCSY_TEXT

*"     REFERENCE(IM_ATTACHMENTS) TYPE  RMPS_T_POST_CONTENT OPTIONAL

*"     REFERENCE(IM_SENDER_UID) TYPE  SYUNAME OPTIONAL

*"     REFERENCE(IM_RECIPIENT_UID) TYPE  SYUNAME OPTIONAL

*"     REFERENCE(IM_SENDER_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL

*"     REFERENCE(IM_RECIPIENT_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL

*"  EXPORTING

*"     REFERENCE(EX_RESULT) TYPE  CHAR100

*"  TABLES

*"      IT_RECIPIENTS STRUCTURE  UIYS_IUSR OPTIONAL

*"----------------------------------------------------------------------

************************************************************************

*                   D A T A   D E C L A R A T I O N

************************************************************************

   DATA:

         lx_bcs_exception    TYPE REF TO cx_bcs            VALUE IS INITIAL,

         lo_send_request     TYPE REF TO cl_bcs            VALUE IS INITIAL,

         lo_sender           TYPE REF TO if_sender_bcs     VALUE IS INITIAL,

         lo_document         TYPE REF TO cl_document_bcs   VALUE IS INITIAL,

         lx_document_bcs     TYPE REF TO cx_document_bcs   VALUE IS INITIAL,

         lx_send_req_bcs     TYPE REF TO cx_send_req_bcs   VALUE IS INITIAL,

         lo_recipient        TYPE REF TO if_recipient_bcs  VALUE IS INITIAL,

******************

*& Internal Tables

******************

         gt_contents   TYPE soli_tab,

         gt_hex        TYPE solix_tab,

         gt_hex2       TYPE solix_tab,

*************

*& Work Areas

*************

         gs_text       TYPE string,

         gs_recipient  LIKE LINE OF it_recipients,

         gs_attachment LIKE LINE OF im_attachments,

*************

*& Varianbles

*************

         v_line           TYPE i,

         v_recipient_uid  TYPE uname,

         v_xstring        TYPE xstring,

         v_att_type       TYPE so_obj_tp,

         v_attachment_sub TYPE so_obj_des,

         v_size           TYPE sood-objlen,

         v_recipient_mail TYPE adr6-smtp_addr.

*************

*& Constants

*************

   CONSTANTS c_255 TYPE int3 VALUE '255'.

************************************************************************

*                 P R O C E S S I N G    L O G I C

************************************************************************

   TRY.

**********************

*& Prepare Mail Object

**********************

       CLASS cl_bcs DEFINITION LOAD.

       lo_send_request = cl_bcs=>create_persistent( ).

***************************

*& Message Body and Subject

***************************

       lo_document = cl_document_bcs=>create_document(

       i_type = 'RAW'

       i_text =  im_message_body

       i_subject = im_subject ).

*********************

*& Adding Attachments

*********************

       IF im_attachments[] IS NOT INITIAL.

         READ TABLE im_attachments INTO gs_attachment INDEX 1.

*** Saving the attachment type like CSV, TXT, PDF etc

         v_att_type = gs_attachment-objtp.

*** Conversion of text data to finally Binary data, if no Binary data is provided

         IF gs_attachment-cont_hex[] IS INITIAL.

           LOOP AT gs_attachment-cont_text INTO gs_text.

             CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

               EXPORTING

                 text   = gs_text

               IMPORTING

                 buffer = v_xstring

               EXCEPTIONS

                 failed = 1

                 OTHERS = 2.

             IF sy-subrc = 0.

               CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

                 EXPORTING

                   buffer     = v_xstring

                 TABLES

                   binary_tab = gt_hex.

               APPEND LINES OF gt_hex TO gt_hex2.

               REFRESH gt_hex.

               CLEAR : gs_text,

                       v_xstring.

             ENDIF.

           ENDLOOP.

         ENDIF.      "IF gs_attachment-cont_hex[] IS INITIAL.

*** Document size

         CLEAR v_size.

         CLEAR v_line.

         v_line = lines( gt_contents ).

         v_size = v_line * c_255.

         CONDENSE v_size.

         v_attachment_sub = gs_attachment-subject.

         TRY.

             lo_document->add_attachment(

             EXPORTING

             i_attachment_type     = v_att_type

             i_attachment_subject  = v_attachment_sub

             i_attachment_size     = v_size

             i_att_content_hex     = gt_hex2 ).

           CATCH cx_document_bcs INTO lx_document_bcs.

             ex_result = lx_document_bcs->get_text( ).

         ENDTRY.

       ENDIF.      "IF im_attachments[] IS NOT INITIAL.

************************************

*& Pass the document to send request

************************************

       lo_send_request->set_document( lo_document ).

       TRY.

           IF im_sender_mail IS NOT INITIAL.

             lo_sender = cl_cam_address_bcs=>create_internet_address( im_sender_mail ).

           ELSEIF im_sender_uid IS NOT INITIAL.

             lo_sender = cl_sapuser_bcs=>create( im_sender_uid ).

           ENDIF.

************

* Set Sender

************

           lo_send_request->set_sender(

           EXPORTING

           i_sender = lo_sender ).

         CATCH cx_address_bcs.

           RETURN.

       ENDTRY.

*****************

*& Set Recipients

*****************

       IF it_recipients[] IS INITIAL.

         IF im_recipient_mail IS NOT INITIAL.

           lo_recipient = cl_cam_address_bcs=>create_internet_address( im_recipient_mail ).

         ELSEIF im_recipient_uid IS NOT INITIAL.

           lo_recipient = cl_sapuser_bcs=>create( im_recipient_uid ).

         ENDIF.

         lo_send_request->add_recipient(

         EXPORTING

         i_recipient = lo_recipient

         i_express = 'X' ).

       ELSE.

         LOOP AT it_recipients INTO gs_recipient.

           IF gs_recipient-iusrid IS NOT INITIAL.

             v_recipient_uid = gs_recipient-iusrid.

             lo_recipient = cl_sapuser_bcs=>create( v_recipient_uid ).

           ELSEIF gs_recipient-email IS NOT INITIAL.

             v_recipient_mail = gs_recipient-email .

             lo_recipient = cl_cam_address_bcs=>create_internet_address( v_recipient_mail ).

           ENDIF.

           lo_send_request->add_recipient(

               EXPORTING

               i_recipient = lo_recipient

               i_express = 'X' ).

         ENDLOOP.

       ENDIF.      "IF it_recipients[] IS INITIAL... ELSE..

       TRY.

*************

*& Send Email

*************

           lo_send_request->send(

           EXPORTING

           i_with_error_screen = 'X'

           RECEIVING

           result = ex_result ).

           COMMIT WORK AND WAIT.

         CATCH cx_send_req_bcs INTO lx_send_req_bcs.

           ex_result = lx_send_req_bcs->get_text( ).

       ENDTRY.

*** Exception Handling (generic exception message)

     CATCH cx_bcs INTO lx_bcs_exception.

       ex_result = lx_bcs_exception->get_text( ).

   ENDTRY.

ENDFUNCTION.