Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Solved - Re - Send Mail With XML Document Attached - SAP Community

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

Send Mail with XML document attached

Go to solution
Former Member

‎10-19-2009 8:47 AM

0 Kudos

Hi ,

My requirement is like i need to send a mail attaching XML document. i am able to receive attchment but XML file is
not opening.

its below mesage displying

"Invalid at the top level of the document. " pls suggest.

Regards,

Rajkumar

SAP Managed Tags:


ABAP Development

Reply 1 ACCEPTED
SOLUTION

Former Member

‎10-22-2009 8:36 AM

0 Kudos

Raj,

Create the FM with SO_XML with out any code in the FM and add the entry in the table TSOTD.

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 1/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

http://help.sap.com/erp2005_ehp_04/helpdata/EN/6c/69c3c5418d11d1896e0000e8322d00/frameset.htm

http://www.sapdb.info/abap-program-to-send-mail-with-attachment/

I tried it.. it is working fine.


SAP Managed Tags:
ABAP Development

Reply 6 REPLIES

athavanraja
Active Contributor

‎10-20-2009 6:49 AM

0 Kudos

am able to receive attchment but XML file is not opening.

its below mesage displying

"Invalid at the top level of the document. " pls suggest.

This means that the xml is malformed.

regarding sending mail with XML attachment check out the following code sample (in FM format)

import parameters:

SENDER_ID TYPE AD_SMTPADR


SUBJECT TYPE SO_OBJ_DES

tables parameter

RECEPIENTS TYPE BCSY_SMTPA


RETURN TYPE TABLE_OF_STRINGS

top include data declaration

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 2/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

DATA: send_request TYPE REF TO cl_bcs.


DATA: text TYPE bcsy_text.
data: xtext type standard table of solix .
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: bcs_exception TYPE REF TO cx_bcs.
DATA: sent_to_all TYPE os_boolean.
DATA: conlength TYPE i ,
conlengths TYPE so_obj_len ,
result_content TYPE string .

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 .

FM Source.

FUNCTION y_email_xml_atta.

DATA: binary_content TYPE solix_tab.


DATA: xl_content TYPE xstring .
DATA: atta_sub TYPE sood-objdes .

data: file_data TYPE string ,


output TYPE string .
DATA: flights TYPE flighttab .

IF NOT recepients[] IS INITIAL .


SELECT * FROM sflight INTO TABLE flights .

CALL TRANSFORMATION (`ID`)


SOURCE flights = flights[]
RESULT XML output.

CLEAR: xl_content .

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'


EXPORTING
text = output

IMPORTING
buffer = xl_content
EXCEPTIONS
https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 3/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

failed = 1
OTHERS = 2
.
IF sy-subrc <> 0.

ENDIF.

REFRESH binary_content .

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'


EXPORTING
buffer = xl_content
TABLES
binary_tab = binary_content.

REFRESH text .
CLEAR result_content .

CONCATENATE
'Please find attached your XML doc.'
'Regards'
' Team<' 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 = 'XML'
i_attachment_subject = atta_sub
i_att_content_hex = binary_content.

CALL METHOD send_request->set_document( document ).

CLEAR sender .
https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 4/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

sender = cl_cam_address_bcs=>create_internet_address( sender_id ).


CALL METHOD send_request->set_sender
EXPORTING
i_sender = sender.

CLEAR wa_rec .

LOOP AT recepients INTO wa_rec .


CLEAR recipient .

recipient = cl_cam_address_bcs=>create_internet_address(
wa_rec ).

CALL METHOD send_request->add_recipient


EXPORTING
i_recipient = recipient
i_express = 'X'.
ENDLOOP .
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = 'E'
i_status_mail = 'E'.

CALL METHOD send_request->set_send_immediately( 'X' ).

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.

ELSE .

APPEND 'Specify email address for sending' TO return .


ENDIF .

ENDFUNCTION.

Edited by: Durairaj Athavan Raja on Oct 20, 2009 8:54 AM


SAP Managed Tags:

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 5/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community
ABAP Development

Reply

Former Member

In response to athavanraja
‎10-20-2009 8:46 AM

0 Kudos

Hi Raja,

i tried your code by creating new function module , i got attchment but XML file not opening stillfacing same
error .

i opened XML file with notepad , contents are shown below .do we need to mention any separator ?

please suggest.

#<?xml version="1.0" encoding="utf-16"?>

<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values><FLIGHTS><SFLIGHT>


