Skip to Content
*Introduction* There seems to be a lot of requests in the forums for tips on how to send E-Mail from ABAP. Based upon these requests, I thought I would pull together some resources and samples on the subject. The setup and samples are quite different on different releases. Yesterday I posted a weblog for releases <=46D that used the API function modules. This weblog focuses on releases 610 and higher. There are many changes in this release. For one there is no longer any reliance on external executables to send the mail. The SAP Kernel now contains native SMTP processing capabilities in the Internet Connection Manager (ICM). Also we have a new ABAP interface that can be used called the Business Communication Services (BCS). All samples came from a 620 system. *Configuration* Before we start with any ABAP coding, we first have to setup and configure a few things. Much of this configuration may have already been completed by your Basis group. *ICM Setup* Unlike in release 46D and lower, we have no external executables to setup. There is no need to configure the Internet Mail Gateway or create any RFC destinations. However there is some setup that needs to be done to the ICM to activate and configure SMTP processing. OSS Note 455140 gives an excellent step-by-step guide to configuring SMTP. I will just give you a quick check list here without going into all the configuration details provided by this note. 1. If you are one release 610 only, you must add the SMTP Plug-In entry in your instance profile. This is not needed as of release 620 because the system is delivered with SMTP as an integrated part of the ICM. 2. In the instance profile, specify the TCP/IP port that SMTP will listen on. Port 25 is the default. 3. If you have multiple clients in your SAP system, you will want to setup a virtual host for each client. 4. Verify in transaction SICF that you have a SMTP node and that it is activated. It should look something like the following: image *SAPConnect* Well we are finished with all the low level setup. Now we need to connect the lowest layer (the ICM) to SAP’s middle message layer: SAPConnect. SAPConnect sits between the application layer (such as SAP Office) and the lowest layer hiding all the technical details. All of the SAPConnect monitoring and configuration can be reached from one transaction called SCOT. First SCOT has several different views that can be used. One shows the Jobs that have been setup, the other shows setup by routings, etc. I prefer to start SCOT in System Status View. This shows a tree with the communication types, the nodes setup for each type and the number of messages in each status. The following is a screen shot of this view: image We want to create a new node in SAPConnect for our E-Mail. As of release 46C you have a quite nice Wizard that walks you through the process. However you can also control all settings directly in a dialog maintenance window. This is a screen shot of our Email Node. image You can see that to setup this node all we have to do is specify the connection information for an external mail server. In our case we give the host name for our Corporate Microsoft Exchange Server. Also at this level we can specify the code page that we want all messages converted to before they are sent. I choose Unicode UTF-8 because we have an MDMP system that contains English, German, Spanish, Thai, and Polish languages. By converting to UTF-8 we get around the problem described in OSS note 331418. You do NOT have to have a Unicode system or kernel to support this functionality. Please refer to OSS Note 633265 for the necessary support package levels for this functionality to be complete. Now lets look at the configuration on our only configured address type: Internet. We don’t do any outbound filtering to different nodes. Therefore the address areas is set to the wildcard ‘*’. To comply with the Unicode setup we have all output formats being converted to PDF as OSS note 633265 suggests. image Next we need to schedule a job that will transmit out E-mails. Normally E-Mails will only go out when this job runs, so you probably want to schedule it quite frequently. (Later in my code example, I will show you how using the BCS interface you can send E-mails immediately as well). In our production system we run this job every 5 minutes. The job setup can also be done from SCOT. Switch your view to JOBS. You can then hit CREATE to start the process of scheduling a new Send Job. *Running a Test* We are just about ready to run our first test. The only other requirement is that the sender of any E-mail, must have their return address setup in their user master. If you don’t maintain this data, you will get a send error. If you want an SAP program to generate these addresses for you, have a look at OSS note 104263. Also in 46C and higher you have the option to configure the system to generate the sender address on the fly. OSS note 320443 gives you the details of this solution. You can see from the following screen shot the field that must be maintained in the user master: image Let’s try a test now. I like to start by testing using the SAP short message functionality. You will want to use menu System->Short Message. From here we will prepare a short test message. If you don’t want to wait for your batch job to run to find out if your message went through, just jump back to transaction SCOT. From the System Status view you can choose Ctrl+F7 or the Execute Icon to start the send process for a communication type. You should receive your test message if everything is setup fine. *Trouble Shooting* Let’s say that everything didn’t go fine. Where do we start trouble shooting? Once again start with the transaction SCOT. The system status shows us our status overview. You can see the number of messages in Error or Waiting. If you have a lot of messages in Waiting status, your send job probably isn’t scheduled. The overview screen is nice, but you probably want more details. From SCOT you can choose Utilities->Overview of Send Orders (Or Tcode SOST). This transaction will give a detailed report by Address type, status, date, time and sender. For each message we can view the transmission history, the message itself, and the trace log. You can also try and resubmit the message from here. If you need to activate tracing, you want to return to transaction SCOT. You then choose Utilities->Trace->Internal Trace. You can also view all traces from here without having to go through SOST. *ABAP Coding* If everything has worked up to this point, then you are ready to start writing your own E-mail calls in ABAP. The example I am going to share with you is actually a function module that we wrote. We wanted to write a function module that would really simplify the process of sending E-mail for our newer ABAP developers. We wanted them to just specify a few parameters and be off and running without having to understand the BCS classes. Hopefully you will find this function module helpful as well as being a good tutorial on how to use the BCS classes. *Function Interface* The function module Interface is quite simple in that it only has 3 input parameters. image The first is an optional parameter used to set what requested statuses types will be returned. You can specify N for No Statuses, E for Only Error Statuses, or A for All Statuses. The second parameter is an internal table that will hold all of our document parts. The following is the line type that defines this internal table type: image The first entry in this table will become our E-Mail Body. All other parts will become attachments in the email. For each record in this table you must specify the document type as a value defined in the foreign key table TSOTD. You can also supply the subject line (or attachment name) in the subject field. You can then specify the content of this document part in either the CONTENT_TEXT (ASCII text) or CONTENT_HEX (Binary data such as PDFs). You will notice that the structure of these two content areas match those used by the API function modules in 46D and lower. This makes for a very easy change from the API function modules to the BCS Classes. The third parameter is another internal table that will hold all of our recipients. The following is the line type that defines this internal table type: image The calling program has the choice of setting a full email address directly into the C_ADDRESS field or just specifying an SAP User ID in the UNAME field. If the calling program chooses to use the UNAME field, the function module will lookup the user’s E-Mail address from their user master record. Finally there are several flags that can be set per recipient such as No Forward, Express Message, etc. The function module has no Exporting, Changing, or table parameters. The only exception that is returned is the BCS Exception class itself: CX_BCS. image
To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Kevin Bowmer
    I have a problem since we migrated our system to microsoft and applied unicode coding standards. i was sending an excel document as an attachment, setting document type to ‘CSV’. This no longer works, when trying to open the attachment it displays the message “invalid type”.

    I notice that ‘CSV’ is not a type on table TSOTD, neither is ‘XLS’. Do these types need to be somehow set up on this table?


    Kev B

    1. Thomas Jung Post author
      How are you building the attachment content?  That is probably far more important tan the attachment document type.  The types are only used to set MIME-Type for the body part. Chances are more likely that you need to adjust how you build the CSV content – perhaps using the CL_ABAP_CHAR_UTILITIES class attributes instead of hard coded hex values. Or maybe it is the conversion from text to binary?  I would need to know more about your processing logic to comment further.
  2. Vinoth Nagarajan
    Hi Thomas,

      Thanks for this very useful info!
      I still have a question. How the status of each notification is determined? Using this class to send notification just puts it in a queue. Where is the feedback mechanism and how it works? Is it possible to predict status in SOST for an email id?

    Thanks in advance!

    1. Thomas Jung Post author
      I’m not sure I understand your question.  SOST displays the processing status of the request from start to finish.  Items are placed into queue and then handed off to the ICM for outbound processing.  SOST displays the status of the document in the ICM. What do you mean – “predict status”? I’m confused.
      1. Vinoth Nagarajan
        Let me put it in detail.
          1. I’m refferring the SOST status ‘green’ or ‘red’ that shows if the mail is successfully sent or not. In the program, after send_request->send, the outbound mail is put in queue for ICM to process. I want to know SOST status from my program. How can I achieve this?
          2. ‘Predict status’ – I had assumed that the status becomes ‘green’ or ‘red’ depending on the email id being valid or not (with all other settings correct). Can I just simulate sending a message to an email id and determine what its status wud be in SOST without actually sending an email?
           Hope I’m clear now.
        1. Thomas Jung Post author
          If you use the send immediately option more of the processing steps will occur within the sending program instead of in batch and you can catch more exceptions.  That is the only options I know of.  This won’t capture the response confirmation from the mail system since that is sent Asynch – hence the need for SOST.

          There may be APIs for reading the status information from SOST; but I’ve never used any or know of any.  You would have to do that research.

  3. David zhang
    Dear Thomas Jung,
    Our SAP System version is ECC 6.0,I check the SMTP Node,it OK!
    But there are always error message(XS816) with “Message cannot be transferred to node SMTP due to connection error (final)”exist.
    I don’t know why?
    Could you help me?
    tks & bestregards.
    1. Shanmugaprabhu N

      i am also receiving the same msg(Message cannot be transferred to node SMTP due to connection error (final)”))
      i want to know the way to proceed further..
      pls suggest me..

      thanks and regards

      1. Thomas Jung Post author
        Check the logs in SCOT for more details. I suspect that you have some misconfiguration in yoru SMTP node configuration in SCOT but can’t say with only seeing this generic error message.
        1. Shanmugaprabhu N
          Hi Thomas,

          did u mean trace as the logs of SCOT or there exists any seperate log for SCOT?
          if so please give the path to find the log.

          thanks and regards,

      2. abdulrahim shaik

        I am also facing the same error now:  Below is the trace of SCOT.  Can anyone guide me further?  Thanks – Abdul

        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Recipient in queue flagged
        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Structure: SOSCO
        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Status written for this recipient
        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Structure: SOST
        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Transfer new queue entries to persistence service
        23.10.2009     11:18:42     SO_OBJECT_SEND     G     Status updated
        23.10.2009     11:18:52     SX_OBJECTS_SEND_ARFC     G     OBJECT_ID: RAW34000000000060
        23.10.2009     11:18:53     SX_OBJECTS_SEND_ARFC     G     RECIPIENTS: 1
        23.10.2009     11:18:53     SX_OBJECTS_SEND_ARFC     G     DOCUMENT_LANGUAGE: E
        23.10.2009     11:18:53     SX_CALL_GENERATE_NDR     G     Call of SX_PERFORM_SMTPSEND with SMTP M BASIS
        23.10.2009     11:18:53     SX_PERFORM_SMTPSEND     G     SAPconnect (E)SMTP Outbound Processing Started
        23.10.2009     11:18:53     SX_PERFORM_SMTPSEND     G     (E)SMTP Plug-In Available on App. Server. E-Mail Data Are Now Collected from BCS
        23.10.2009     11:18:53     SO_OBJECT_MIME_GET     G     SO_OBJECT_MIME_GET Called
        23.10.2009     11:18:53     SO_OBJECT_MIME_GET     G     SET_SCOM_KEY
        23.10.2009     11:18:53     SO_OBJECT_MIME_GET     G     ADR34000000000044
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     AS_MIME_MESSAGE Called
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     No MIME Message Exists in Send Order
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     No MIME Document Exists in Send Order
        23.10.2009     11:18:54     BCS->MIME_DOCUMENT_GENERATE     G     Generate MIME Document
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->AS_MIME     G     Start of MIME generation of a single body part
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Type:text/plain
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     charset:us-ascii
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Transfer-Encoding:quoted-printable
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Description:test mail from Abdul
        23.10.2009     11:18:54     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Length of Body Part 000000000597
        23.10.2009     11:18:54     BCS->MIME_DOCUMENT_GENERATE     G     MIME of Document Received
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     MIME of Document Received
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     MIME_MESSAGE_GENERATE Called
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     TO Name:
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     TO Address:
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Sensitivity:
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Importance:N
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Encryption: Switched Off
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Signature: Switched Off
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Document Title: test mail from Abdul
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Trans.Time 20091023 111842
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Sender: BASIS ADMIN
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     SenderAddress:
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     MESSAGE-ID: ADR34000000000044
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Status Recip. Name: BASIS ADMIN
        23.10.2009     11:18:54     BCS->MIME_MESSAGE_GENERATE     G     Status Recipient Addr.:
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     MIME Message Created
        23.10.2009     11:18:54     BCS->AS_MIME_MESSAGE     G     Application suppressed storage of MIME message
        23.10.2009     11:18:54     SO_OBJECT_MIME_GET     G     MIME Object Successfully Returned
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     MIME GENERATION: 1233 ms
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     BCS Data Received. Recipient List (First 70 Characters with Status Code+Notify):
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G  S
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Sender First 50 Characters:
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Messages ID and Type: RAW34000000000060 M
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Length of Received RFC822/Mime E-Mail Data Stream: 1242
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Node Data: SMTP GREATSHIPGLOBAL.COM X
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Node Data: 25
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     CL_SMTP_REQUEST Successfully Created and Filled with Data
        23.10.2009     11:18:54     SX_PERFORM_SMTPSEND     G     Send Routine IF_SMTP_CLIENT->SEND Is Now Called.
        23.10.2009     11:18:54     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: S T EX IADR34000000000044 DGREATSHIPGLOBAL.COM Oab
        23.10.2009     11:18:54     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: Rramesh_parakala@gre
        23.10.2009     11:18:54     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: YS X
        23.10.2009     11:19:00     SX_GENERATE_NDR     G     Message cannot be transferred to node SMTP due to connection error (final)
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     Start of synchronous Status Update
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     Deleted from Queue: RAW34000000000060                 ADR34000000000044
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     SOES updated: ADR34000000000044
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     Structure: SOES
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     SOST updated
        23.10.2009     11:19:00     SO_SYNCHRON_STATUS_UPDATE     G     “Error” Status Set for Send Request
        23.10.2009     11:19:00     SO_STATUS_DOC_CREATE     G     STATUSMAIL_CONTENT_LEVEL 0
        23.10.2009     11:19:00     SO_STATUS_DOC_SEND     G     Statusmail will be sent to: BASIS
        23.10.2009     11:19:00     SO_STATUS_DOC_SEND     G     Start sending of Status Mail
        23.10.2009     11:19:01     SO_STATUS_DOC_SEND     G     Status Mail has been sent
        23.10.2009     11:19:01     SO_SYNCHRON_STATUS_UPDATE     G     End of synchronous Status Update
        23.10.2009     11:26:52     SX_OBJECTS_SEND_ARFC     G     OBJECT_ID: RAW34000000000060
        23.10.2009     11:26:52     SX_OBJECTS_SEND_ARFC     G     RECIPIENTS: 1
        23.10.2009     11:26:52     SX_OBJECTS_SEND_ARFC     G     DOCUMENT_LANGUAGE: E
        23.10.2009     11:26:52     SX_CALL_GENERATE_NDR     G     Call of SX_PERFORM_SMTPSEND with SMTP M BASIS
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     SAPconnect (E)SMTP Outbound Processing Started
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     (E)SMTP Plug-In Available on App. Server. E-Mail Data Are Now Collected from BCS
        23.10.2009     11:26:52     SO_OBJECT_MIME_GET     G     SO_OBJECT_MIME_GET Called
        23.10.2009     11:26:52     SO_OBJECT_MIME_GET     G     SCOM Key Already Exists
        23.10.2009     11:26:52     SO_OBJECT_MIME_GET     G     SOCS_INSERT 4
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     AS_MIME_MESSAGE Called
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     No MIME Message Exists in Send Order
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     No MIME Document Exists in Send Order
        23.10.2009     11:26:52     BCS->MIME_DOCUMENT_GENERATE     G     Generate MIME Document
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->AS_MIME     G     Start of MIME generation of a single body part
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Type:text/plain
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     charset:us-ascii
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Transfer-Encoding:quoted-printable
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Content-Description:test mail from Abdul
        23.10.2009     11:26:52     CL_BCOM_MIME_SINGLEPART->ADD_HEA     G     Length of Body Part 000000000597
        23.10.2009     11:26:52     BCS->MIME_DOCUMENT_GENERATE     G     MIME of Document Received
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     MIME of Document Received
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     MIME_MESSAGE_GENERATE Called
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     TO Name:
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     TO Address:
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Sensitivity:
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Importance:N
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Encryption: Switched Off
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Signature: Switched Off
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Document Title: test mail from Abdul
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Trans.Time 20091023 111842
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Sender: BASIS ADMIN
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     SenderAddress:
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     MESSAGE-ID: ADR34000000000044
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Status Recip. Name: BASIS ADMIN
        23.10.2009     11:26:52     BCS->MIME_MESSAGE_GENERATE     G     Status Recipient Addr.:
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     MIME Message Created
        23.10.2009     11:26:52     BCS->AS_MIME_MESSAGE     G     Application suppressed storage of MIME message
        23.10.2009     11:26:52     SO_OBJECT_MIME_GET     G     MIME Object Successfully Returned
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     MIME GENERATION: 97 ms
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     BCS Data Received. Recipient List (First 70 Characters with Status Code+Notify):
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G  S
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Sender First 50 Characters:
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Messages ID and Type: RAW34000000000060 M
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Length of Received RFC822/Mime E-Mail Data Stream: 1242
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Node Data: SMTP GREATSHIPGLOBAL.COM X
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Node Data: 25
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     CL_SMTP_REQUEST Successfully Created and Filled with Data
        23.10.2009     11:26:52     SX_PERFORM_SMTPSEND     G     Send Routine IF_SMTP_CLIENT->SEND Is Now Called.
        23.10.2009     11:26:52     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: S T EX IADR34000000000044 DGREATSHIPGLOBAL.COM Oab
        23.10.2009     11:26:52     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: Rramesh_parakala@gre
        23.10.2009     11:26:52     CL_SMTP_REQUEST->GENERATE_XSTRIN     G     SMTP-ENVELOPE: YS X
        23.10.2009     11:26:57     SX_GENERATE_NDR     G     Message cannot be transferred to node SMTP due to connection error (final)
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     Start of synchronous Status Update
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     Deleted from Queue: RAW34000000000060                 ADR34000000000044
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     SOES updated: ADR34000000000044
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     Structure: SOES
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     SOST updated
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     “Error” Status Set for Send Request
        23.10.2009     11:26:57     SO_STATUS_DOC_CREATE     G     STATUSMAIL_CONTENT_LEVEL 0
        23.10.2009     11:26:57     SO_STATUS_DOC_SEND     G     Statusmail will be sent to: BASIS
        23.10.2009     11:26:57     SO_STATUS_DOC_SEND     G     Start sending of Status Mail
        23.10.2009     11:26:57     SO_STATUS_DOC_SEND     G     Status Mail has been sent
        23.10.2009     11:26:57     SO_SYNCHRON_STATUS_UPDATE     G     End of synchronous Status Update

  4. Abhishek Suppal
    I am using FM SO_NEW_DOCUMENT_SEND_API1 to send the mail. I want to change the Sender name and Email id. How can i achieve this?

            document_data              = l_doc_data
            put_in_outbox              = ‘X’
            commit_work                = ‘X’
            new_object_id              = l_object_id
            object_content             = li_objcont
            receivers                  = li_receiver
            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.

    1. Thomas Jung Post author
      The easiest way if you are basis release 6.10 or higher is to use the CL_BCS class based interface to send email instead.  This lets you supply the sender ID.

      The older function module automatically uses the sy-uname.  Therefore the way that you send with a different ID is to schedule the function module call within a background job and setup that job to run under the user ID that you want to have as the sender.

  5. olivier allain
    Can-you help me ?
    When i send a mail by my program ABAP with the MF
    SO_DOCUMENT_SEND_API1, my mail is not send with an external adress.
    The message is 672
    Can you known why ?
    My programm is :
    *& Form send_mail
    * text
    * –> p1 text
    * <– p2 text
    FORM send_mail USING domaine TYPE c p_receiver TYPE c.

    CLEAR: maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.
    REFRESH: mailtxt, mailbin, mailpack, mailhead, mailrec.

    * Creation mail
    CONCATENATE text-ms1 domaine INTO maildata-obj_descr SEPARATED BY space. “titre mail
    maildata-obj_langu = sy-langu. ” Langue
    * maildata-sensitivty = ‘F’. ” Type message essai pour transfert
    maildata-doc_size = 1.

    * Corps du message
    CONCATENATE text-c01 domaine INTO mailtxt-line SEPARATED BY space.
    APPEND mailtxt.

    * Prepare Packing List
    PERFORM prepare_packing_list USING domaine.

    * Liste des destinataires
    mailrec-receiver = ‘ALLAIN’.
    * mailrec-rec_type = ‘B’.
    * mailrec-express = ‘X’.
    APPEND mailrec.
    mailrec-receiver = p_receiver.
    * mailrec-rec_type = ‘B’.
    * mailrec-express = ‘X’.
    APPEND mailrec.

    document_data = maildata
    * put_in_outbox = ‘ ‘
    * commit_work = ‘ ‘
    packing_list = mailpack
    object_header = mailhead
    contents_bin = it_attachment
    contents_txt = mailtxt
    receivers = mailrec
    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.

    ENDFORM. “send_mail

    FORM prepare_packing_list USING p_domaine TYPE c.

    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.

    * Nom du fichier si export vers windows
    CONCATENATE ‘Domaine_ ‘ p_domaine ‘.xls’ INTO mailhead.
    APPEND mailhead.

    DESCRIBE TABLE it_attachment LINES tab_lines.
    mailpack-transf_bin = ‘X’.
    mailpack-head_start = 1.
    mailpack-head_num = 1.
    mailpack-body_start = 1.
    mailpack-body_num = tab_lines.
    mailpack-doc_type = ‘XLS’.
    mailpack-obj_name = p_domaine.
    CONCATENATE ‘Domaine ‘ p_domaine ‘.xls’ INTO mailpack-obj_descr.
    mailpack-doc_size = tab_lines * 255.
    APPEND mailpack.
    ENDFORM. “prepare_packing_list

    1. Thomas Jung Post author
      I don’t have all the error codes memorized so I wouldn’t know off hand what 672 is.  You should look at the Overview of sent items report in SCOT for more details. Chances are there is a misconfiguration somewhere.  Try sending a message via the Short Message option in the SAPGUI to test the connection and make sure that your problem is the source of your problem.  Also in general consider switching to the CL_BCS classes described in this blog instead of using the old SO_DOCUMENT function module.  That isn’t likely the cause of your problem – but just a general suggestion.
    2. olivier allain
      Excuse me.
      The message 672 is “Still no entry in queue”.
      I can send the message with SCOT when i use the icon repeat send in menu “overview of send orders”.
      Can y make a BTCI to repeat send automaticaly ?
      1. Thomas Jung Post author
        You can’t do send immediately with the old Function Module based interface. Only the new CL_BCS has the send immediately option.
      1. Abel Isaac Polo
        Hi Oliver,

        I am impressed you were able to send an EXCEL file just with your declaration below:

        mailpack-doc_type = ‘XLS’.

        Could you please send me the code where you fill up the it_attachment internal table.

        Abel Polo

  6. Theresa Hayes

    Excellent blog!  Quick question: do you know if it is possible to delay the delivery of emails?  What I’d like to do within ABAP is create and email today but delay it’s delivery for a week (or a set date).  Any assistance is greatly appreciated.


    1. Thomas Jung Post author
      I didn’t see anything in the BCS classes to allow for this. I would think you could build it in at your application level. You could have your application schedule a background job for the set date/time and that background job would be what actually sends the email.
  7. Anonymous
    We implemented an email solution with PDF-attachment successfully in CRM. The content is passed with a Smartform. The OTF output of the Smartform is converted to PDF with Function Module CONVERT_OTF. The FM’s output-XSTRING is converted to a SOLIX_TAB and added as a PDF-attachment to the mail document. This works fine.
    However now we get our content for the PDF-attachment not from a plain-text editor but from a BTF-editor in CRM. This text contains lots of HTML-tags, which are visible now in the content-Smartforms OTF-output and thus in the PDF attachment as well.
    What we want is nicely formatted text in our PDF, just like the BTF-editor. I guess we shoud use the XML-output of the Smartform, but how can this be converted to PDF?
    1. Thomas Jung Post author
      Your question doesn’t really seem directly related to this blog. Regardless of if you are ultimately emailing the output, the problem actually sounds like the HTML based BTF output not really being compatible with the SmartForm.  I’m not sure I have any advice for you on that topic other than to try to remove BTF HTML tags before sending the content into the Smartform.
      1. Community User

        I tried sending an email with PDF attachment. I could see the English words in correct format, however all the Chinese Text were converted to “U”. Can you help me in finding out the solution what could be the way of displaying the data in PDF with respective languages?

        Warm Regards,

  8. Praveen Kumar Sembeti

    I have a requirment to send email with CSV file attachment which contains some Russian Text. I am getting email but when I opened the attachment it has some junk text. I am using FM SO_NEW_DOCUMENT_ATT_SEND_API1 for sending email and RU language is installed in the System also. Kindly provide your valuable feedback on this issue.


    1. Thomas Jung Post author
      What is the logon language used to send the email?  Is your system Unicode, Single Code Page (if so, what code page), or MDMP?  What format is the attachment in – Text or Binary?

      Ideally if you have a mixture of langauges, you would want to use Unicode and convert your attachment to binary, so that it won’t go through any further text conversions.

      1. Praveen Kumar Sembeti
        Hi Thomas,

        Thanks for your quick reply.

        I tried to trigger email by logging in both RU and EN, but in both cases iam getting junk in the attachment and my system is Unicode and the format is Binary only.

        When i opened the CSV attchment with text i can see the Russian Text ( it is asking for encoding format, i selected UTF-8 encoding), but when i opened directly by double clicking it shows junk( ;Счет-Проформа №;1016641501;от;20090817;;;;;;;). is this something due to encoding??

        1. Thomas Jung Post author
          How are you building the CSV document?  With concatentaitons and then a conversion to binary?  Perhaps you should consider attaching a BOM (Byte Order Mark) to the front of the document so that desktop applications know that it is UTF-8. Constants for the UTF-8 Byte Order Mark can be found in class CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_UTF8
            1. Thomas Jung Post author
              Have you tried adding a BOM?  Since you say that if you force the encoding on the client side the file opens OK, then the content is correct.  The BOM should help the client application know what encoding and how to open the file.
                1. Thomas Jung Post author
                  You can attach a BOM to the front of any binary content.  The email function module isn’t going to care what is the binary content it is passed. Just concatenate the BOM constant onto the front of your content as you are building it.
  9. Amit Gupta

    Within Few days I am facing problem in the Smartform data in the form of excel file attached in the mail.
    I am able to fill the exact data in XLS form in OBJBIN table as I have downloaded the contents of OBJBIN
    in Debugging mode as XLS file and the file is opening properly.

    The real problem comes when I try to pass the contents of OBJBIN to Function module
    Then mail attachment(XLS file ) wont open properly.

    So can anyone please help me regarding this.
    Am I using the wrong parameters????

    We are using ECC6 system.

    Amit Gupta

    1. Thomas Jung Post author
      First why use SO_DOCUMENT_SEND_API if you are on ECC 6?  Why not use the newer CL_BCS API that this blog is based upon? How are you backing the CONTENTS_BIN?  Since you have a very specific question about an attachment, perhaps you should post this question in the SDN Forum.
      1. Amit Gupta
        Hi Thomas ,

        Thanks for replying and guiding me.
        Now I am using CL_BCS for attachment and sending mail.
        Can you please help me in filling the parameters of
        cl_document_bcs=>create_document and
        document->add_attachment for ‘XLS’ file.

        thanks and regards,

          1. Amit Gupta

            My excel file output looks like:                                                                                <LocationOfComponents HRef=”file:///
            ” />                                                                                                      

              1. Amit Gupta
                Hi Thomas,

                Thanks for the quick reply…
                My main problem is that the data in xls file is coming in XML format.
                When I opened my mail , XLs file attachment shows the data in XML format which is not needed.
                I need the actual proper XLS file with proper data..
                How to fix this thing…


                1. Thomas Jung Post author
                  Make sure your attachement has XLS as the file extension.  Also make sure your version of Excel on the desktop can handle the newer Excel XML format.
                  1. Amit Gupta
                    Hi Thomas,

                    Office 2007 is installed in my machine..
                    Also in SOST the data in XLS file is in XML format.
                    So i think problem is in the accumulation of data.
                    Please suggest..


                    1. Thomas Jung Post author
                      The building of an XML Excel file is well outside the scope of this blog.  I would suggest that if you believe the problem is with the content itself, you post that question on the SDN Forums. 
                      1. Amit Gupta
                        Hi Thomas,

                        I have posted this problem in SDN forum but i haven’t got any satisfactory answer ,
                        I guess only you could help me in this case…
                        I have read in your blog somewhere about Byte Order mark , can you help me in adding BOM to the front of my binary contents…

                        And one thing also , we passs binary content to content_hex or content_bin.


                        1. Thomas Jung Post author
                          You only need a Byte Order Mark if your binary content is encoded in UTF-16.  UTF-8 doesn’t require require a BOM.  What encoding did you create your binary string output for the XML in?  If you want to add the BOM, just concatenate the correct BOM using the constants in CL_ABAP_CHAR_UTILITIES.  UTF-16 LE (Little Endian) is what you use for Windows based files.

                          If you are using ADD_ATTACHMENT of the CL_BCS class based mail interfaces there is only options for I_ATT_CONTENT_TEXT or I_ATT_CONTENT_HEX.  For this content you would would want I_ATT_CONTENT_HEX because hopefully you have binary string that you have parsed down to an internal table of type SOLIX_TAB, using CL_DOCUMENT_BCS=>XSTRING_TO_SOLIX.

                          1. Amit Gupta
                            thanks thomas for giving such a nice blog…..
                            I have figured out the real problem in my coding but now problem is
                            that french accents are lost in my excel file and I am using UTF-8 encoding….
                            How cud I preserve those accents like :



                            1. Thomas Jung Post author
                              We are really far outside the scope of this blog since this all relates to how you are building your attachment.  I suggest that you post this question in the appropriate forum.  Provide the code that you are using to do the conversion to Excel.
  10. Surya Narayan Nanda
    Hi Thomas am using the class concept
    for sending an attached Excel sheet by mail .

    In the form of one Excel sheet .
    Its working absolutely fine if am taking as a CSV file but the problem in .XLS is everything is coming in one column its not dividing the data at column levels. ow to do that ?

    1. Thomas Jung Post author
      How are you building the CSV?  Perhaps you should post this question in the SDN Forums.  I’m not sure how central it is to how-to send an email.
  11. Brian Meier
    Thanks for an excellent blog.  We want to send HTML as the body of the email. However, we only see BIN “Binary” and RAW “Raw” as the formats for the generation of the body of the email.  Is there a simple way to sent HTML as the body of the email?


    1. Thomas Jung Post author
      HTML is just text.  I’ve got an HTML email being built like this:

            i_type = ‘HTM’
            i_text = l_mailtext
            i_subject = l_subject ).

      l_mailtext is type soli_tab.  I just build up my HTML as a string and then conver to SOLI_TAB with either function module SCMS_STRING_TO_FTEXT’ or in later releases CL_DOCUMENT_BCS=>STRING_TO_SOLI.

  12. Anusha Ahuja
    Hi Thomas,
    Thanks for sharing the information.
    1 Question – Will this function module work on both Windows-based as well as Unix-based systems?
    1. Thomas Jung Post author
      Yes, of course it works on all platforms that the ABAP AS runs on. It has no connection to or dependency upon the OS of the server. The SMTP stack is native in the ABAP kernel as of AS 6.10 and higher.
      1. Anusha Ahuja
        Thankyou for the clarification.

        I noticed that the contents in the email body (First record – RAW type) get converted to CAPITALS.
        Is there any way to keep them as they are typed (i.e. only the required alphabets as capital)?

        1. Thomas Jung Post author
          The email process itself doesn’t convert anything to upper case.  If you are getting conversion then check all the variables you are using to hold the data before putting into the email. If the domains of the data types of these fields don’t support Upper/Lower, then ABAP naturally converts to upppe case. 
    1. Thomas Jung Post author
      Build up your HTML in a single string instead of 255 character rows in an internal table. That way you aren’t wrapping content between lines in the table.  Then use the static method CL_BCS_CONVERT=>STRING_TO_SOLIX( ).  This will turn your string into an internal table of type SOLIX_TAB.  The lines of the table will no longer represent a line in the content.  You can have single lines of content that are more than 255 characters.  The content is now stored binary and contains newline and carrage return characters inline to mark the end of line.  This is also a more efficient way of storing the data since there is no wasted space.
      1. Vasu Mullapudi
        Hi Thomas,

        This solution helped me getting the URL string without truncation. But the problem is the email-content is displayed as an attachment but not in the email body.

        Appreciate your suggestions.

        Thank you,

        1. Thomas Jung Post author
          Are you using this function module?  If so the first document in the DOCUMENTS importing parameter will always be used as the email body.  Formatting the content as a string won’t effect this.
          1. Vasu Mullapudi
            Hi Thomas,

            I’m using the following code. But still the text is displayed as an attachment. Email-body is empty
            If I use ‘RAW’ doc type its displayed in the email body but it truncates the URL.

                  CALL METHOD cl_bcs_convert=>string_to_solix
                      iv_string   = url
                      iv_codepage = ‘UTF-8’
                      et_solix    = et_solix
                      ev_size     = length

                  document = cl_document_bcs=>create_document(
                  i_type = ‘BIN’
                  i_hex = et_solix
                  i_subject = l_subject ).

               send_request->set_document( document ).

            Thank you,

  13. David C.

    Lets say that the recipient is maintained, why this is always executed?

    recipient = cl_cam_address_bcs=>create_internet_address( recipients_line-c_address ).

    You will create a new registry in ADR6 each time…does it make sense?

  14. Nilesh Masurkar
    Hi Thomas,
    This is indeed brilliant. I have a small query re: sending pdf attachement. I am using the FM CONVERT_OTFSPOOLJOB_2_PDF to convert the spool o/p of an sap script into pdf. Can you please guide me how can i use your fm to send this pdf as an attachment?


    1. Thomas Jung Post author
      CONVERT_OTFSPOOLJOB_2_PDF exports the PDF document as an XSTRING using the parameter BIN_FILE.  If you want to add that as an attachment, I would suggest using the utility static method CL_BCS_CONVERT=>XSTRING_TO_SOLIX.  That will convert the XSTRING PDF into the table format needed for attachments.
  15. saslove sap
    Hi Thomas,
    Thanks for the wonderful blog which the intention of SAP developing all the new functionailities are reaching the users ..
    I have one small issue although its not complete related to this….
    User want to input multiple MAIL ID entries which is sucesfully achieving using select-options over a long of period .Its works fine But i got a new issue that  user want to input few email ids which are more than 45 character…unable to support by select-options…Is there a way we can achieve ?
    Is any chance of SAP coming with new patch for this?
    1. Thomas Jung Post author
      What release level are you on?  I assume it is an older release level.  On NetWeaver 7.0, select-options are limited to 255 characters, not 45 characters.
      1. Thomas Jung Post author
        >On NetWeaver 7.0, select-options are limited to 255 characters, not 45 characters

        Actually a little deeper research on my part – the increase of the size to 255 characters doesn’t ship until NetWeaver 7.02.

  16. Anonymous
    Thanks for the blog Thomas…  I have a question/problem I was hoping you might be able to help me with…

    I am creating a Tab delimited Excel spreadsheet to be attached to my email.

    If I use type = ‘RAW’ and call your function the file extension always comes out on the email as ‘TXT’, even though I specify ‘XLS’ in the subject (it double tags the file – “fname.XLS.TXT”).

    If pass the data in the CONTENT_TEXT table and rename the resulting attachment to XLS, Excel opens the attachment with no problems.

    I want to have the attachment extension = ‘XLS’ so the user doesn’t need to “Save As” XLS and then open; I want them to be able to doubleclick and have Excel start automatically.

    I have looked through the classes (CL_DOCUMENT_BCS and CL_BCS) for a way to drive the extension name but have found nothing.

    Is there something I am missing or is this something driven internally that cannot be changed?

    Thanks in advance

    1. Thomas Jung Post author

      Have you tried setting the TYPE = ‘XLS’ and passing the content as binary (content_hex)? This should do the trick.  I generally take the file extension off the attachment filename and use that as the type directly:    find first occurrence of regex ‘\.[^\.]$'<br/>      in <wa_attach>-filename match offset dot_offset.<br/>    add 1 to dot_offset.<br/>    extension = <wa_attach>-filenamedot_offset.<br/>    attachment_type = extension.<br/>    translate attachment_type to upper case.<br/><br/>    document->add_attachment(<br/>        i_attachment_type = attachment_type<br/>        i_att_content_hex = l_mailhex<br/>        i_attachment_subject = l_subject ).<br/>  endloop.

    2. Anonymous
      It worked like a charm and there’s no File->Save As or other workaround required!

      Thanks for the help Thomas. 

  17. Bruce Tjosvold

    We have been using this FM successfully for five years. Now today, intermittently the FM is abending at the line “raise exception bcs_exception.”. The FM is executed inside of a method called by a foreground task of a workflow. We are on basis 620 and applications 4.7.

    This FM is called successfully by other methods called by a foreground task of a workflow. The FM is also called by many reports. I just successfully ran a report that uses this FM.

    function z_620_email_or_office_bcs .


    * Send document
    call method send_request->send( ).

    commit work.

    catch cx_bcs into bcs_exception.
    raise exception bcs_exception. ” <<<<<<br/>*
    Run time error log text follows

    The exception ‘CX_SEND_REQ_BCS’ was raised but was not caught at any stage in the call hierarchy.

    Any ideas what the problem is or how to correct it?


  18. Saikat Basak
    Hi Thomas,

    I have used your function module to send external mail . But I am facing a problem. When I am executing the FM through SE37 its working fine. I am getting the mail in my external mail ID. But when I am using it inside a BADI it is not working.

    I have created custom BADI implementation z_HRPAD00INFTY of the BADI is HRPAD00INFTY. I have wrote my code inside the method AFTER_INPUT.

    Could you please help to understand the issue.

    1. Thomas Jung Post author
      I couldn’t say what this BADI is doing since I don’t know anything about the surrounding application.  The Email send needs a commit work to trigger the send.  Perhaps the surrounding application is triggering a rollback instead.  Consider starting the email send process via CALL FUNCTION  … STARTING NEW TASK so that you can issue your own commit work.
  19. Felice Kiew
    Thanks for the powerful FM.
    Everything ok when i run the program until i schedule it as backgound job, only email being sent, my attachment disappear.

    I really out of mind, hope you could see anything wrong in using the FM.
    *& Report  ZTESTMAIL

    REPORT  zhr_ee_confirm.                                                .
    include RSAQEXCD.

    * **************************************************************
    * basis: declaration of add. tables, fields, structures, etc.
    * **************************************************************

      PERFORM p_file_open_dialog.



    PERFORM p_process_file USING p_file.

    DATA: w_tab_lines TYPE i.           ” Table lines

    *           CLIENT                  = SY-MANDT
              ID                      = ‘ST’
              LANGUAGE                = ‘E’
              NAME                    = ‘MAIL_FOR_I0019_STEXT’
              OBJECT                  = ‘TEXT’
    *         ARCHIVE_HANDLE          = 0
              HEADER                  = HTEXT
              LINES                   = LTEXT
              ID                      = 1
              LANGUAGE                = 2
              NAME                    = 3
              NOT_FOUND               = 4
              OBJECT                  = 5
              REFERENCE_CHECK         = 6
              WRONG_ACCESS_TO_ARCHIVE = 7
              OTHERS                  = 8.

    *Start of text_symbol_replace*
    REPLACE ‘&P0019-TERMN&’ with lv_date1 INTO DTEXT-TDLINE.

    MOVE DTEXT-TDLINE TO wa_objtxt-line.
    APPEND wa_objtxt TO t_objtxt.
    clear DTEXT.
    clear wa_objtxt.
    *End of text_symbol_replace*

    *&      Form  send_message
    *       Sending Mail
    DOCUMENTS = lt_attach
    RECIPIENTS = lt_rec
    MESSAGE = t_objtxt
    SUBJECT = ‘Confirmation Review’.
    clear lt_rec.
    clear ls_rec-i_copy.
    clear lt_attach.
    clear t_objtxt.
    *&      Form  p_file_open_dialog
    *       text
    FORM p_file_open_dialog.
      DATA ls_filetable       LIKE LINE OF gt_filetable.
      CALL METHOD cl_gui_frontend_services=>file_open_dialog
          file_table              = gt_filetable
          rc                      = gv_rc
          file_open_dialog_failed = 1
          cntl_error              = 2
          error_no_gui            = 3
          not_supported_by_gui    = 4
          OTHERS                  = 5.
      IF sy-subrc EQ 0.
        READ TABLE gt_filetable INTO ls_filetable INDEX 1.
        p_file = ls_filetable-filename.
    ENDFORM.                    “p_file_open_dialog

    *&      Form  p_upload_file
    *       text
    *      –>FILENAME   text
    FORM p_process_file USING filename.
      DATA lv_filename        TYPE string.
      DATA lt_data            TYPE solix_tab.
      DATA lt_data1            TYPE solix_tab.
      DATA lv_filelength      TYPE i.
      DATA lv_datax           TYPE xstring.
      DATA lv_clstr_indx      TYPE indx_srtfd.
      DATA lv_str_filename    TYPE string.
      DATA lv_junk            TYPE string.
      DATA lv_filetype        TYPE string.
      DATA lt_ftext           TYPE soli_tab.
      DATA ls_return          TYPE bapiret2.
      DATA lw_xstring         TYPE xstring.

              TEXT_FIELD(1000) TYPE C,
            END OF TY_TEXT.
              BINARY_FIELD(1000) TYPE C,
            END OF TY_BINARY.

    DATA : LT_BINARY type table of TY_BINARY with header line.
    DATA : LT_TEXT type table of TY_TEXT with header line.
    DATA : LT_TEXT_OUT type table of TY_TEXT with header line.
    DATA : i_contents_bin TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE.
    DATA : solix TYPE LINE OF solix_tab.
    DATA : lr_conv_ci TYPE REF TO cl_rpe_convert.
    DATA : w_s TYPE string.
    DATA : w_s0 TYPE xstring.
    DATA : w_s1 TYPE xstring.

    IF p_app EQ ‘X’. “application server
    *  MOVE filename TO lv_filename.
      MOVE filename TO lv_filename.
    * Upload file to read data as binary
        CALL METHOD cl_gui_frontend_services=>gui_upload
            filename                = lv_filename
            filetype                = ‘BIN’ “Binary Mode
            filelength              = lv_filelength
            data_tab                = lt_data
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            not_supported_by_gui    = 17
            error_no_gui            = 18
            OTHERS                  = 19.

    *LOOP AT lt_data INTO solix.
    *    CONCATENATE w_s0 solix-line INTO w_s0 IN BYTE MODE.
           input_length       = lv_filelength
           buffer             = w_s0
           binary_tab         = lt_data
           OTHERS             = 2.

    *Convert encoded string to unicode string
             FROM_CODEPAGE       = ‘1100’
              IN_XSTRING          = w_s0
             OUT_LEN             = 0
             OUT_STRING          = w_s.

    *Uploading Files and Manipulating their Content.
    REPLACE ‘lv_name_xxxxxxxxxxxxxxxxxxxxxx’ with lv_name into w_s.
    REPLACE ‘lv_pernr’ WITH lv_pernr into w_s.

    *Convert unicode string into xstring
             CODEPAGE_TO            = ‘1100’
              UNICODE_STRING         = w_s
             OUT_LEN                = 0
             XSTRING_STREAM         = w_s1
             INVALID_CODEPAGE       = 1
             INVALID_STRING         = 2
             OTHERS                 = 3.

    * Convert xstring to binary.
        BUFFER                = w_s1
    *   APPEND_TO_TABLE       = ‘ ‘
    *   OUTPUT_LENGTH         =
        BINARY_TAB            = lt_data1

    * Get Stripped file name
    IF lt_data1[] IS NOT INITIAL.
            full_name     = lv_filename
            stripped_name = lv_str_filename
            x_error       = 1
            OTHERS        = 2.
        SPLIT lv_str_filename AT ‘.’ INTO lv_junk lv_filetype.
        ls_attach-type = lv_filetype.
        ls_attach-subject = lv_str_filename.
    *    ls_attach-CONTENT_TEXT[] = t_objtxt[].
        ls_attach-CONTENT_HEX[] = lt_data1[].
        APPEND ls_attach TO lt_attach.
        CLEAR ls_attach.

    ENDFORM.                    “p_upload_file

    1. Thomas Jung Post author
      Your program seems to be designed to upload a file from a PC using the SAPGUI. When you are running in the background, you aren’t going to have any connection to a client machine and therefore no way to call SAPGUI commands to upload files.
      1. Felice Kiew
        Thanks Thomas. Does it mean if i upload the file from SAP directory instead of PC, could i schedule the program as a background job ?

        If not, can you help point me some tips how i able change the design in order to schedule as a job ?
        i need a reminder program which check Staff probation expiry everyday and sent confirmation form to respective HR and Mgr.

        Binary mode will read the file as xstring
            IF sy-subrc EQ 0.
              READ DATASET p_file INTO lw_xstring.
              CLOSE DATASET p_file.
            IF lw_xstring IS NOT INITIAL.
        * Converts xstring to solix
                  buffer     = lw_xstring
                  binary_tab = lt_data.

        1. Thomas Jung Post author
          >Does it mean if i upload the file from SAP directory instead of PC, could i schedule the program as a background job ?

          Sure. You can read files off the application server file system in a background job just fine.

  20. Fran├žois BOBET
    First as we said in french “Chapeau l’artiste”, very good job.

    second : is-it possible ? and how ?
    I need using very long name for attach (PDF and CSV) both in the same mail, but i_attachment_subject is CHAR 50 and I need 80 or 100
    Best regards François

    1. Thomas Jung Post author
      Sorry but I don’t know any way to send in a subject larger than the interface allows. The interface uses the older SAP Object structure definitions (I assume for backwards compatibility) and you often find shorter char based fields instead of STRING.
      1. Fran├žois BOBET
        finaly I find a solution.
        try as show
        DATA :  lt_header TYPE soli_tab.
        CONCATENATE ‘&SO_FILENAME=’ lv_att_name INTO lv_string.
        APPEND lv_string TO lt_header.
        * add the spread sheet as attachment to document object
          i_attachment_type = ‘XLS’ 
          i_attachment_subject ‘CSV_FILE’
          i_attachment_header  = lt_header
          i_attachment_size    = size
          i_att_content_hex    = binary_content ).

        I can’t find documentation about header, but may be there are lot’s of parameter ‘&SO_…’

        Best regards

  21. sap csb

    Hi Thomas,<br/><br/>Thanks for the blog.<br/><br/>I have developed the report with the help of blog for sending email with excel attachment.<br/><br/>My problem is date in excel attachment is truncating. <br/>I am using SAP 4.7 Version.<br/>Below is my code:<br/>Preparing contents of attachment <br/>  LOOP AT gt_outtab ASSIGNING <dy_wa>.<br/>    IF sy-tabix = 1.<br/>      CLEAR l_text.<br/>      CONCATENATE text-011<br/>                  text-012<br/>                  text-013<br/>                  text-014<br/>                  text-015<br/>                  text-016<br/>                  text-017<br/>                  text-018<br/>                  text-019<br/>                  text-020<br/>                  text-021<br/>                  text-022<br/>                  text-023<br/>                  text-024<br/>                  text-025<br/>                  text-026<br/>                  text-027<br/>                  text-028<br/>                  text-029<br/>                  text-030<br/>                  text-031<br/>                  text-032<br/>                  text-033<br/>                  text-034<br/>                  text-035<br/>                  text-036<br/>                  text-037<br/>                  text-038<br/>                  text-039<br/>                  text-040<br/>                  text-041<br/>                  text-042<br/>                  text-043<br/>                  text-044<br/>                  text-045<br/>                  text-046<br/>                  text-047<br/>                  text-048<br/>                  text-049<br/>                  text-050<br/>                  text-051<br/>                  text-052<br/>                  text-053<br/>                  text-054<br/>                  text-055<br/>                  text-056<br/>                  text-057<br/>                  text-058<br/>                  text-059<br/>                  INTO l_text SEPARATED BY c_tab.<br/>      CONDENSE l_text.<br/>      APPEND l_text TO i_attach.<br/>      CLEAR l_text.<br/>    ENDIF.<br/>    DO.<br/>      ASSIGN COMPONENT sy-index OF STRUCTURE <dy_wa> TO <dy_field>.<br/>      IF sy-subrc NE 0.<br/>        CONCATENATE c_cr l_text INTO l_text.<br/>        APPEND l_text TO i_attach.<br/>        EXIT.<br/>      ELSE.<br/>        CLEAR l_con.<br/>        MOVE <dy_field> TO l_con.<br/>        CONDENSE l_con.<br/>        IF sy-index = 1.<br/>          CLEAR l_text.<br/>          MOVE l_con TO l_text.<br/>        ELSE.<br/>          CONCATENATE l_text l_con INTO l_text SEPARATED BY c_tab.<br/>        ENDIF.<br/>      ENDIF.<br/>    ENDDO.<br/>  ENDLOOP.<br/><br/>  CONCATENATE sy-uzeit0(2) ‘:’ sy-uzeit2(2) INTO lv_time.<br/><br/>  CALL FUNCTION ‘CONVERT_DATE_TO_EXTERNAL'<br/>    EXPORTING<br/>      date_internal            = sy-datum<br/>    IMPORTING<br/>      date_external            = lv_date<br/>    EXCEPTIONS<br/>      date_internal_is_invalid = 1<br/>      OTHERS                   = 2.<br/>  IF sy-subrc <> 0.<br/> MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO<br/>*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.<br/>  ENDIF.<br/><br/><br/>  CONCATENATE text-088 lv_date lv_time INTO<br/>                                    lv_sub SEPARATED BY ‘ / ‘.<br/>  TRY.<br/>      l_send_request = cl_bcs=>create_persistent( ).<br/>Creating Document<br/>      l_document = cl_document_bcs=>create_document( i_type = ‘RAW'<br/>                                                     i_text = i_content[]<br/>                                                     i_subject = lv_sub ).<br/><br/>      DESCRIBE TABLE gt_outtab LINES l_lines.<br/>Size to multiplied by 2 for UNICODE enabled systems<br/>      l_size = l_lines * 2 * 255.<br/>Adding Attachment<br/>      CALL METHOD l_document->add_attachment<br/>        EXPORTING<br/>          i_attachment_type    = c_ext<br/>          i_attachment_size    = l_size<br/>          i_attachment_subject = ‘List of Orders'<br/>          i_att_content_text   = i_attach[].<br/><br/>Add document to send request<br/>      CALL METHOD l_send_request->set_document( l_document ).<br/>Get Sender Object<br/>      l_uname = sy-uname.<br/>      l_sender = cl_sapuser_bcs=>create( l_uname ).<br/>      CALL METHOD l_send_request->set_sender<br/>        EXPORTING<br/>          i_sender = l_sender.<br/>E-Mail<br/>      TRANSLATE p_mailto TO LOWER CASE.<br/>      l_recipient = cl_cam_address_bcs=>create_internet_address( p_mailto ).<br/><br/>      CALL METHOD l_send_request->add_recipient<br/>        EXPORTING<br/>          i_recipient  = l_recipient<br/>          i_express    = ‘U'<br/>          i_copy       = ‘ ‘<br/>          i_blind_copy = ‘ ‘<br/>          i_no_forward = ‘ ‘.<br/><br/>      TRANSLATE p_mailcc TO LOWER CASE.<br/>      l_recipient = cl_cam_address_bcs=>create_internet_address( p_mailcc ).<br/><br/>      CALL METHOD l_send_request->add_recipient<br/>        EXPORTING<br/>          i_recipient  = l_recipient<br/>          i_express    = ‘U'<br/>          i_copy       = ‘X'<br/>          i_blind_copy = ‘ ‘<br/>          i_no_forward = ‘ ‘.<br/><br/>*Trigger E-Mail immediately<br/>      l_send_request->set_send_immediately( ‘X’ ).<br/><br/>      CALL METHOD l_send_request->send( ).<br/>      COMMIT WORK.<br/>    CATCH cx_document_bcs INTO l_bcs_exception.<br/>    CATCH cx_send_req_bcs INTO l_send_exception.<br/>    CATCH cx_address_bcs INTO l_addr_exception.<br/>  ENDTRY.<br/>ENDFORM.                    ” f_send_email<br/><br/>Please guide me..

  22. Ray Mannion
    Hi Thomas,
    Thanks for all your excellent posts.  I was wondering if you could explain how to get the new_object_id that we used to get with SO_DOCUMENT_SEND_API1.

    I am getting the document info by calling the three methods get_docno, get_docyr, get_doctp but that doesn’t seem to be the correct set.

      call method send_request->get_docno
          result = ls_itab-objno.

    Thanks for any suggestions you can offer.

    1. Thomas Jung Post author

      Yes I am afraid it appears the migration has destroyed all formating and in the process removed all the code blocks from the blog completely. In my opinion it is SCN’s repsonibility to migrate blogger’s content.  I’m quite disappointed to see that SCN has lost this content and can only hope they find a way to restore it. I don’t necessarily have backups of every blog I wrote and even if I did I don’t find the prospect of repairing them manually very appealing.

      1. Alejandro Bindi

        Approximately 1 month has passed and the contents are still lost…I hope not forever. I can’t imagine how you feel given the amount of blogs and other content you have contributed to SDN, of course I don’t expect you to restore it.

        I still have faith that a common solution is being searched for to restore at once ALL the blogs and lost content, and not in a case by case basis as some posts I read suggest.

        1. Thomas Jung Post author

          I’m very frustrated by this as well.  This is one of my most viewed blogs of all time and now it is rendered effectively unreadable and missing large sections of content. On one of my other blogs I asked if the content could be restored.  I received an email with the original content in it.  I then manually had to reformat it and reconstruct the blog.  Even at that, I lost all links to images.  The whole process for one rather small blog took over an hour to clean up. Frankly there is no way I have the time to do this for all my older, larger blogs.  It is looking like the content is gone forever.


Leave a Reply