Sample code to send mail from SAP

A sample program to send mail from SAP to Outlook with a text on body not at attachment.











report zh_email1.
data: email type somlreci1-receiver value ''.
data: sender type soextreci1-receiver value 'SAPDEV02'.

data: imessage type standard table of solisti1 with header line,
iattach type standard table of solisti1 with header line,
ipacking_list like sopcklsti1 occurs 0 with header line,
ireceivers like somlreci1 occurs 0 with header line.

start-of-selection.

clear imessage. refresh imessage.
imessage = 'This is a mail from SAP ECC6'.
append imessage.
imessage = 'Thanks and Regards'.
append imessage.
imessage = 'xyzabax'.
append imessage.

perform send_email tables imessage
using email
'Mail from xyzbax'.

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

* Form SEND_EMAIL

**************************************************************
*
form send_email tables pit_message
using email
p_mtitle.

data: xdocdata like sodocchgi1,
xcnt type i.

* Fill the document data.

xdocdata-doc_size = 1.

* Populate the subject/generic message attributes

xdocdata-obj_langu = sy-langu .
xdocdata-obj_name = 'SAPRPT' .
xdocdata-obj_descr = p_mtitle .


clear ipacking_list. refresh ipacking_list.
ipacking_list-transf_bin = space.
ipacking_list-head_start = 1.
ipacking_list-head_num = 0.
ipacking_list-body_start = 1.
describe table imessage lines ipacking_list-body_num.
ipacking_list-doc_type = 'RAW'.
append ipacking_list.


clear ireceivers.
refresh ireceivers.
ireceivers-receiver = email.
ireceivers-rec_type = 'U'.

append ireceivers.
call function 'SO_DOCUMENT_SEND_API1'
exporting
document_data = xdocdata
put_in_outbox = 'X'
sender_address = sender
commit_work = 'X'
tables
packing_list = ipacking_list
contents_txt = imessage
receivers = ireceivers
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.


*** These two statemnets are used to force the
mail to send it to the
*receipeint otherwise we need to go to SOST
tcode where we need to press
* F8 to send the mail to the other user.
To avoid this we need to use
*these two statemnets. Here the mail is not in queue.

submit rsconn01 using selection-set 'INT' and return.
call function 'SO_DEQUEUE_UPDATE_LOCKS'.
* SUBMIT rsconn01 WITH mode = 'INT'
* WITH output = 'X'
* AND RETURN.

endform.

No comments:

Post a Comment