Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Back to Sending Mails - Home Page

Author:  Pratik Mallick
Submitted: 3rd April 2009

Introduction

Send notification is becoming important from business point of view. It adds extra reliability to the process and enables business person to react quickly to customer's requirement. If any error occurred to a process some group of people in the top management will get a notification and react based on the details specified in the notification.

Business Requirements

In some situation we had to send a notification to a group people. Some cases we had to only send the text in the mail body and some cases we had to send the detail records as attachments. Suppose one user ran a program to update the material master and the top management wants to get the success records as one attachment and error records as another attachment in the email.   

Methodology

Using the below object oriented technique it can easily obtained. 

Create object

The standard class CL_BCS is used to send the notification along with attachment to a group of people. Method CREATE_PERSISTENT is used to create an send request object of the standard class CL_BCS.

Create body of the email

Method CREATE_DOCUMENT of standard class CL_DOCUMENT_BCS is used to create the text of the email body.

Create attachments of the email

Method ADD_ATTACHMENT of standard class CL_DOCUMENT_BCS is used to create attachment of the email. If we want to create the multiple attachment we have call this method multiple times.
Method SET_DOCUMENT of the standard class CL_BCS is used to add the document (email body and attachments) to be send request object.

Create sender

Method CREATE of the standard class CL_SAPUSER_BCS is used to create the sender address.
Method SET_SENDER of standard class CL_BCS is used add the sender address to the send request object.

Create recipient

Method CREATE_INTERNET_ADDRESS of standard class CL_CAM_ADDRESS_BCS is used to create recipient email address.

Method ADD_RECIPIENT of standard class CL_BCS is used to add the recipient email address to the send request object.

Send email

Method SEND of standard class CL_BCS is used to send the send request object to a group of people added in the recipient list.
Sending is done in a queued way; immediate sending can be triggered for very urgent alerts.

Code

data: l_send_request type ref to cl_bcs,         " Send request
      l_body      type bcsy_text,                " Mail body
      l_success   type bcsy_text,                " Atchmnt for success
      l_error     type bcsy_text,                " Atchmnt for error
      wa_text     type soli,                     " Work area for attach
      l_document  type ref to cl_document_bcs,   " Mail body
      l_sender    type ref to if_sender_bcs,     " Sender address
      l_recipient type ref to if_recipient_bcs,  " Recipient
      l_size      type sood-objlen,              " Size of Attachment
      l_lines     type i,                        " Lines count
      l_email     type ad_smtpadr,               " Email ID
      l_extension type soodk-objtp value 'RAW'.  " TXT format
* Prepare mail bidy
append 'Send multiple file as attachment of a Email.'  to l_body.
append space to l_body.
* Preparing contents of attachment with Change Log
* Header line
wa_text-line+0   = 'First column'.
wa_text-line+20  = 'Second column'.
wa_text-line+40  = 'Third column'.
wa_text-line+60  = 'Fourth column'.
wa_text-line+80  = 'Fifth column'.
wa_text-line+100 = 'Sixth column'.
wa_text-line+120 = 'Seventh column'.
wa_text-line+140 = 'Eighth column'.
append wa_text to l_success.
append wa_text to l_error.
clear : wa_text.
* Populate the data part
wa_text-line+0   = '111111111'.
wa_text-line+20  = '222222222'.
wa_text-line+40  = '333333333'.
wa_text-line+60  = '333333333'.
wa_text-line+80  = '444444444'.
wa_text-line+100 = '555555555'.
wa_text-line+120 = '666666666'.
wa_text-line+140 = '777777777'.
append wa_text to l_success.
clear : wa_text.
wa_text-line+0   = 'aaaaaaaaa'.
wa_text-line+20  = 'bbbbbbbbb'.
wa_text-line+40  = 'ccccccccc'.
wa_text-line+60  = 'ddddddddd'.
wa_text-line+80  = 'eeeeeeeee'.
wa_text-line+100 = 'fffffffff'.
wa_text-line+120 = 'ggggggggg'.
wa_text-line+140 = 'hhhhhhhhh'.
append wa_text to l_error.
clear : wa_text.
* Creates persistent send request
l_send_request = cl_bcs=>create_persistent( ).
* Craete document for mail body
l_document = cl_document_bcs=>create_document(
             i_type    = 'RAW'
             i_text    = l_body  " Mail body
             i_subject = 'Mail send as attachment' ).
* Attachment for success records
l_lines = lines( l_success ).
l_size = l_lines * 255.
* Add attchment
call method l_document->add_attachment
  exporting
    i_attachment_type    = l_extension
    i_attachment_subject = 'Success'
    i_attachment_size    = l_size
    i_att_content_text   = l_success. " Attachment for success record
* Attachment for error records
l_lines = lines( l_error ).
l_size = l_lines * 255.
* Add attchment
call method l_document->add_attachment
  exporting
    i_attachment_type    = l_extension
    i_attachment_subject = 'Error'
    i_attachment_size    = l_size
    i_att_content_text   = l_error. " Attachment for error record
* Add the document to send request
call method l_send_request->set_document( l_document ).
* Sender addess
l_sender = cl_sapuser_bcs=>create( sy-uname ).
call method l_send_request->set_sender
  exporting
    i_sender = l_sender.
* Recipient address
l_email = 'myname@in.ibm.com'.
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 = ' '.
l_email = 'myname@gmail.com'.
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 = ' '.
* E-Mail is placed into queue for sending. In case of very important alerts
* use the following parameter to initiate an 'immediate sending'; but notice that
* this will trigger a dedicated send process for every single message - which is
* very costly for system performance
* l_send_request->set_send_immediately( 'X' ). "uncomment for immediate sending
* Send mail
call method l_send_request->send( ).
commit work. */

3 Comments

  1. Guest

    Nice Post.

     I have tried sending a mail by creating document of type HTM, but the mail is not delivered. Attachment to the document is necessary?

     Regards,

    Imran.

  2. Former Member

    Very good to remember how to use the class
  3. Good post. Very useful