<MANDT>300</MANDT><CARRID>AC</CARRID><CONNID>0820</CONNID><FLDATE>2002-12-
20</FLDATE><PRICE>1222.0</PRICE><CURRENCY>CAD</CURRENCY><PLANETYPE>A330-
300</PLANETYPE><SEATSMAX>320</SEATSMAX><SEATSOCC>12</SEATSOCC>
<PAYMENTSUM>0.0</PAYMENTSUM><SEATSMAX_B>20</SEATSMAX_B>
<SEATSOCC_B>1</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT>
<CARRID>AF</CARRID><CONNID>0820</CONNID><FLDATE>2002-12-23</FLDATE>
<PRICE>2222.0</PRICE><CURRENCY>EUR</CURRENCY><PLANETYPE>A330-300</PLANETYPE>
<SEATSMAX>320</SEATSMAX><SEATSOCC>2</SEATSOCC><PAYMENTSUM>0.0</PAYMENTSUM>
<SEATSMAX_B>20</SEATSMAX_B><SEATSOCC_B>1</SEATSOCC_B>
<SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT>
<MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>0400</CONNID><FLDATE>1995-02-
28</FLDATE><PRICE>899.0</PRICE><CURRENCY>DEM</CURRENCY>
<PLANETYPE>A319</PLANETYPE><SEATSMAX>350</SEATSMAX><SEATSOCC>3</SEATSOCC>
<PAYMENTSUM>2639.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B>
<SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT>

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 6/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

<CARRID>LH</CARRID><CONNID>0454</CONNID><FLDATE>1995-11-17</FLDATE>
<PRICE>1499.0</PRICE><CURRENCY>DEM</CURRENCY><PLANETYPE>A319</PLANETYPE>
<SEATSMAX>350</SEATSMAX><SEATSOCC>2</SEATSOCC>
<PAYMENTSUM>2949.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B>
<SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT>
<CARRID>LH</CARRID><CONNID>0455</CONNID><FLDATE>1995-06-06</FLDATE>
<PRICE>1090.0</PRICE><CURRENCY>USD</CURRENCY><PLANETYPE>A319</PLANETYPE>
<SEATSMAX>220</SEATSMAX><SEATSOCC>1</SEATSOCC>
<PAYMENTSUM>1499.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B>
<SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT>
<CARRID>LH</CARRID><CONNID>3577</CONNID><FLDATE>1995-04-28</FLDATE>
<PRICE>6000.0</PRICE><CURRENCY>LIT</CURRENCY><PLANETYPE>A319</PLANETYPE>
<SEATSMAX>220</SEATSMAX><SEATSOCC>1</SEATSOCC>
<PAYMENTSUM>600.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B>
<SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT>
<CARRID>LH</CARRID><CONNID>9981</CONNID><FLDATE>2002-12-21</FLDATE>
<PRICE>222.0</PRICE><CURRENCY>EUR</CURRENCY><PLANETYPE>A330-300</PLANETYPE>
<SEATSMAX>320</SEATSMAX><SEATSOCC>12</SEATSOCC><PAYMENTSUM>0.0</PAYMENTSUM>
<SEATSMAX_B>20</SEATSMAX_B><SEATSOCC_B>1</SEATSOCC_B>
<SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT>
<MANDT>300</MANDT><CARRID>SQ</CARRID><CONNID>0026</CONNID><FLDATE>1995-02-
28</FLDATE><PRICE>849.0</PRICE><CURRENCY>DEM</CURRENCY><PLANETYPE>DC-10-
10</PLANETYPE><SEATSMAX>380</SEATSMAX><SEATSOCC>2</SEATSOCC>
<PAYMENTSUM>1684.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B>
<SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F>
<SEATSOCC_F>0</SEATSOCC_F></SFLIGHT></FLIGHTS></asx:values></asx:abap>
SAP Managed Tags:
ABAP Development

Reply

Former Member

In response to athavanraja
‎10-21-2009 11:06 AM

0 Kudos

Hi Raja,

Earlier problem was Invalid at the top level of the document

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 7/8
27/4/24, 19:57 Solved: Re: Send Mail with XML document attached - SAP Community

it is due to XML header #<?xml version="1.0" encoding="utf-16"?>

now i have added OPTIONS addition to the CALL TRANSFORMATION statement to avoid header

Now The syntax is

CALL TRANSFORMATION (`ID`) OPTIONS XML_HEADER = 'no'

SOURCE flights = flights[]

RESULT XML output.

but now the header coming like #<asx:abap xmlns:asx="http://www.sap.com/abapxml"


version="1.0">

because of # key it is not able to open the file.still message Invalid at the top level of the
document is coming

after CALL TRANSFORMATION statement in output parameter There is no # at the begining .

Please suggest how to delete # .

Regards,

Rajkumar

Edited by: Raj Kumar on Oct 21, 2009 12:08 PM


SAP Managed Tags:
ABAP Development

https://community.sap.com/t5/application-development-discussions/send-mail-with-xml-document-attached/m-p/6231876#M1380732 8/8

You might also like