Advertisement
Guest User

ABAP Method to send email with attachment easy

a guest
May 18th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 4.36 KB | None | 0 0
  1. ****** zca_email_content DDIC defintion ************
  2. SENDER  SYUNAME
  3. SUBJECT SO_OBJ_DES
  4. BODY_T  BCSY_TEXT
  5. ATTACH_NAME SO_OBJ_DES
  6. ATTACH_TYPE CHAR3
  7.  
  8.  
  9. ****** PUBLIC CLASS SECTION ************
  10. class zcl_ca_tools definition
  11.   public
  12.   final
  13.   create public .
  14.  
  15. *"* public components of class ZCL_CA_TOOLS
  16. *"* do not include other source files here!!!
  17. public section.
  18.  
  19.   type-pools abap .
  20.   methods send_email_attach
  21.     importing
  22.       !is_email_content type zca_email_content
  23.       !it_recipients_smtp type isu_adr6_tab optional
  24.       !it_recipients_sapid type uiys_iusr optional
  25.       !it_attach type solix_tab optional
  26.       !ir_attach_ref type ref to solix_tab optional
  27.     returning
  28.       value(rv_result) type abap_bool
  29.     raising
  30.       cx_address_bcs
  31.       cx_send_req_bcs .
  32.  
  33.  
  34. ****** Method ************
  35.  
  36. method send_email_attach.
  37.  
  38. ********************************************************************************
  39. * sends mail either as BCS or SMTP-EMAIL depending on the parameter supplied
  40. ********************************************************************************
  41.  
  42. *Data Declaration
  43.    data: lo_sender           type ref to if_sender_bcs value is initial,
  44.          lo_send_request     type ref to cl_bcs value is initial,
  45.          lo_recipient        type ref to if_recipient_bcs value is initial,
  46.          lo_document         type ref to cl_document_bcs value is initial,
  47.          ls_recipient_smtp   type adr6,
  48.          lv_recipient_uid    type uname.
  49.  
  50.    clear: ls_recipient_smtp, lv_recipient_uid.
  51.  
  52. * Prepare Mail Object
  53.    class cl_bcs definition load.
  54.    try.
  55.        lo_send_request = cl_bcs=>create_persistent( ).
  56.      catch cx_send_req_bcs.
  57.    endtry.
  58.  
  59. * Message body and subject
  60.    try.
  61.        lo_document = cl_document_bcs=>create_document(
  62.          i_type = 'RAW'
  63.          i_text =  is_email_content-body_t
  64.          i_subject = is_email_content-subject ).
  65.  
  66.        if it_attach is supplied.
  67.          lo_document->add_attachment(
  68.              i_attachment_type     = is_email_content-attach_type
  69.              i_attachment_subject  = is_email_content-attach_name
  70.              i_att_content_hex     = it_attach
  71.                 ).
  72.        elseif ir_attach_ref is supplied.
  73.          if ir_attach_ref is bound.
  74.            lo_document->add_attachment(
  75.                i_attachment_type     = is_email_content-attach_type
  76.                i_attachment_subject  = is_email_content-attach_name
  77.                i_att_content_hex     = ir_attach_ref->*[]
  78.                    ).
  79.          endif.
  80.        endif.
  81.      catch cx_document_bcs .
  82.    endtry.
  83.  
  84. * Pass the document to send request
  85.    try.
  86.        lo_send_request->set_document( lo_document ).
  87.      catch cx_send_req_bcs.
  88.    endtry.
  89.  
  90. * Set Sender
  91.    if is_email_content-sender is not initial.
  92.      try.
  93.          lo_sender = cl_sapuser_bcs=>create( is_email_content-sender ).
  94.        catch cx_address_bcs.
  95.      endtry.
  96.    else.
  97.      try.
  98.          lo_sender = cl_sapuser_bcs=>create( sy-uname ).
  99.        catch cx_address_bcs.
  100.      endtry.
  101.    endif.
  102.    try.
  103.        lo_send_request->set_sender(
  104.          exporting
  105.            i_sender = lo_sender ).
  106.      catch cx_send_req_bcs.
  107.        return.
  108.    endtry.
  109.  
  110. * Set  recipients
  111. * if IT_RECIPIENTS_SMTP is supplied, we do not process IT_RECIPIENTS_SAPID
  112.    if it_recipients_smtp is not initial.
  113.      loop at it_recipients_smtp into ls_recipient_smtp.
  114.        try.
  115.            lo_recipient = cl_cam_address_bcs=>create_internet_address( ls_recipient_smtp-smtp_addr ).
  116.          catch cx_address_bcs.
  117.        endtry.
  118.        try.
  119.            lo_send_request->add_recipient(
  120.                i_recipient = lo_recipient
  121.                i_express = abap_true ).
  122.          catch cx_send_req_bcs.
  123.        endtry.
  124.      endloop.
  125.    else.
  126.      if it_recipients_sapid is not initial.
  127.        lv_recipient_uid = it_recipients_sapid-iusrid.
  128.        lo_recipient = cl_sapuser_bcs=>create( lv_recipient_uid ).
  129.  
  130.        lo_send_request->add_recipient(
  131.            i_recipient = lo_recipient
  132.            i_express = abap_true ).
  133.      endif.
  134.    endif.
  135.  
  136. * Send email
  137.    try.
  138.        lo_send_request->set_send_immediately( abap_true ).
  139.        rv_result = lo_send_request->send( i_with_error_screen = abap_true ).
  140.      catch cx_send_req_bcs.
  141.        rv_result = ''.
  142.    endtry.
  143.  
  144. * dont forget to COMMIT WORK AND WAIT if the system does not do it for you
  145.  
  146.  endmethod.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement