Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

Sending Mail using Oo

$
0
0

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_soli-line.
   APPEND is_soli TO it_soli.
   MOVE '<HTML>' TO is_soli-line.
   APPEND is_soli TO it_soli.
   MOVE '<BODY>' TO is_soli-line.
   APPEND is_soli TO it_soli.
   MOVE '<P>Hello world !</P>' TO is_soli-line.
   APPEND is_soli TO it_soli.
   MOVE '</BODY>' TO is_soli-line.
   APPEND is_soli TO it_soli.
   MOVE '</HTML>' TO is_soli-line.
   APPEND is_soli TO it_soli.

* Set the HTML body of the mail
   CALL METHOD obj_mime_helper->set_main_html
     EXPORTING
       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( ).
   obj_bcs->set_document(
              i_document = obj_doc_bcs ).


* Set the email address
   obj_recipient = cl_cam_address_bcs=>create_internet_address(
                     i_address_string 'toto@titi.com' ).
   obj_bcs->add_recipient(
              i_recipient = obj_recipient ).


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


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



* Commit Work.
   IF sy-subrc EQ space.
     COMMIT WORK AND WAIT.
   ELSE.
     ROLLBACK WORK.
   ENDIF.

 

 

 

Result

 

Go in transaction SOST to see the mail :

 

SOST_1.png

The sender is the user who ran the program.

 

SOST_2.png

 

 

 

 

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 sood-objlen ,
        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
     CHANGING
       file_table = it_file
       rc         = w_rc
     EXCEPTIONS
       OTHERS     = 5.
   CHECK sy-subrc EQ space AND
         NOT it_file[] IS INITIAL.

* Get the first line (single selection)
   READ TABLE it_file
        INTO is_file
        INDEX 1.
   CHECK sy-subrc EQ space.
   MOVE is_file-filename TO w_filename.


* Read the file and put it in the internal table
   CALL METHOD cl_gui_frontend_services=>gui_upload
     EXPORTING
       filename   = w_filename
       filetype   = 'BIN'
     IMPORTING
       filelength = w_length
     CHANGING
       data_tab   = it_file_content
     EXCEPTIONS
       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
     EXPORTING
       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.

 

 

Result

 

SOST_3.png

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

 

sost_4.png

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 customer-suport@mycompany.com

 

 

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 ).

 

 

Result

 

result_1.png

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

 

 

sost_5.png

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 @sap.com because I'm not owner of this domain. You could have a message like : "SMTP relaying denied"

 

 

 

 

 

(next step, Mail of a Smartforms)

Fred


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>