Sunday, March 15, 2009

Email from SAP - class replaces SO_OBJECT_SEND

SO_OBJECT_SEND has been replaced by CL_BCS and CL_DOCUMENT_BCS. Less code is required to create and send email messages through Business Communication Services aka SAPOffice and SAPconnect. Here is a simple example of sending an email message from NetWeaver using the classes.

Full documentation is available on SDN here.

Note: to send email from SAP your user profile (system > user profile > own data) must have an email address set up so the return address can be detemined.


* Send email from SAP using BCS classes
* Peter Chapman - March 2009 - Final

REPORT zr_email.

DATA:
lv_message TYPE bcsy_text,
lv_send_result TYPE c,

lo_receiver TYPE REF TO if_recipient_bcs,

lo_email TYPE REF TO cl_bcs,
lo_email_body TYPE REF TO cl_document_bcs,

lx_exception TYPE REF TO cx_bcs.

APPEND '<font color="#0000FF">Your message is reddy!</font>' TO lv_message.

TRY.
lo_email = cl_bcs=>create_persistent( ).

lo_email_body = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = lv_message
i_subject = 'Message from SAP BCS' ).

PERFORM add_attachment USING lo_email_body.

lo_email->set_document( lo_email_body ).

lo_receiver = cl_cam_address_bcs=>create_internet_address( 'someone@somewhere.com' ).
lo_email->add_recipient( i_recipient = lo_receiver
i_express = 'X' ).

lo_email->set_send_immediately( 'X' ).

lo_email->send( EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = lv_send_result ).

WRITE: / 'Success flag:', lv_send_result.

COMMIT WORK.

CATCH cx_bcs INTO lx_exception.

WRITE:/ 'Message sending failed:', lx_exception->error_type.

ENDTRY.


*&-----------------------------*
*& Form add_attachment
*&-----------------------------*

FORM add_attachment USING po_attachee TYPE REF TO cl_document_bcs.

DATA:
lt_attachment TYPE soli_tab,
lx_exception TYPE REF TO cx_bcs.

APPEND '<html><head/><body><font color="#0000FF">Your attachment is bluey!</font></body></html>' TO lt_attachment.

TRY.

po_attachee->add_attachment(
EXPORTING
i_attachment_type = 'HTM'
i_attachment_subject = 'My Attachment'
i_att_content_text = lt_attachment ).

CATCH cx_bcs INTO lx_exception.

WRITE:/ 'Attachment failed:', lx_exception->error_type.

ENDTRY.

ENDFORM. "add_attachment

3 comments:

Rajanya said...

Thanks for the sample code.

Unknown said...

Well, guess what? You CAN be a certified SAP consultant fast...and it's not illegal or even the slightest bit shady. Trust me, this is unlike anything you've ever seen before. I've used this same step-by-step system I'm sharing with you that transform SAP trainees to professional consultants. Get a free copy here : http://www.ultimatesap.co.cc

Unknown said...

Hi..

Thank you very much.. code is working fine..
but I want to send a email without any attachment (body has to be in HTML format).

when I remove the attachment from the code, body only appear as simple text in the mail.

Need help.

Thank you