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: 

sending mail through abap program...

Former Member
0 Kudos

Hi All,

I need a sample code to send a mail through ABAP code.

Thanks & Regards

Ashu Singh.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi Ashu,

u can use the FM

*Send the mail using the folloing mail to the above mentioned

*receivers

call function <b> 'SO_NEW_DOCUMENT_SEND_API1'</b>

exporting

document_data = wa_document_data

document_type = 'RAW'

put_in_outbox = 'X'

commit_work = 'X'

tables

object_header = it_object_header

object_content = it_object_content

receivers = it_receivers

exceptions

too_many_receivers = 1

document_not_sent = 2

document_type_not_exist = 3

operation_no_authorization = 4

parameter_error = 5

x_error = 6

enqueue_error = 7

others = 8.

endif.

hope this will help u

Amit

8 REPLIES 8

Former Member
0 Kudos

hi Ashu,

u can use the FM

*Send the mail using the folloing mail to the above mentioned

*receivers

call function <b> 'SO_NEW_DOCUMENT_SEND_API1'</b>

exporting

document_data = wa_document_data

document_type = 'RAW'

put_in_outbox = 'X'

commit_work = 'X'

tables

object_header = it_object_header

object_content = it_object_content

receivers = it_receivers

exceptions

too_many_receivers = 1

document_not_sent = 2

document_type_not_exist = 3

operation_no_authorization = 4

parameter_error = 5

x_error = 6

enqueue_error = 7

others = 8.

endif.

hope this will help u

Amit

prasanth_kasturi
Active Contributor

Former Member
0 Kudos

Hai Ashu,

The following call funciton is used for mail sending.....

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

document_data = doc_chng

put_in_outbox = 'X'

commit_work = 'X'

TABLES

object_content = objcont

receivers = t_receivers[]

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

document_type_not_exist = 3

operation_no_authorization = 4

parameter_error = 5

x_error = 6

enqueue_error = 7

OTHERS = 8.

Regards,

Harish

Former Member
0 Kudos

HI Here is a Sample Program to send mail through ABAP code....

check the following sample program and also its easy to understand.

REPORT zvenkat_mail_xls_attach.

----


" Data retrieval related declarations

----


TYPES:

BEGIN OF t_emp_dat,

pernr TYPE pa0001-pernr,

persg TYPE pa0001-persg,

persk TYPE pa0001-persk,

plans TYPE pa0001-plans,

stell TYPE pa0001-stell,

END OF t_emp_dat.

DATA:

w_emp_data TYPE t_emp_dat.

DATA:

i_emp_data TYPE STANDARD TABLE OF t_emp_dat.

----


" Mail related declarations

----


"Variables

DATA :

g_sent_to_all TYPE sonv-flag,

g_tab_lines TYPE i.

"Types

TYPES:

t_document_data TYPE sodocchgi1,

t_packing_list TYPE sopcklsti1,

t_attachment TYPE solisti1,

t_body_msg TYPE solisti1,

t_receivers TYPE somlreci1.

"Workareas

DATA :

w_document_data TYPE t_document_data,

w_packing_list TYPE t_packing_list,

w_attachment TYPE t_attachment,

w_body_msg TYPE t_body_msg,

w_receivers TYPE t_receivers.

"Internal Tables

DATA :

i_document_data TYPE STANDARD TABLE OF t_document_data,

i_packing_list TYPE STANDARD TABLE OF t_packing_list,

i_attachment TYPE STANDARD TABLE OF t_attachment,

i_body_msg TYPE STANDARD TABLE OF t_body_msg,

i_receivers TYPE STANDARD TABLE OF t_receivers.

----


"Start-of-selection.

----


START-OF-SELECTION.

PERFORM get_data.

PERFORM build_xls_data_table.

----


"End-of-selection.

----


END-OF-SELECTION.

PERFORM send_mail.

&----


"Form get_data from PA0001

&----


FORM get_data.

SELECT pernr

persg

persk

plans

stell

FROM pa0001

INTO CORRESPONDING FIELDS OF TABLE i_emp_data

UP TO 4 ROWS.

ENDFORM. " get_data

&----


