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 email with html content in abap

Hello. I wrote a program that sends HTML content. In sost is displayed properly but when it's on the recipient side in Outlook/gmail the content is not shown as HTML.Is there some settings in Email server?

    DATA(lt_message) = cl_bcs_convert=>string_to_soli( iv_message ).

    IF lt_message IS INITIAL.
        RETURN.
    ENDIF.

    TRY.
        " email body
        DATA(lo_document) =
          cl_document_bcs=>create_document(
          i_type    = 'HTM'
          i_text    = lt_message
          i_subject = CONV #( iv_subject ) ).

        " sending
        DATA(lo_send_request) = cl_bcs=>create_persistent( ).
        lo_send_request->set_document( lo_document ).
        lo_send_request->set_message_subject( iv_subject ).

*        get a sender and receiver
        mo_email_assist->read_sender_recepient(
          EXPORTING
            iv_system_key = mv_system_key
          IMPORTING
            ev_sender     = DATA(lv_sender)
            ev_receiver   = DATA(lv_receiver)
        ).
        FIND '@' IN lv_receiver.

        IF sy-subrc = 0.
          DATA lo_recipient      TYPE REF TO if_recipient_bcs.

          lo_recipient ?=
            cl_cam_address_bcs=>create_internet_address( CONV #( lv_receiver ) ).
        ELSE.
          lo_recipient ?=
            cl_sapuser_bcs=>create( CONV #( lv_receiver ) ).
        ENDIF.

        lo_send_request->add_recipient(
          i_recipient  = lo_recipient
          i_blind_copy = abap_false ).


        " Sender


        FIND '@' IN lv_sender.
        DATA lo_sender TYPE REF TO if_sender_bcs.
        IF sy-subrc = 0.
          lo_sender ?=
            cl_cam_address_bcs=>create_internet_address( CONV #( lv_sender ) ).
        ELSE.
          lo_sender ?=
              cl_sapuser_bcs=>create( CONV #( lv_sender ) ).
        ENDIF.
        lo_send_request->set_sender( lo_sender ).


        lo_send_request->set_status_attributes(
          i_requested_status = 'E'    " return only error statuses
          i_status_mail      = 'E' ). " send status mail only in case of errors

*        lo_send_request->set_send_immediately( abap_true ).
        lo_send_request->send( ).

      CATCH cx_bcs INTO DATA(lx_bcs).
        RAISE EXCEPTION TYPE y03cacx_cnr_static_check
            MESSAGE e245(y03mmmsg_fnd) WITH lx_bcs->get_text( ).
    ENDTRY. 
4 REPLIES 4

abo
Active Contributor
0 Kudos

Oh, a question with proper formatting and modern ABAP, thank you for the welcome change 🙂

Back to your question with a question: are you sure lt_message[] contains HTML-formatted text?

Make that two questions: is it multipart? In most cases, so called HTML messages are really multipart messages, with two distinct parts, one HTML and one plain text.

0 Kudos

The problem is that I changed the code for attachment (pdf) and still in SOST I could see the file but on the recipient it came in decoded format in the email body

abo
Active Contributor
0 Kudos

Here is an example of multipart email, pay attention to the MIME types.

Sandra_Rossi
Active Contributor
0 Kudos

Difficult to say without seeing the HTML code and without seeing the result in Outlook. Email clients all render HTML contents differently. You should ask your administrator to be able to test with your own email address/Outlook from the development system.

Additional questions: Do you wrap your HTML code in <html><body>...</body></html>? Are there other ABAP programs which send emails? (if yes, look at what these programs do)