SAP ABAP EMAIL
From SapWiki
EJEMPLO 1
r_subrc = zcl_hcm_class=>send_mail( i_correouser = lt_correos[] i_text = lt_text[] i_subject = l_subject i_sender = 'Remuneraciones@sapwiki.cl' i_copy = lt_copy ). METHOD send_mail. *I_CORREOUSER Importing Type BCSY_SMTPA *I_TEXT Importing Type BCSY_TEXT *I_SUBJECT Importing Type SO_OBJ_DES *I_MAILHEX Importing Type SOLIX_TAB op *I_ATTACH_NAME Importing Type STRING op *I_SENDER Importing Type AD_SMTPADR op *I_COPY Importing Type BCSY_SMTPA op *R_SUBRC Returning Type SYST_SUBRC ********************************************************************** DATA: send_request TYPE REF TO cl_bcs, text TYPE bcsy_text, binary_content TYPE solix_tab, document TYPE REF TO cl_document_bcs, sender TYPE REF TO cl_sapuser_bcs, recipient TYPE REF TO if_recipient_bcs, bcs_exception TYPE REF TO cx_bcs, sent_to_all TYPE os_boolean, document_data TYPE sofolenti1, doc_id TYPE sofolenti1-doc_id, cont_text TYPE soli_tab, attachment TYPE REF TO if_document_bcs, lv_attach_name TYPE sood-objdes, l_sender TYPE REF TO if_sender_bcs, l_message TYPE string, l_correouser TYPE ad_smtpadr, l_address_name TYPE ad_smtpadr. DATA: ld_addr TYPE adr6-smtp_addr, internet_sender TYPE REF TO if_sender_bcs. text[] = i_text[]. TRY. * -------- create persistent send request ------------------------ send_request = cl_bcs=>create_persistent( ). * -------- create and set document with attachment --------------- * create document from internal table with text document = cl_document_bcs=>create_document( i_type = 'HTM' i_text = i_text * i_length = '12' i_subject = i_subject ). * add attachment IF i_attach_name <> space. lv_attach_name = i_attach_name. CALL METHOD document->add_attachment EXPORTING i_attachment_type = 'BIN' i_attachment_subject = lv_attach_name i_att_content_hex = i_mailhex. ENDIF. * add document to send request CALL METHOD send_request->set_document( document ). * --------- add recipient (e-mail address) ----------------------- * create recipient LOOP AT i_correouser INTO l_correouser. l_address_name = l_correouser. recipient = cl_cam_address_bcs=>create_internet_address( l_address_name ). * add recipient with its respective attributes to send request CALL METHOD send_request->add_recipient EXPORTING i_recipient = recipient i_express = 'X'. ENDLOOP. LOOP AT i_copy INTO l_correouser. l_address_name = l_correouser. recipient = cl_cam_address_bcs=>create_internet_address( l_address_name ). * add recipient with its respective attributes to send request CALL METHOD send_request->add_recipient EXPORTING i_recipient = recipient i_express = 'X' i_copy = abap_true. ENDLOOP. ** Crear 'Enviado por:" * IF p_from IS NOT INITIAL. * CALL METHOD cl_cam_address_bcs=>create_internet_address * EXPORTING * i_address_string = p_from * i_address_name = p_from_name * RECEIVING * result = l_sender. * * send_request->set_sender( l_sender ). * CLEAR l_sender. * ENDIF. * --------- set sender ------------------------------------------- * note: this is necessary only if you want to set the sender * different from actual user (SY-UNAME). Otherwise sender is * set automatically with actual user. IF i_sender IS NOT INITIAL. ld_addr = i_sender. "'Remuneraciones@sapwiki.cl'. internet_sender = cl_cam_address_bcs=>create_internet_address( i_address_string = ld_addr ). CALL METHOD send_request->set_sender EXPORTING i_sender = internet_sender. ENDIF. * ---------- send document --------------------------------------- send_request->send_request->set_link_to_outbox( 'X' ). CALL METHOD send_request->send( EXPORTING i_with_error_screen = 'X' RECEIVING result = sent_to_all ). IF sent_to_all = 'X'. * WRITE text-003. ENDIF. COMMIT WORK. r_subrc = 0. * ----------------------------------------------------------- * * exception handling * ----------------------------------------------------------- * * replace this very rudimentary exception handling * * with your own one !!! * ----------------------------------------------------------- CATCH cx_bcs INTO bcs_exception. r_subrc = 4. EXIT. ENDTRY. ENDMETHOD.
Ejemplo 2 Enviar email con imagenes
REPORT zenvio_mail_html. DATA: lt_html TYPE soli_tab, ls_html TYPE soli, lt_solix_tab TYPE solix_tab, ls_htmlx TYPE solix, l_url TYPE so_text255, l_file_length TYPE i, l_string TYPE string. DATA: lo_bcs TYPE REF TO cl_bcs, lo_doc_bcs TYPE REF TO cl_document_bcs, lo_mime_helper TYPE REF TO cl_gbt_multirelated_service, lo_recipient TYPE REF TO if_recipient_bcs, lo_address TYPE REF TO cl_cam_address_bcs. DATA : gv_mr_api TYPE REF TO if_mr_api, "mime repository object l_xstring TYPE xstring, "image in XSTRING is_folder TYPE boole_d, b64data TYPE string, l_loio TYPE skwf_io. DATA: l_subject TYPE so_obj_des, l_username TYPE uname, l_mail_address TYPE ad_smtpadr. DATA: l_send_exception TYPE REF TO cx_root, l_reason TYPE string. * Attach image to HTML body DATA: l_filename TYPE string, l_content_id TYPE string. "for HTML content DATA : l_obj_len TYPE so_obj_len. CREATE OBJECT lo_mime_helper. *--- Imagen uno IF gv_mr_api IS INITIAL. gv_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ). ENDIF. * crear via trn. se80 => Repository MIME, SAP=>PUBLIC CALL METHOD gv_mr_api->get EXPORTING i_url = '/SAP/PUBLIC/mi_imagen.jpg' "image path IMPORTING e_is_folder = is_folder e_content = l_xstring e_loio = l_loio EXCEPTIONS parameter_missing = 1 error_occured = 2 not_found = 3 permission_failure = 4 OTHERS = 5. CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = l_xstring IMPORTING output_length = l_file_length TABLES binary_tab = lt_solix_tab. * Attach image to HTML body l_filename = 'mi_imagen.jpg'. l_content_id = 'mi_imagen.jpg'. l_obj_len = l_file_length. CALL METHOD lo_mime_helper->add_binary_part EXPORTING content = lt_solix_tab " Xstring in table form filename = l_filename " file name to be given to image extension = 'JPG' " type of file description = 'Graphic in JPG format' " description content_type = 'image/jpg' " content type / Mime type. If mime type not present in system then need to add through tcode : SMW0 length = l_obj_len " length of image content_id = l_content_id. " content id would be used in html part ********************************************************************** * crear HTML ********************************************************************** lt_html = value #( ( LINE = '<!DOCTYPE html>') ( LINE = '<HTML>') ( LINE = '<HEAD>') ( LINE = '<TITLE>Mi titulo</TITLE>') ( LINE = '</HEAD>') ( LINE = '<BODY>') ( line = '<p class=Default align=center>') ( line = '<IMG alt="[image]" src="cid:mi_imagen.jpg"/>') ( line = '</p>') ( LINE = '<H1>Rechazo el Pago</H1>') ( LINE = 'Estimado Representante de la Empresa <P>') ( LINE = 'Ud. esta rechazado <P>') ( LINE = '</BODY></HTML>') ). BREAK-POINT. ********************************************************************** * ********************************************************************** TRY. l_subject = 'Rechazo'(t01). CALL METHOD lo_mime_helper->set_main_html EXPORTING content = lt_html " filename = 'sap.htm' " file name to be given to image description = 'Imagen'. " description. " content id would be used in html part lo_doc_bcs = cl_document_bcs=>create_from_multirelated( i_subject = l_subject i_multirel_service = lo_mime_helper ). CATCH cx_document_bcs INTO l_send_exception. * error handling * write: / 'Fehler in cl_document_bcs=>create_from_multirelated()'(001). l_reason = l_send_exception->get_text( ). WRITE: / l_reason. EXIT. CATCH cx_bcom_mime INTO l_send_exception. * error handling * write: / 'Fehler in cl_document_bcs=>create_from_multirelated()'(002). l_reason = l_send_exception->get_text( ). WRITE: / l_reason. EXIT. CATCH cx_gbt_mime INTO l_send_exception. * error handling * write: / 'Fehler in cl_document_bcs=>create_from_multirelated()'(003). l_reason = l_send_exception->get_text( ). WRITE: / l_reason. EXIT. ENDTRY. * create send_request TRY. lo_bcs = cl_bcs=>create_persistent( ). CATCH cx_send_req_bcs INTO l_send_exception. * error handling * write: / 'Fehler in cl_bcs=>create_persistent()'(004). l_reason = l_send_exception->get_text( ). WRITE: / l_reason. EXIT. ENDTRY. TRY. lo_bcs->set_document( i_document = lo_doc_bcs ). CATCH cx_send_req_bcs INTO l_send_exception. * error handling * write: / 'Fehler in <cl_bcs>->set_document()'(005). l_reason = l_send_exception->get_text( ). WRITE: / l_reason. EXIT. ENDTRY. l_mail_address = 'pablo.tapia@polosur.cl'. TRY. lo_address = cl_cam_address_bcs=>create_internet_address( l_mail_address ). CATCH cx_address_bcs. * "Error al crear objeto de destinatario RETURN. ENDTRY. * TRY. lo_bcs->add_recipient( lo_address ). CATCH cx_send_req_bcs. * "Error al agregar destinatario a la lista RETURN. ENDTRY. * --------- set sender ------------------------------------------- * note: this is necessary only if you want to set the sender * different from actual user (SY-UNAME). Otherwise sender is * set automatically with actual user. DATA: ld_addr TYPE adr6-smtp_addr, lo_internet_sender TYPE REF TO if_sender_bcs. l_mail_address = 'ejecutivo@sapwiki.cl'. TRY. lo_internet_sender = cl_cam_address_bcs=>create_internet_address( i_address_string = l_mail_address ). CATCH cx_address_bcs . ENDTRY. TRY. CALL METHOD lo_bcs->set_sender EXPORTING i_sender = lo_internet_sender. CATCH cx_send_req_bcs . ENDTRY. *--------------------------------------------------------------------* TRY. lo_bcs->set_send_immediately( abap_true ). CATCH cx_send_req_bcs. * "Error al marcar el correo como envio inmediato RETURN. ENDTRY. * TRY. "Enviar con error en pantalla DATA(l_result) = lo_bcs->send( abap_true ). CATCH cx_send_req_bcs. * "Error al setear el envío de correo RETURN. ENDTRY. IF l_result EQ abap_true. "Se confirma el envío, es necesario. COMMIT WORK. ENDIF.