메뉴 건너뛰기

SAP 한국 커뮤니티

Email 전송 Sample

양키(이경환) 2014.03.12 19:25 조회 수 : 6998

  REPORT  z_test_email.
DATA:
  subject               TYPE so_obj_des,
  it_body_text         TYPE soli_tab,
  it_body_html         TYPE soli_tab,
  from                 TYPE ad_smtpadr,
  to                   TYPE ad_smtpadr.
DATA:
  document             TYPE REF TO cl_document_bcs,
  request              TYPE REF TO cl_bcs,
  mime_helper          TYPE REF TO cl_gbt_multirelated_service,
  sender               TYPE REF TO if_sender_bcs,
  recipient            TYPE REF TO if_recipient_bcs.

to      'receiver@gmail.com'.
from    'sendor@gmail.com'.

subject 'Test Email Data'.
APPEND 'Hello World'           TO it_body_text.
APPEND 'Column 1     Column 2' TO it_body_text.
APPEND 'a            b'        TO it_body_text.
APPEND 'xyz          test'     TO it_body_text.
APPEND '4711         0815'     TO it_body_text.
APPEND '<html>'                      TO it_body_html.
APPEND '<head>'                      TO it_body_html.
APPEND '<title>MY HTML part</title>' TO it_body_html.
APPEND '</head>'                     TO it_body_html.
APPEND '<body>'                      TO it_body_html.
APPEND '<h1>Hello World!</h1>'       TO it_body_html.
APPEND '<table border="1">'          TO it_body_html.
APPEND '<tr><td>Column 1</td><td>Column 2</td></tr>' TO it_body_html.
APPEND '<tr><td>a</td><td>b</td></tr>'               TO it_body_html.
APPEND '<tr><td>xyz</td><td>test</td></tr>'          TO it_body_html.
APPEND '<tr><td>4711</td><td>0815</td></tr>'         TO it_body_html.
APPEND '</table>' TO it_body_html.
APPEND '</body>'  TO it_body_html.
APPEND '</html>'  TO it_body_html.

CREATE OBJECT mime_helper.
mime_helper->set_main_textcontent it_body_text ).
mime_helper->set_main_htmlcontent it_body_html ).
document cl_document_bcs=>create_from_multirelated(
  i_subject          subject
  i_multirel_service mime_helper ).

request cl_bcs=>create_persistent).
request->set_documentdocument ).

IF from IS NOT INITIAL.
  sender cl_cam_address_bcs=>create_internet_addressfrom ).
ELSE.
  sender cl_sapuser_bcs=>createsy-uname ).
ENDIF.

request->set_sendersender ).
recipient cl_cam_address_bcs=>create_internet_addressto ).
request->add_recipientEXPORTING i_recipient recipient ).
request->set_send_immediately'X' ).

IF request->sendi_with_error_screen 'X' 'X'.
  WRITE:'Email sent succesfully'.
ELSE.
  WRITE:' Error sending email'.
ENDIF.
COMMIT WORK.