Skip to Content
0
Feb 22, 2016 at 01:15 PM

Problem when send mail using cl_bcs.

391 Views

Hello community.

I am sending email using cl_bcs class.

I created a call function and put my code.

But the 'commit work' within the code is stopping all standard process.

I searched the solution in scn and then i used the option in background task.

Now IS NOT sending email.

Can someone help me?

Thank you.

=========================================================

CALL FUNCTION 'ZTM_SEND_MAIL' IN BACKGROUND TASK

EXPORTING

otf = ls_job_output_info-otfdata[]

p_trqid = p_trqid

kunnr = kunnr.

=========================================================

DATA: lt_lines TYPE STANDARD TABLE OF tline,

lv_bin_filesize TYPE i,

lv_pdf_xstring TYPE xstring.

*** Selecionar Destinatários - Ini ***

TYPES: BEGIN OF ty_parid,

parid TYPE j_1bnfdoc-parid,

END OF ty_parid.

DATA:

t_parid TYPE TABLE OF ty_parid,

w_parid TYPE ty_parid,

t_j1bnfnad TYPE TABLE OF j_1bnfnad,

t_j1bnfdoc TYPE TABLE OF j_1bnfdoc,

t_kna1 TYPE TABLE OF kna1,

t_adr6 TYPE TABLE OF adr6.

* -- Convert the OTF data to xstring

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

IMPORTING

bin_filesize = lv_bin_filesize

bin_file = lv_pdf_xstring

TABLES

otf = otf[]

lines = lt_lines

EXCEPTIONS

OTHERS = 0.

* ----------------------------------------------------------

IF p_trqid IS NOT INITIAL.

IF kunnr IS NOT INITIAL.

CALL FUNCTION 'ZTM_BUSCA_ADR6'

EXPORTING

kunnr = kunnr

TABLES

lt_adr6 = t_adr6

EXCEPTIONS

not_found = 1

OTHERS = 2.

IF sy-subrc <> 0.

* Implement suitable error handling here

ENDIF.

ENDIF.

DATA:lv_docnum TYPE j_1bnflin-docnum,

ls_active TYPE j_1bnfe_active.

CONDENSE p_trqid.

SELECT SINGLE docnum

FROM j_1bnflin

INTO lv_docnum

WHERE maktx = p_trqid.

IF lv_docnum IS NOT INITIAL.

SELECT SINGLE *

FROM j_1bnfe_active

INTO ls_active

WHERE docnum = lv_docnum.

IF sy-subrc = 0.

MOVE-CORRESPONDING ls_active TO ls_acckey.

lv_access_key = ls_acckey.

ENDIF.

ENDIF.

ENDIF.

CHECK lv_pdf_xstring IS NOT INITIAL.

CLEAR: lv_text.

CONCATENATE 'Segue anexo os arquivos referente a emissão' p_trqid '.' INTO lv_text SEPARATED BY space.

APPEND lv_text TO l_body.

APPEND ' ' TO l_body.

APPEND 'Obrigado por utilizar a Azul Cargo!' TO l_body.

l_attach[] = otf[].

l_lines = lines( l_attach ).

* l_size = "l_lines * 255.

*Creates persistent send request

l_send_request = cl_bcs=>create_persistent( ).

CLEAR: lv_text.

CONCATENATE 'CT-e Azul Cargo' p_trqid INTO lv_subject_email SEPARATED BY space.

*Craete document for mail body

l_document = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = l_body

i_subject = lv_subject_email ).

*Sender addess

l_sender = cl_sapuser_bcs=>create( 'NO-REPLY' ). "Usuário NO-REPLY

CALL METHOD l_send_request->set_sender

EXPORTING

i_sender = l_sender.

SELECT SINGLE low

FROM tvarvc

INTO lv_rfcdest

WHERE name EQ 'ZMDFE_GRC_DESTINATION'.

* If connection was found

IF lv_rfcdest IS NOT INITIAL.

" Instantiate download object

CREATE OBJECT lo_download

EXPORTING

iv_xml_key = lv_access_key

iv_rfc = lv_rfcdest.

" Check the nf type

CASE ls_active-model.

WHEN 55.

lv_doctype = lc_model_nfe.

WHEN 57.

lv_doctype = lc_model_cte.

ENDCASE.

lv_direction = lc_direct_out.

" Check the nf type

lv_doctype = lc_model_cte.

lo_xml_download = cl_j_1bnfe_xml_download_da=>get_instance( ).

TRY.

lo_xml_download->xml_read(

EXPORTING

iv_xml_key = lv_access_key

iv_direction = lv_direction

iv_doctype = lv_doctype

iv_rfc_dest = lv_rfcdest

IMPORTING

et_messages = t_messages

ev_xml = xml_content ).

ENDTRY.

" Converts raw string content into DOM object

CALL FUNCTION 'SDIXML_XML_TO_DOM'

EXPORTING

xml = xml_content

IMPORTING

document = lo_dom

EXCEPTIONS

invalid_input = 1

OTHERS = 2.

" Convert DOM to XML doc (table)

CALL FUNCTION 'SDIXML_DOM_TO_XML'

EXPORTING

document = lo_dom

pretty_print = ' '

IMPORTING

xml_as_string = w_xml_string

size = w_size

TABLES

xml_as_table = t_xml

EXCEPTIONS

no_document = 1

OTHERS = 2.

l_size = w_size.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

EXPORTING

buffer = w_xml_string "xml_content

TABLES

binary_tab = t_binary_content.

CLEAR: lv_subject.

lv_subject = lv_access_key.

CALL METHOD l_document->add_attachment

EXPORTING

i_attachment_type = l_extension_xml "'XML'

i_attachment_subject = lv_subject

i_att_content_hex = t_binary_content

i_attachment_size = l_size.

*Add the document to send request

CALL METHOD l_send_request->set_document( l_document ).

FREE lo_download.

ENDIF.

*** XML - Fim ***

*** PDF - Ini ***

*Add attchment

CLEAR: lv_subject.

lv_subject = ls_acckey-nfnum9.

CALL METHOD l_document->add_attachment

EXPORTING

i_attachment_type = l_extension "'PDF'

i_attachment_subject = lv_subject

i_attachment_size = l_size

i_att_content_text = l_attach.

*Add the document to send request

CALL METHOD l_send_request->set_document( l_document ).

*** PDF - Fim ***

* Destinatário

* Mandar e-mail para cada ADR6-SMTP_ADDR.

LOOP AT t_adr6 INTO DATA(w_adr6).

l_email = w_adr6-smtp_addr.

l_recipient = cl_cam_address_bcs=>create_internet_address( l_email ).

*Add recipient address to send request

CALL METHOD l_send_request->add_recipient

EXPORTING

i_recipient = l_recipient

i_express = 'X'

i_copy = ' '

i_blind_copy = ' '

i_no_forward = ' '.

ENDLOOP.

IF t_adr6 IS INITIAL.

l_email = 'no-reply@voeazul.com.br'.

l_recipient = cl_cam_address_bcs=>create_internet_address( l_email ).

*Add recipient address to send request

CALL METHOD l_send_request->add_recipient

EXPORTING

i_recipient = l_recipient

i_express = 'X'

i_copy = ' '

i_blind_copy = ' '

i_no_forward = ' '.

ENDIF.

* trigger e-mail immediately

l_send_request->set_send_immediately( 'X' ).

* send mail

CALL METHOD l_send_request->send( ).

COMMIT WORK.

* SEND EMA