"Form build_xls_data_table

&----


FORM build_xls_data_table.

"If you have Unicode check active in program attributes then

"you will need to declare constants as follows.

CLASS cl_abap_char_utilities DEFINITION LOAD.

CONSTANTS:

con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,

con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

CONCATENATE 'PERNR' 'PERSG' 'PERSK' 'PLANS' 'STELL'

INTO w_attachment

SEPARATED BY con_tab.

CONCATENATE con_cret

w_attachment

INTO w_attachment.

APPEND w_attachment TO i_attachment.

CLEAR w_attachment.

LOOP AT i_emp_data INTO w_emp_data.

CONCATENATE w_emp_data-pernr

w_emp_data-persg

w_emp_data-persk

w_emp_data-plans

w_emp_data-stell

INTO w_attachment

SEPARATED BY con_tab.

CONCATENATE con_cret w_attachment

INTO w_attachment.

APPEND w_attachment TO i_attachment.

CLEAR w_attachment.

ENDLOOP.

ENDFORM. "build_xls_data_table

&----


"Form send_mail

"----


"PACKING LIST

"This table requires information about how the data in the

"tables OBJECT_HEADER, CONTENTS_BIN and CONTENTS_TXT are to

"be distributed to the documents and its attachments.The first

"row is for the document, the following rows are each for one

"attachment.

&----


FORM send_mail .

"Subject of the mail.

w_document_data-obj_name = 'MAIL_TO_HEAD'.

w_document_data-obj_descr = 'Regarding Mail Program by SAP ABAP'.

"Body of the mail

PERFORM build_body_of_mail

USING:space,

'Hi,',

'I am fine. How are you? How are you doing ? ',

'This program has been created to send simple mail',

'with Subject,Body with Address of the sender. ',

'Regards,',

'Venkat.O,',

'SAP HR Technical Consultant.'.

"Write Packing List for Body

DESCRIBE TABLE i_body_msg LINES g_tab_lines.

w_packing_list-head_start = 1.

w_packing_list-head_num = 0.

w_packing_list-body_start = 1.

w_packing_list-body_num = g_tab_lines.

w_packing_list-doc_type = 'RAW'.

APPEND w_packing_list TO i_packing_list.

CLEAR w_packing_list.

"Write Packing List for Attachment

w_packing_list-transf_bin = 'X'.

w_packing_list-head_start = 1.

w_packing_list-head_num = 1.

w_packing_list-body_start = 1.

DESCRIBE TABLE i_attachment LINES w_packing_list-body_num.

w_packing_list-doc_type = 'XLS'.

w_packing_list-obj_descr = 'Excell Attachment'.

w_packing_list-obj_name = 'XLS_ATTACHMENT'.

w_packing_list-doc_size = w_packing_list-body_num * 255.

APPEND w_packing_list TO i_packing_list.

CLEAR w_packing_list.

"Fill the document data and get size of attachment

w_document_data-obj_langu = sy-langu.

READ TABLE i_attachment INTO w_attachment INDEX g_tab_lines.

w_document_data-doc_size = ( g_tab_lines - 1 ) * 255 + STRLEN(

w_attachment ).

"Receivers List.

w_receivers-rec_type = 'U'. "Internet address

w_receivers-receiver = 'mail id'.

w_receivers-com_type = 'INT'.

w_receivers-notif_del = 'X'.

w_receivers-notif_ndel = 'X'.

APPEND w_receivers TO i_receivers .

CLEAR:w_receivers.

"Function module to send mail to Recipients

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = w_document_data

put_in_outbox = 'X'

commit_work = 'X'

IMPORTING

sent_to_all = g_sent_to_all

TABLES

packing_list = i_packing_list

contents_bin = i_attachment

contents_txt = i_body_msg

receivers = i_receivers

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

document_type_not_exist = 3

operation_no_authorization = 4

parameter_error = 5

x_error = 6

enqueue_error = 7

OTHERS = 8.

IF sy-subrc = 0 .

MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.

ELSE.

WAIT UP TO 2 SECONDS.

"This program starts the SAPconnect send process.

SUBMIT rsconn01 WITH mode = 'INT'

