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: 

Built-in class for sending e-mail?

Former Member
0 Kudos

Is there a built-in class for sending e-mail in ABAP?

1 ACCEPTED SOLUTION

former_member194669
Active Contributor
0 Kudos

Hi,

Check this code , i have done for sending mail with multiple attachments.


Form f_send_html_mail.
  data: send_request       type ref to cl_bcs.
  data: document           type ref to cl_document_bcs.
  data: sender             type ref to cl_sapuser_bcs.
  data: recipient          type ref to if_recipient_bcs.
  data: exception_info     type ref to if_os_exception_info,
  bcs_exception            type ref to cx_document_bcs.
 
  data i_attachment_size type sood-objlen.
  data v_status          type bcs_stml.
  data v_request_status  type bcs_rqst.
 
  data : v_desc like sopcklsti1-obj_descr.
 
      concatenate 'Doc-' pdocno into v_desc.
      condense v_desc no-gaps.
      submit yttcr0001 exporting list to memory
               with p_docno = pdocno
               and return.
 
    call function 'LIST_FROM_MEMORY'
      tables
        listobject = report_list
      exceptions
        not_found  = 1
        others     = 2.
 
    call function 'WWW_HTML_FROM_LISTOBJECT'
      exporting
        template_name = 'WEBREPORTING_REPORT'
      tables
        html          = report_html
        listobject    = report_list.
 
    describe table objbin lines tab_lines.
    v_lines = tab_lines + 1.
 
    loop at report_html.
      move report_html to it_attach1.
      append it_attach1.
    endloop.
 
  send_request = cl_bcs=>create_persistent( ).
  try.
      document = cl_document_bcs=>create_document(
                                    i_type    = 'RAW'
                                    i_text = it_content[]
                                    i_subject = subject ).
      if not attach_name1 is initial.
        call method document->add_attachment
          exporting
            i_attachment_type    = attach_ext1
            i_attachment_subject = attach_name1
            i_att_content_text   = it_attach1[].
      endif.
     "<<< if you want multiple attachment then use this
      if not attach_name2 is initial.
        call method document->add_attachment
          exporting
            i_attachment_type    = attach_ext2
            i_attachment_subject = attach_name2
            i_att_content_text   = it_attach2[].
      endif.
 
      call method send_request->set_document( document ).
      sender = cl_sapuser_bcs=>create( sy-uname ).
 
      call method send_request->set_sender
        exporting
          i_sender = sender.
 
      loop at it_recipient.
        translate it_recipient-smtp_addr to lower case.
        recipient = cl_cam_address_bcs=>create_internet_address( it_recipient-smtp_addr ).
        call method send_request->add_recipient
          exporting
            i_recipient  = recipient
            i_express    = ' '
            i_copy       = ' '
            i_blind_copy = ' '
            i_no_forward = ' '.
      endloop.
      move : 'E' to v_request_status.
      v_status = v_request_status.
      call method send_request->set_status_attributes
        exporting
          i_requested_status = v_request_status
          i_status_mail      = v_status.
      call method send_request->send( ).
      commit work.
    catch cx_document_bcs into bcs_exception.
  endtry.
endform.                                

Please check link , i have answered related to query like yours

aRs

4 REPLIES 4

Former Member
0 Kudos

Hello

No that i know...but check this:

*&----


*& Report YMAIL

*&

*&----


*& uses sap script

*&

*&----


REPORT YMAIL.

DATA: ITCPO LIKE ITCPO,

TAB_LINES LIKE SY-TABIX.

  • Variables for EMAIL functionality

DATA: MAILDATA LIKE SODOCCHGI1.

DATA: MAILPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.

DATA: MAILHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.

DATA: MAILBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: MAILTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: MAILREC LIKE SOMLREC90 OCCURS 0 WITH HEADER LINE.

DATA: SOLISTI1 LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE.

PERFORM SEND_FORM_VIA_EMAIL.

***********************************************************************

  • FORM SEND_FORM_VIA_EMAIL *

***********************************************************************

FORM SEND_FORM_VIA_EMAIL.

CLEAR: MAILDATA, MAILTXT, MAILBIN, MAILPACK, MAILHEAD, MAILREC.

REFRESH: MAILTXT, MAILBIN, MAILPACK, MAILHEAD, MAILREC.

  • Creation of the document to be sent File Name

MAILDATA-OBJ_NAME = 'TEST'.

  • Mail Subject

