Skip to Content
Author's profile photo Frederic Girod

Sending Mail using Oo

You can use the function SO_NEW_DOCUMENT_SEND_API1 to send an email from SAP, but you could also use Oo.

Example of simple HTML mail with Oo.

Data declaration

DATA : obj_mime_helper   TYPE REF TO cl_gbt_multirelated_service ,
        obj_bcs           TYPE REF TO cl_bcs ,
        obj_doc_bcs       TYPE REF TO cl_document_bcs ,
        obj_recipient     TYPE REF TO if_recipient_bcs ,

        w_status          TYPE bcs_rqst ,

        is_soli           TYPE soli ,

        it_soli           TYPE TABLE OF soli .

Creation of the mail

* Create the main object of the mail.
   CREATE OBJECT obj_mime_helper.

* Create the mail content.
   MOVE ‘<!DOCTYPE html PUBLIC “-//IETF//DTD HTML 2.0//EN”>’ TO is_soliline.
   APPEND is_soli TO it_soli.
   MOVE ‘<HTML>’ TO is_soliline.
   APPEND is_soli TO it_soli.
   MOVE ‘<BODY>’ TO is_soliline.
   APPEND is_soli TO it_soli.
   MOVE ‘<P>Hello world !</P>’ TO is_soliline.
   APPEND is_soli TO it_soli.
   MOVE ‘</BODY>’ TO is_soliline.
   APPEND is_soli TO it_soli.
   MOVE ‘</HTML>’ TO is_soliline.
   APPEND is_soli TO it_soli.

* Set the HTML body of the mail
   CALL METHOD obj_mime_helper->set_main_html
       content     = it_soli
       filename    =
       description = ‘Hello world’.

* Set the subject of the mail.
   obj_doc_bcs = cl_document_bcs=>create_from_multirelated(
                   i_subject          = ‘Mail example’
                   i_importance       = ‘9’                ” 1 / 5 / 9
                   i_multirel_service = obj_mime_helper ).
   obj_bcs = cl_bcs=>create_persistent( ).
              i_document = obj_doc_bcs ).

* Set the email address
   obj_recipient = cl_cam_address_bcs=>create_internet_address(
                     i_address_string ‘
              i_recipient = obj_recipient ).

* Change the status.
   MOVE ‘N’ TO w_status.
   CALL METHOD obj_bcs->set_status_attributes
       i_requested_status = w_status.

* Send the mail.
   obj_bcs->send( ).

* Commit Work.
   IF sysubrc EQ space.


Go in transaction SOST to see the mail :


The sender is the user who ran the program.


Example of mail with a simple attachment

Data declaration (add)

        w_attach_type     TYPE so_obj_tp ,
        w_attach_subject  TYPE so_obj_des ,
        w_attach_length   TYPE soodobjlen ,
        w_rc              TYPE i ,
        w_filename        TYPE string ,
        w_length          TYPE i ,

        is_file           TYPE file_table ,

        it_file           TYPE filetable ,
        it_file_content   TYPE TABLE OF solix.

Open the file

* Get the filename of the input file.
   CALL METHOD cl_gui_frontend_services=>file_open_dialog
       file_table = it_file
       rc         = w_rc
       OTHERS     = 5.
   CHECK sysubrc EQ space AND
         NOT it_file[] IS INITIAL.

* Get the first line (single selection)
   READ TABLE it_file
        INTO is_file
        INDEX 1.
   CHECK sysubrc EQ space.
   MOVE is_filefilename TO w_filename.

* Read the file and put it in the internal table
   CALL METHOD cl_gui_frontend_services=>gui_upload
       filename   = w_filename
       filetype   = ‘BIN’
       filelength = w_length
       data_tab   = it_file_content
       OTHERS     = 19.

it’s really an easy example, there is no need to convert the file content.

Insert the attachment in the mail

  MOVE : ‘PDF’         TO w_attach_type ,
          w_filename   TO w_attach_subject ,
          w_length     TO w_attach_length .

   CALL METHOD obj_doc_bcs->add_attachment
       i_attachment_type    = w_attach_type
       i_attachment_subject = w_attach_subject
       i_attachment_size    = w_attach_length
       i_att_content_hex    = it_file_content.

For this example, I force the attachment type to PDF, but it could be anything.

Used the method ADD_ATTACHMENT after the creation of the object OBJ_DOC_BCS or you will have a dump.



The mail didn’t change, but there is a new tab in the message detail : Attachments


I have keep the full location of the filename, but you could keep only the filename.

Modify the sender

It’s something useful to change the sender of an email. For example, when you send the mail to a customer and you didn’t want he replies to you but to a generic mail box like

Data declaration (add)

        obj_sender        TYPE REF TO if_sender_bcs .

PARAMETERS : p_sender TYPE ad_smtpadr.

Modify the sender in the OBJ_BCS

* Modify the sender
   obj_sender = cl_cam_address_bcs=>create_internet_address(
                         i_address_string = p_sender ).
   obj_bcs->set_sender( i_sender = obj_sender ).



In my example you could choose the email address of the sender.


In the RFC of the SMTP is not really allowed to send a mail from a domain different of the mail address. That means, my SMTP server must refused to send a mail from because I’m not owner of this domain. You could have a message like : “SMTP relaying denied”

(next step, Mail of a Smartforms)


Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member


      Nice articles.

      Thanks for sharing