WITH output = 'X'

AND RETURN.

ENDIF.

ENDFORM. " send_mail

&----


" Form build_body_of_mail

&----


FORM build_body_of_mail USING l_message.

w_body_msg = l_message.

APPEND w_body_msg TO i_body_msg.

CLEAR w_body_msg.

ENDFORM. " build_body_of_mail

0 Kudos

Even after using the API 's on the above posts, still you are not able to send amil, ensure that user settings in SU01 for your user is properly set so that you can send the mail.

Also goto the transaction SCOT to see whether the corresponding job is executed or not.

Ragha.

0 Kudos

Even after using the API 's on the above posts, still you are not able to send amil, ensure that user settings in SU01 for your user is properly set so that you can send the mail.

Also goto the transaction SCOT to see whether the corresponding job is executed or not.

Ragha.

Former Member
0 Kudos
DATA: OBJPACK   LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.
  DATA: OBJHEAD   LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.
  DATA: OBJBIN    LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.
  DATA: OBJTXT    LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.
  DATA: RECLIST   LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE.
  DATA: DOC_CHNG  LIKE SODOCCHGI1.
  DATA: TAB_LINES LIKE SY-TABIX.
  DATA L_NUM(3).


* Creation of the document to be sent
* File Name
  DOC_CHNG-OBJ_NAME = 'SENDFILE'.
* Mail Subject
  DOC_CHNG-OBJ_DESCR = 'Delivered Mail'.
* Mail Contents
  OBJTXT = 'Object text'.
  APPEND OBJTXT.

  DESCRIBE TABLE OBJTXT LINES TAB_LINES.
  READ TABLE OBJTXT INDEX TAB_LINES.
  DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).
* Creation of the entry for the compressed document
  CLEAR OBJPACK-TRANSF_BIN.
  OBJPACK-HEAD_START = 1.
  OBJPACK-HEAD_NUM = 0.
  OBJPACK-BODY_START = 1.
  OBJPACK-BODY_NUM = TAB_LINES.
  OBJPACK-DOC_TYPE = 'RAW'.
  APPEND OBJPACK.

* Creation of the document attachment
  LOOP AT ITAB_DATA.
    CONCATENATE ITAB_DATA-PRODUCTOR
                ITAB_DATA-VBELN
                ITAB_DATA-POSNR
                ITAB_DATA-MATNR INTO OBJBIN.
    APPEND OBJBIN.

  ENDLOOP.

  DESCRIBE TABLE OBJBIN LINES TAB_LINES.
  OBJHEAD = 'ORDERS'.
  APPEND OBJHEAD.

** Creation of the entry for the compressed attachment
  OBJPACK-TRANSF_BIN = 'X'.
  OBJPACK-HEAD_START = 1.
  OBJPACK-HEAD_NUM   = 1.
  OBJPACK-BODY_START = 1.
  OBJPACK-BODY_NUM   = TAB_LINES.
  OBJPACK-DOC_TYPE   = 'TXT'.
  OBJPACK-OBJ_NAME   = 'WEBSITE'.
  OBJPACK-OBJ_DESCR  = 'ORDERS.TXT'.
  OBJPACK-DOC_SIZE   = TAB_LINES * 255.
  APPEND OBJPACK.

* Completing the recipient list
* target recipent
  clear RECLIST.
  RECLIST-RECEIVER = ' Enter Email ID'.
  RECLIST-EXPRESS  = 'X'.
  RECLIST-REC_TYPE = 'U'.
  APPEND RECLIST.
* copy recipents
  clear RECLIST.
  
* Sending the document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
       EXPORTING
            DOCUMENT_DATA              = DOC_CHNG
       TABLES
            PACKING_LIST               = OBJPACK
            OBJECT_HEADER              = OBJHEAD
            CONTENTS_BIN               = OBJBIN
            CONTENTS_TXT               = OBJTXT
            RECEIVERS                  = RECLIST
       EXCEPTIONS
            TOO_MANY_RECEIVERS         = 1
            DOCUMENT_NOT_SENT          = 2
            OPERATION_NO_AUTHORIZATION = 4
            OTHERS                     = 99.