MAILDATA-OBJ_DESCR = 'Subject'.

  • Mail Contents

MAILTXT-LINE = 'Here is your file, would you check it?'.

APPEND MAILTXT.

  • Prepare Packing List

PERFORM PREPARE_PACKING_LIST.

BREAK gpulido.

  • Set recipient - email address here!!!

*MAILREC-RECEIVER = 'gabe_pu@hotmail.com'.

MAILREC-RECEIVER = 'rafamarfer@gmail.com'.

MAILREC-REC_TYPE = 'U'.

APPEND MAILREC.

    • Set recipient - email address here!!!

*MAILREC-RECEIVER = 'BGIRALDO'.

*MAILREC-REC_TYPE = 'B'.

*APPEND MAILREC.

  • Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = MAILDATA

PUT_IN_OUTBOX = 'X'

TABLES

PACKING_LIST = MAILPACK

OBJECT_HEADER = MAILHEAD

CONTENTS_BIN = MAILBIN

CONTENTS_TXT = MAILTXT

RECEIVERS = MAILREC

EXCEPTIONS

TOO_MANY_RECEIVERS = 1

DOCUMENT_NOT_SENT = 2

OPERATION_NO_AUTHORIZATION = 4

OTHERS = 99.

submit rsconn01 with mode = 'INT' and return.

CASE SY-SUBRC.

WHEN 0.

WRITE: / 'Result of the send process:'.

LOOP AT MAILREC.

WRITE: / mailrec-RECEIVER(48), ':'.

IF mailrec-RETRN_CODE = 0.

WRITE 'sent successfully'.

ELSE.

WRITE 'not sent'.

ENDIF.

ENDLOOP.

WHEN 1.

WRITE: / 'no authorization to send to the specified number of'

.

WHEN 2.

WRITE: / 'document could not be sent to any of the recipients!'.

WHEN 4.

WRITE: / 'no authorization to send !'.

WHEN OTHERS.

WRITE: / 'error occurred during sending !'.

ENDCASE.

ENDFORM.

***********************************************************************

  • Form PREPARE_PACKING_LIST

***********************************************************************

FORM PREPARE_PACKING_LIST.

CLEAR: MAILPACK, MAILBIN, MAILHEAD.

REFRESH: MAILPACK, MAILBIN, MAILHEAD.

DESCRIBE TABLE MAILTXT LINES TAB_LINES.

READ TABLE MAILTXT INDEX TAB_LINES.

MAILDATA-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( MAILTXT ).

  • Creation of the entry for the compressed document

CLEAR MAILPACK-TRANSF_BIN.

MAILPACK-HEAD_START = 1.

MAILPACK-HEAD_NUM = 0.

MAILPACK-BODY_START = 1.

MAILPACK-BODY_NUM = TAB_LINES.

MAILPACK-DOC_TYPE = 'RAW'.

APPEND MAILPACK.

  • Creation of the document attachment

  • This form gets the OTF code from the SAPscript form.

  • If you already have your OTF code, I believe that you may

  • be able to skip this form. just do the following code, looping thru

  • your SOLISTI1 and updating MAILBIN.

PERFORM GET_OTF_CODE.

LOOP AT SOLISTI1.

MOVE-CORRESPONDING SOLISTI1 TO MAILBIN.

APPEND MAILBIN.

ENDLOOP.

DESCRIBE TABLE MAILBIN LINES TAB_LINES.

MAILHEAD = 'TEST.OTF'.

APPEND MAILHEAD.

    • Creation of the entry for the compressed attachment

MAILPACK-TRANSF_BIN = 'X'.

MAILPACK-HEAD_START = 1.

MAILPACK-HEAD_NUM = 1.

MAILPACK-BODY_START = 1.

MAILPACK-BODY_NUM = TAB_LINES.

MAILPACK-DOC_TYPE = 'OTF'.

MAILPACK-OBJ_NAME = 'TEST'.

MAILPACK-OBJ_DESCR = 'Subject'.

MAILPACK-DOC_SIZE = TAB_LINES * 255.

APPEND MAILPACK.

ENDFORM.

***********************************************************************

  • Form GET_OTF_CODE

***********************************************************************

FORM GET_OTF_CODE.

DATA: BEGIN OF OTF OCCURS 0.

INCLUDE STRUCTURE ITCOO .

DATA: END OF OTF.

