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

email through a program

Former Member
0 Likes
622

I have written a simple program for sending email through this program but its not working. I appreciate if anyone can help me with this.

Tables: zhmssecurity.

data: begin of email_data.

include structure sodocchgi1.

data: end of email_data.

data: begin of email_send occurs 10.

include structure somlreci1.

data: end of email_send.

data: begin of data_tab occurs 2,

line(255),

end of data_tab.

data: begin of object_para occurs 2.

include structure soparai1.

data: end of object_para.

data: begin of object_parb occurs 2.

include structure soparbi1.

data: end of object_parb.

select single * from zhmssecurity

where data_key = 'W'

and data_id = 'WATCHFOR'

and data = sy-uname.

if sy-subrc <> 0.

exit.

endif.

select * from zhmssecurity

where data_key = 'W'

and data_id = 'EMAIL'.

email_send-receiver = zhmssecurity-data.

email_send-rec_type = 'U'.

email_send-express = 'X'.

append email_send.

endselect.

email_data-obj_name = 'MESSAGE'.

concatenate sy-uname 'Logged on to' sy-sysid

'on' sy-datum 'at' sy-uzeit

into email_data-obj_descr separated by space.

email_data-obj_langu = 'E'.

email_data-sensitivty = 'P'.

email_data-obj_prio = '1'.

email_data-no_change = 'X'.

email_data-priority = '1'.

clear data_tab.

refresh data_tab.

clear object_para.

refresh object_para.

concatenate sy-uname 'Logged on to' sy-sysid

'client' sy-mandt into data_tab-line

separated by space.

append data_tab.

concatenate 'on' sy-datum 'at' sy-uzeit

into data_tab-line

separated by space.

append data_tab.

Move space to data_tab-line.

append data_tab.

move ' Use transaction SM20 to monitor this users activity'

to data_tab-line.

append data_tab.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

document_data = email_data

DOCUMENT_TYPE = 'RAW'

PUT_IN_OUTBOX = ' '

tables

OBJECT_CONTENT = data_tab

receivers = email_send.

IF sy-subrc EQ 0.

SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

ENDIF.

5 REPLIES 5
Read only

suresh_datti
Active Contributor
0 Likes
588

Use COMMIT ie

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = email_data
DOCUMENT_TYPE = 'RAW'
COMMIT_WORK = 'X'
PUT_IN_OUTBOX = ' '
tables
OBJECT_CONTENT = data_tab
receivers = email_send.

~Suresh

Read only

ferry_lianto
Active Contributor
0 Likes
588

Hi,

Please try to add commit work and check the sy-subrc.


...

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
  EXPORTING
    document_data = email_data
    DOCUMENT_TYPE = 'RAW'
    PUT_IN_OUTBOX = ' '
  TABLES
    OBJECT_CONTENT = data_tab
    receivers = email_send.

COMMIT WORK.

CASE SY-SUBRC.
  WHEN 0.
    SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
  WHEN 1.
    WRITE: / 'Too many receivers specified !'.
  WHEN 2.
    WRITE: / 'No receiver got the document !'.
  WHEN 4.
    WRITE: / 'Missing send authority !'.
  WHEN OTHERS.
    WRITE: / 'Unexpected error occurred !'.
ENDCASE.

Also please check whether the SAP Example from SAP note:

#190669 works? It contains an example how to use relased FM for sending mails.

If your system is above 6.10 you will need an explicite commit work. Check Note: #489286

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
588

Hi,

*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------*
*       Send email message
*----------------------------------------------------------------------*
form send_email_message.
* Fill the document data.
  gd_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  gd_doc_data-obj_langu = sy-langu.
  gd_doc_data-obj_name  = 'SAPRPT'.
  gd_doc_data-obj_descr = psubject.
  gd_doc_data-sensitivty = 'F'.

* Describe the body of the message
  clear it_packing_list.
  refresh it_packing_list.
  it_packing_list-transf_bin = space.
  it_packing_list-head_start = 1.
  it_packing_list-head_num = 0.
  it_packing_list-body_start = 1.
  describe table it_message lines it_packing_list-body_num.
  it_packing_list-doc_type = 'RAW'.
  append it_packing_list.

* Add the recipients email address
  clear it_receivers.
  refresh it_receivers.
  it_receivers-receiver = p_email.
  it_receivers-rec_type = 'U'.
  it_receivers-com_type = 'INT'.
  it_receivers-notif_del = 'X'.
  it_receivers-notif_ndel = 'X'.
  append it_receivers.

* Call the FM to post the message to SAPMAIL
  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
       exporting
            document_data              = gd_doc_data
            put_in_outbox              = 'X'
       importing
            sent_to_all                = gd_sent_all
       tables
            packing_list               = it_packing_list
            contents_txt               = it_message
            receivers                  = it_receivers
       exceptions
            too_many_receivers         = 1
            document_not_sent          = 2
            document_type_not_exist    = 3
            operation_no_authorization = 4
            parameter_error            = 5
            x_error                    = 6
            enqueue_error              = 7
            others                     = 8.

* Store function module return code
  gd_error = sy-subrc.

* Get it_receivers return code
  loop at it_receivers.
  endloop.
endform.                    " SEND_EMAIL_MESSAGE


*&---------------------------------------------------------------------*
*&      Form  INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------*
*       Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------*
form initiate_mail_execute_program.
  wait up to 2 seconds.
  if gd_error eq 0.
      submit rsconn01 with mode = 'INT'
                    with output = 'X'
                    and return.
  endif.
endform.                    " INITIATE_MAIL_EXECUTE_PROGRAM

Regards

Sudheer

Read only

0 Likes
588

thanks everyone for your patience . It worked with commit work.

Read only

ferry_lianto
Active Contributor
0 Likes
588

Hi,

Please close the thread if your problem solved.

Regards,

Ferry Lianto