Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending mail with attachment document type .docx / .xlsx ..

Former Member
0 Kudos

Hi all,

   In my specific requirement user can upload document in application server & later I have to send them in mail as a attachment.

I have used 'CL_BCS' to send them.

Now evrything is working fine but except new Microsoft 2007 document type ( with .x extention eg .docx ). While trying them to open from mail

system is giving unnecessary user acknowledgement message.

While checking I came to know this due to  a variable responsible for document type & this is of 3 character and cannot hold 'docx'.

Also checked  SAP Note '1459896' & tried to implement the solution but still getting same error. Any suggetion.

1 ACCEPTED SOLUTION

Former Member

Plese find the code as below :-

  DATA: l_document TYPE REF TO cl_document_bcs VALUE IS INITIAL,

  DATA: l_message TYPE bcsy_text VALUE 'Hello'.

  DATA l_mail TYPE so_obj_des VALUE 'Email with attachment '.

  DATA lv_file_path TYPE char255 VALUE ' .\TEST/Employee_CV.docx'.

.

  DATA: l_file TYPE draw-filep,           

            l_ext(6) TYPE c,

            l_type TYPE  so_obj_tp,

            l_cv TYPE so_obj_des VALUE 'Employee_CV.docx'.

,

 

  DATA: li_solix   TYPE solix_tab ,

  DATA: l_xstring TYPE xstring.

* create document

  TRY.
      l_document = cl_document_bcs=>create_document(
      i_type = 'RAW'

      i_text =  l_message
      i_subject = l_mail ).
    CATCH cx_document_bcs .
*      RAISE error_in_ia_docu_send.
  ENDTRY.

* get uploaded file details

  OPEN DATASET lv_file_path FOR INPUT  IN BINARY MODE.

    READ DATASET lv_file_path
     INTO l_xstring.

CLOSE DATASET lv_file_path.

  CALL METHOD cl_document_bcs=>xstring_to_solix
    EXPORTING
       ip_xstring = l_xstring
    receiving
       rt_solix   =  li_solix.

   MOVE lv_file_path TO l_file.

   * get file extension
  CALL FUNCTION 'CV120_SPLIT_FILE'
    EXPORTING
      pf_file       = l_file
    IMPORTING
      pfx_extension = l_ext.

  MOVE lv_name TO l_cv.
  MOVE l_ext TO l_type.

*** changes as suggested by the note ***

    DATA: lt_doc_text type bcsy_text,
               lv_text_line type soli,
               lt_att_head type soli_tab.

   * four character file extension pass the complete file name as below
  concatenate '&SO_FILENAME='
  lv_name into lv_text_line.
  append lv_text_line to lt_att_head.

    TRY.
    CALL METHOD l_document->add_attachment
      EXPORTING
        i_attachment_type        = l_type
        i_attachment_subject    = l_cv
        I_ATT_CONTENT_HEX  = li_solix
        i_attachment_header     = lt_att_head .
     CATCH cx_document_bcs .

  ENDTRY.

* get sender & receiver details & send the mail

5 REPLIES 5

Former Member

Plese find the code as below :-

  DATA: l_document TYPE REF TO cl_document_bcs VALUE IS INITIAL,

  DATA: l_message TYPE bcsy_text VALUE 'Hello'.

  DATA l_mail TYPE so_obj_des VALUE 'Email with attachment '.

  DATA lv_file_path TYPE char255 VALUE ' .\TEST/Employee_CV.docx'.

.

  DATA: l_file TYPE draw-filep,           

            l_ext(6) TYPE c,

            l_type TYPE  so_obj_tp,

            l_cv TYPE so_obj_des VALUE 'Employee_CV.docx'.

,

 

  DATA: li_solix   TYPE solix_tab ,

  DATA: l_xstring TYPE xstring.

* create document

  TRY.
      l_document = cl_document_bcs=>create_document(
      i_type = 'RAW'

      i_text =  l_message
      i_subject = l_mail ).
    CATCH cx_document_bcs .
*      RAISE error_in_ia_docu_send.
  ENDTRY.

* get uploaded file details

  OPEN DATASET lv_file_path FOR INPUT  IN BINARY MODE.

    READ DATASET lv_file_path
     INTO l_xstring.

CLOSE DATASET lv_file_path.

  CALL METHOD cl_document_bcs=>xstring_to_solix
    EXPORTING
       ip_xstring = l_xstring
    receiving
       rt_solix   =  li_solix.

   MOVE lv_file_path TO l_file.

   * get file extension
  CALL FUNCTION 'CV120_SPLIT_FILE'
    EXPORTING
      pf_file       = l_file
    IMPORTING
      pfx_extension = l_ext.

  MOVE lv_name TO l_cv.
  MOVE l_ext TO l_type.

