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: 

Send mail using class

david_fryda2
Participant
0 Kudos
2,208

Hi,

I need to send PDF files as attachment to a mail using Classes.

Is there a way to do it ?

Thanks.

David

1 ACCEPTED SOLUTION

former_member194669
Active Contributor
0 Kudos
859

Hi,

I have develop this code to send multiple HTML attachments using CL_BCS class


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.                                

and also check this link

/people/thomas.jung3/blog/2005/01/05/develop-a-web-service-that-sends-an-email--in-abap

aRs

8 REPLIES 8

Former Member
0 Kudos
859

Hi David,

Did you already check the IF_SWF_UTL_MAIL ?

Regards,

Attila

former_member194669
Active Contributor
0 Kudos
860

Hi,

I have develop this code to send multiple HTML attachments using CL_BCS class


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.                                

and also check this link

/people/thomas.jung3/blog/2005/01/05/develop-a-web-service-that-sends-an-email--in-abap

aRs

0 Kudos
859

Hi,

Just one question : How do you fill the it_attach1 ?

thanks.

Regards

Former Member
0 Kudos
859

Hi David

You can use BCS for this

Check these progs BCS_EXAMPLE_1, BCS_EXAMPLE_2, BCS_EXAMPLE_3, BCS_EXAMPLE_4, BCS_EXAMPLE_5, BCS_EXAMPLE_6 in your R/3

Reward points if useful

Cheers

~Arun

0 Kudos
859

Thanks again for the help.

David

Former Member
0 Kudos
859

Hi David,

look via se38 for BCS_EXAMPLE_*.

There are very good examples.

Regards, Dieter

0 Kudos
859

Hi,

How can I read the binary from file without using GUI_UPLOAD because I want to run the program in background ?

Thanks,

0 Kudos
859

Hi,

I have created my it_attach1...5 attachment this way.


form f_append_object_bin tables pattach structure soli
                         using pdocno pctc pscc .
  data : v_desc like sopcklsti1-obj_descr.

  if not pdocno is initial.
      concatenate 'TCC-' pdocno into v_desc.
      condense v_desc no-gaps.
      submit ytccr0001 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 pattach.
      append pattach.
    endloop.

  endif.
endform.                                 " F_append_object_bin

May this one help you out.

aRs