DATA: ITCPO LIKE ITCPO.

DATA: ITCPP LIKE ITCPP.

CLEAR ITCPO.

ITCPO-TDGETOTF = 'X'.

  • Start writing OTF code

CALL FUNCTION 'OPEN_FORM'

EXPORTING

FORM = 'YSEND_MAIL'

LANGUAGE = SY-LANGU

OPTIONS = ITCPO

DIALOG = ' '

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'START_FORM'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

WINDOW = 'MAIN'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

  • Close up Form and get OTF code

CALL FUNCTION 'END_FORM'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

MOVE-CORRESPONDING ITCPO TO ITCPP.

CALL FUNCTION 'CLOSE_FORM'

IMPORTING

RESULT = ITCPP

TABLES

OTFDATA = OTF

EXCEPTIONS

OTHERS = 1.

  • Move OTF code to structure SOLI form email

CLEAR SOLISTI1. REFRESH SOLISTI1.

LOOP AT OTF.

SOLISTI1-LINE = OTF.

APPEND SOLISTI1.

ENDLOOP.

ENDFORM.

Bye Gabriel

Former Member
0 Kudos

Hello,

check this one also, will send using a smartForm, please post your email I´ll send the code.

Bye

Gabriel

Message was edited by:

Gabriel Fernando Pulido V.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Yes there is, see this blog.

/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface

Regards,

RIch Heilman

former_member194669
Active Contributor
0 Kudos

Hi,

Check this code , i have done for sending mail with multiple attachments.


Form f_send_html_mail.
  data: send_request       type ref to cl_bcs.
  data: document           type ref to cl_document_bcs.
  data: sender             type ref to cl_sapuser_bcs.
  data: recipient          type ref to if_recipient_bcs.
  data: exception_info     type ref to if_os_exception_info,
  bcs_exception            type ref to cx_document_bcs.
 
  data i_attachment_size type sood-objlen.
  data v_status          type bcs_stml.
  data v_request_status  type bcs_rqst.
 
  data : v_desc like sopcklsti1-obj_descr.
 
      concatenate 'Doc-' pdocno into v_desc.
      condense v_desc no-gaps.
      submit yttcr0001 exporting list to memory
               with p_docno = pdocno
               and return.
 
    call function 'LIST_FROM_MEMORY'
      tables
        listobject = report_list
      exceptions
        not_found  = 1
        others     = 2.
 
    call function 'WWW_HTML_FROM_LISTOBJECT'
      exporting
        template_name = 'WEBREPORTING_REPORT'
      tables
        html          = report_html
        listobject    = report_list.
 
    describe table objbin lines tab_lines.
    v_lines = tab_lines + 1.
 
    loop at report_html.
      move report_html to it_attach1.
      append it_attach1.
    endloop.
 
  send_request = cl_bcs=>create_persistent( ).
  try.
      document = cl_document_bcs=>create_document(
                                    i_type    = 'RAW'
                                    i_text = it_content[]
                                    i_subject = subject ).
      if not attach_name1 is initial.
        call method document->add_attachment
          exporting
            i_attachment_type    = attach_ext1
            i_attachment_subject = attach_name1
            i_att_content_text   = it_attach1[].
      endif.
     "<<< if you want multiple attachment then use this
      if not attach_name2 is initial.
        call method document->add_attachment
          exporting
            i_attachment_type    = attach_ext2
            i_attachment_subject = attach_name2
            i_att_content_text   = it_attach2[].
      endif.
 
      call method send_request->set_document( document ).
      sender = cl_sapuser_bcs=>create( sy-uname ).
 
      call method send_request->set_sender
        exporting
          i_sender = sender.
 
      loop at it_recipient.
        translate it_recipient-smtp_addr to lower case.
        recipient = cl_cam_address_bcs=>create_internet_address( it_recipient-smtp_addr ).
        call method send_request->add_recipient
          exporting
            i_recipient  = recipient
            i_express    = ' '
            i_copy       = ' '
            i_blind_copy = ' '
            i_no_forward = ' '.
      endloop.
      move : 'E' to v_request_status.
      v_status = v_request_status.
      call method send_request->set_status_attributes
        exporting
          i_requested_status = v_request_status
          i_status_mail      = v_status.
      call method send_request->send( ).
      commit work.
    catch cx_document_bcs into bcs_exception.
  endtry.
endform.                                

Please check link , i have answered related to query like yours

aRs