*** changes as suggested by the note ***

    DATA: lt_doc_text type bcsy_text,
               lv_text_line type soli,
               lt_att_head type soli_tab.

   * four character file extension pass the complete file name as below
  concatenate '&SO_FILENAME='
  lv_name into lv_text_line.
  append lv_text_line to lt_att_head.

    TRY.
    CALL METHOD l_document->add_attachment
      EXPORTING
        i_attachment_type        = l_type
        i_attachment_subject    = l_cv
        I_ATT_CONTENT_HEX  = li_solix
        i_attachment_header     = lt_att_head .
     CATCH cx_document_bcs .

  ENDTRY.

* get sender & receiver details & send the mail

Hi ,

  Found the problem...

in the last call method I need to pass the xstring size. If you dont pass it , then the method will itself try to calculate it & while doing this the word file is getting corrupted. Its better to calculate the XSTRING file length  & pass it.

Add below part

   DATA: object_hd_change type sood1.

   DATA: lv_length TYPE i.

    lv_length = xstrlen( l_xstring ).

    object_hd_change-objlen = lv_length.

Now the modified 'add attachment method'

        CALL METHOD l_document->add_attachment
        EXPORTING
          i_attachment_type    = l_type   "'BIN'    " l_type
          i_attachment_subject = l_cv
          i_att_content_hex    = li_solix
          i_attachment_header  = lt_att_head
         I_ATTACHMENT_SIZE    =  object_hd_change-objlen.

Regards,

Monishankar Chatterjee


       

0 Kudos

hi monishkar,

    great answer however i write the below logic to sending excel file format xlsx to external mail. but iam unable to open this file. kindly help me

        

data lo_send_request type ref to cl_bcs.

data lo_document type ref to cl_document_bcs.

data lt_doc_text type bcsy_text.

data lt_att_text type bcsy_text.

data lt_att_head type soli_tab.

data lv_text_line type soli.

data lv_filename type string.

data sent_to_all    type os_boolean.

data binary_content type solix_tab.

*for sending mail

constants:

   gc_tab  type c value cl_bcs_convert=>gc_tab,

   gc_crlf type c value cl_bcs_convert=>gc_crlf.

   data lv_string type string.

parameters:

   mailto type ad_smtpadr

    default 'venkat.veeramaneni@sitacorp.com'.

*data send_request   type ref to cl_bcs.

*data document       type ref to cl_document_bcs.

data recipient      type ref to if_recipient_bcs.

data bcs_exception  type ref to cx_bcs.

data size           type so_obj_len.

*CONCATENATE 'Report Date' gc_tab

   CONCATENATE lv_string 'Report Date' ' :  '  sy-datum+4(2) '/' sy-datum+6(2) '/' sy-datum+0(4) gc_crlf gc_crlf INTO lv_string.

* header line

   concatenate lv_string

               'Material Number'    gc_tab

               'Material Description'    gc_tab

               'Material Type' gc_tab                        "#EC NOTEXT

               'Plant'   gc_tab

               'Date Extended to SLOC' gc_tab

               'SLOC Extension' gc_tab

               'Info Record'   gc_tab

               'Source List'   gc_tab

               'Vendor Name'   gc_crlf                       "#EC NOTEXT

               into lv_string.

try.

       cl_bcs_convert=>string_to_solix(

         exporting

           iv_string   = lv_string

           iv_codepage = '4103'  "suitable for MS Excel, leave empty

           iv_add_bom  = 'X'     "for other doc types

         importing

           et_solix  = binary_content ).

*          ev_size   = size ).

     catch cx_bcs.

       message e445(so).

   endtry.

try.

* -------- create persistent send request ------------------------

lo_send_request = cl_bcs=>create_persistent( ).

* -------- create and set document with attachment ---------------

* create document from internal table with text

append 'Hello world!' to lt_doc_text.

lo_document = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = lt_doc_text

*i_length = size

i_subject = 'Main Document'

).

* add attachment to document

append 'This is an attachment' to lt_att_text.

* four character file extension '.text' is set

lv_filename = 'AttachmentFilename.XLSX'.

concatenate '&SO_FILENAME='

lv_filename into lv_text_line.

append lv_text_line to lt_att_head.

lo_document->add_attachment( exporting

i_attachment_type = 'XLS'

i_attachment_subject = 'Attachment'

i_attachment_size = '21'

i_att_content_hex = binary_content

i_attachment_header = lt_att_head

).

* add document to send request

lo_send_request->set_document( lo_document ).

*

*     --------- add recipient (e-mail address) -----------------------

*     create recipient object

       recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

*     add recipient object to send request

       lo_send_request->add_recipient( recipient ).

*     ---------- send document ---------------------------------------

       sent_to_all = lo_send_request->send( i_with_error_screen = 'X' ).

       commit work.

       if sent_to_all is initial.

         message i500(sbcoms) with mailto.

       else.

         message s022(so).

       endif.

*   ------------ exception handling ----------------------------------

*   replace this rudimentary exception handling with your own one !!!

     catch cx_bcs into bcs_exception.

       message i865(so) with bcs_exception->error_type.

   endtry.

kat_mat
Explorer
0 Kudos

This was really helpful.  Thank you!

Former Member
0 Kudos

This message was moderated.