SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

To send an email with attachment ABAP



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Fri Feb 27, 2009 3:56 pm    Post subject: To send an email with attachment ABAP Reply with quote

Source from: http: /thilanka.owain.org/2008/12/to-send-email-with-attachement-abap.html
Author: Thilanka

According to my knowledge this works on SAP systems which has 4.6c higher versions.
A way to find out is by going to SE80 and entering the relevant class names and find out whether they are available in the system.
Thanks IndiaKing for pointing out the version issue.

Code:
*&---------------------------------------------------------------------*
*& Report  ZTHILANKA_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTHILANKA_TEST.


data: binary_content type solix_tab.
data: xl_content type xstring .

constants:
con_tab  type c value cl_abap_char_utilities=>horizontal_tab,
con_cret type c value cl_abap_char_utilities=>cr_lf.

data: xlcontent type xstring,
      conlength type i,
      conlengths type so_obj_len,
      result_content type string,
      wa_string type string, "This holds all of your data
      dummy type string.

data: send_request type ref to cl_bcs.
data: text type bcsy_text.
data: document type ref to cl_document_bcs.
data: sender type ref to if_sender_bcs.
data: recipient type ref to if_recipient_bcs.
data: recipients type bcsy_smtpa.
data: bcs_exception type ref to cx_bcs.
data: sent_to_all type os_boolean.

data: e_r_page type ref to cl_rsr_www_page.
data: content_length type w3param-cont_len ,
      content_type type w3param-cont_type,
      return_code type w3param-ret_code .
data: html type standard table of w3html .
data: server type string ,
      port type string .
data: wa_rec type ad_smtpadr .
data: bcs_message type string .

data: subject type so_obj_des.
data: sender_id type ad_smtpadr.
data: email type ad_smtpadr.

data: gv_file type string,
      gv_zipfilehex type xstring,
      go_zipper type ref to cl_abap_zip. "The zip folder object

concatenate 'Customer no.'
'CUSTOMER name'
'Subcustomer'
'Sales incoterms'
'Currency'
'Material'
'Mat long text'
'level1'
'level2'
'level3'
'Level 1 description'
'Level 2 description'
'Level 3 description'
'Data'
'Valid from'
'Valid to'
into wa_string separated by con_tab.
concatenate con_cret con_tab wa_string into wa_string.
* After this keep on appending the data to the wa_string
data: i type i,
      true type string.

clear: xl_content .
* convert the string data to xstring
call function 'SCMS_STRING_TO_XSTRING'
exporting
  text = wa_string
* MIMETYPE = ' '
* ENCODING =
importing
  buffer = xl_content.
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

gv_file = 'Data.xls'. "Zip file name

create OBJECT go_zipper.
go_zipper->add( name    = gv_file
                content = xl_content )."If you have other content to be added to the zip folder, do it here

gv_zipfilehex = go_zipper->save( ).


concatenate 'Data list ' 'date' into  subject .

sender_id = '[email protected]'.
email = '[email protected]'.

refresh binary_content .

call function 'SCMS_XSTRING_TO_BINARY'
exporting
  buffer     = gv_zipfilehex
tables
  binary_tab = binary_content.


*refresh text .
clear result_content .
*The body message
concatenate
'<p><font color="#000080">Dear Customer,</font></p>'
'<p><font color="#000080">Attached herewith is the data list you requested.</font></p>'

into result_content .
conlength = strlen( result_content ) .
conlengths = conlength .
call function 'SCMS_STRING_TO_FTEXT'
exporting
  text      = result_content
tables
  ftext_tab = text.



try.
  clear send_request .

  send_request = cl_bcs=>create_persistent( ).

  clear document .
  document = cl_document_bcs=>create_document(
  i_type    = 'HTM'
  i_text    = text
  i_length  = conlengths
  i_subject = subject ).

  call method document->add_attachment
  exporting
    i_attachment_type    = 'zip'
    i_attachment_subject = 'Price List' "Attachment name
    i_att_content_hex    = binary_content.


*You can add multiple attachments like below

*        CALL METHOD document->add_attachment
*          EXPORTING
*            i_attachment_type    = 'zip'
*            i_attachment_subject = 'Subject testing 2' "atta_sub
*            i_att_content_hex    = binary_content.
*
*        CALL METHOD document->add_attachment
*          EXPORTING
*            i_attachment_type    = 'zip'
*            i_attachment_subject = 'Subject 3' "atta_sub
*            i_att_content_hex    = binary_content.

*     add document to send request
  call method send_request->set_document( document ).

  clear sender .
  sender = cl_cam_address_bcs=>create_internet_address( sender_id ).
  call method send_request->set_sender
  exporting
    i_sender = sender.

  clear wa_rec .


 
*     add recipient with its respective attributes to send request
*     you can add multiple recipients in the same mail by calling
*     this method repeatedly.

 clear recipient .
  wa_rec =  email.
  recipient = cl_cam_address_bcs=>create_internet_address(
  wa_rec ).

*  call method send_request->add_recipient
*  exporting
*    i_recipient = recipient1
*    i_express   = 'X'.

*  call method send_request->add_recipient
*  exporting
*    i_recipient = recipient2
*    i_express   = 'X'.



  call method send_request->add_recipient
  exporting
    i_recipient = recipient
    i_express   = 'X'.

  call method send_request->set_status_attributes
  exporting
    i_requested_status = 'E'
    i_status_mail      = 'E'.

  call method send_request->set_send_immediately( 'X' ).
*     ---------- send document ---------------------------------------

  call method send_request->send(
  exporting
    i_with_error_screen = 'X'
    RECEIVING
    result = sent_to_all ).
  if sent_to_all = 'X'.
*APPEND 'Mail sent successfully ' TO return .
  endif.
  commit work.
catch cx_bcs into bcs_exception.
  bcs_message = bcs_exception->get_text( ).
*APPEND bcs_message TO return .
  exit.
endtry.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.