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 email from java to SAP

Former Member
0 Kudos

Hi,

From a java app., a call is made to a BAPI which calls SO_OBJECT_SEND to send emails to internet addresses. But its not working and a return code of 9 is returned, which is OBJECT_NOT_SENT. Has anyone tried using SO_OBJECT_SEND and make a call to it from a java method? Please advise.

Thanks.

Kshitij.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Thanks Rich for your feedback.

I used the FM SO_DOCUMENT_SEND_API1 instead which in its help mentions email using RFC. But the java method is giving a return code of 2. I am on 4.6C.

Your FM SO_NEW_DOCUMENT_SEND_API1 doesn't mention anything with RFC. Do you think it will work?

8 REPLIES 8

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I have been successful in call a custom RFC enabled function module which inturn calls function module

SO_NEW_DOCUMENT_ATT_SEND_API1 from a java iView.

Regards,

Rich Heilman

0 Kudos

Here is a quick and dirty sample of sending mail from ABAP. Put this functionality into a RFC enabled FM and call the FM from your java app.

Regards,

Rich Heilman



REPORT ZRICH_0003 .

DATA: MAILDATA   LIKE SODOCCHGI1.
DATA: MAILTXT    LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.
DATA: MAILREC    LIKE SOMLREC90 OCCURS 0  WITH HEADER LINE.

START-OF-SELECTION.

  CLEAR:    MAILDATA, MAILTXT,  MAILREC.
  REFRESH:  MAILTXT, MAILREC.

  MAILDATA-OBJ_NAME = 'TEST'.
  MAILDATA-OBJ_DESCR = Subject Line'.
  MAILDATA-OBJ_LANGU = SY-LANGU.

* Build message
  mailtxt = 'This is the body'.
  APPEND MAILTXT.

* Build Receivers
  MAILREC-RECEIVER = 'whoever@company.com'.
  MAILREC-REC_TYPE  = 'U'.
  APPEND MAILREC.

* Send the mail  
  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
       EXPORTING
            DOCUMENT_DATA              = MAILDATA
            DOCUMENT_TYPE              = 'RAW'
            PUT_IN_OUTBOX              = 'X'
       TABLES
            OBJECT_HEADER              = MAILTXT
            OBJECT_CONTENT             = MAILTXT
            RECEIVERS                  = MAILREC
       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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

Former Member
0 Kudos

Thanks Rich for your feedback.

I used the FM SO_DOCUMENT_SEND_API1 instead which in its help mentions email using RFC. But the java method is giving a return code of 2. I am on 4.6C.

Your FM SO_NEW_DOCUMENT_SEND_API1 doesn't mention anything with RFC. Do you think it will work?

0 Kudos

Hello,

The FM SO_NEW_DOCUMENT_SEND_API1 should work with RFC. Since it is an RFC enabled function module.

I would suggest you to go step by step.

First try to send a mail by using the above FM by SAP. Once successful, you can make a remote call with correct set of parameters.

For RFC:

CALL FUNCTION SO_NEW_DOCUMENT_SEND_API1'

DESTINATION <dest>

....

Best Regards, Murugesh AS

0 Kudos

Thanks Murugesh.

I tried the following in my RFC enabled BAPI;

describe table obj_cont lines lin.

move: sy-repid to doc_data-obj_name,

'C' to doc_data-sensitivty,

'1' to doc_data-OBJ_PRIO.

doc_data-doc_size = ( lin - 1 ) * 255 + strlen( obj_cont ).

concatenate 'LMI: Org/Pay change -- Rejection; ID:' req_id

into doc_data-OBJ_DESCR separated by space.

move: '1' to pack_list-head_start,

'0' to pack_list-head_num,

'1' to pack_list-body_start,

lin to pack_list-body_num,

'RAW' to pack_list-doc_type,

sy-repid to pack_list-obj_name,

doc_data-doc_size to pack_list-doc_size.

append pack_list. clear pack_list.

move '&SO_FONTTYPE=FIX' to obj_headr.

append obj_headr.

loop at act_lines.

move act_lines-tdline to cont_txt-line.

append cont_txt. clear cont_txt.

endloop.

loop at receiver.

move: receiver-recextnam to rece-RECEIVER,

receiver-recesc to rece-REC_TYPE,

'INT' to rece-com_type.

append rece. clear rece.

endloop.

  • If called from Java then use rfc fm ****

call function 'SO_NEW_DOCUMENT_SEND_API1'

destination SYST-SYSID

exporting

document_data = doc_data

DOCUMENT_TYPE = 'RAW'

PUT_IN_OUTBOX = 'X'

tables

OBJECT_HEADER = obj_headr

OBJECT_CONTENT = cont_txt

receivers = rece

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.

But it still gives error DOCUMENT_NOT_SENT when invoked from JAVA. But it runs FINE from SAP.

0 Kudos

Ok....so it works from SAP, not from Java app. Another question for ya. How is your Java app connecting to SAP. Through Jco? If so, what is the user id that is used to log into SAP through Jco? Does this user id have an email address tied to it in SAP? This could be your problem. If think that you need to have a email address associated with the user id to sent outgoing mail. Check this in transaction SU01.

Regards,

Rich Heilman

0 Kudos

Thanks to Rich and every one who responded to my query. The user WEBAPPROVAL did not have an email address maintained. After assigning it, the emails were sent. I appreciate everyones help.

Former Member
0 Kudos

Dear Kshitij,

Could you send my your code? I have a same problem but I can't solve it.

Regards,

GUsztav