Introduction
There are many APIs to send emails programmatically, but you should prefer CL_BCS class as said in note 190669 for sending lists via SAPconnect using the BCS interface:
As of Release 6.10, we recommend that you no longer use the API1 interface for sending documents. Instead, use the BCS interface.
Compared to the API1 interface, the BCS interface is easier to use since it is no longer required to fill a packing list. In addition, it offers more options (for example, setting any sender).
People have often many issues with the packing list, this parameter is difficult to understand, as binary should be stored in text parameter.
- Class CL_BCS (the BCS interface), to be used since release 6.10
- Older function modules:
- SO_DOCUMENT_SEND_API1
- SO_NEW_DOCUMENT_ATT_SEND_API1 is now a wrapper of SO_DOCUMENT_SEND_API1, so simply prefer to use SO_DOCUMENT_SEND_API1
- SO_NEW_DOCUMENT_SEND_API1
- SO_OBJECT_SEND
- There are also these function modules that some people use:
- EFG_GEN_SEND_EMAIL, but it is not released and cannot send to multiple recipients
- SO_DOCUMENT_REPOSITORY_MANAGER, but it is not released
The email is not sent immediately but collected, either SOST transaction has to be used to release them, or you can call RSCONN01 program.
Moreover, these APIs may also be used to send fax, SMS, or even send message only to Business WorkPlace (SAP integrated office, SBWP transaction).
Before being able to send mails, SAPconnect must be configured, using SCOT transaction.
Documentation
The reference documentations are the blogs of Thomas Jung:
- SDN blog - Sending E-Mail from ABAP - Version 610 and Higher - BCS Interface
- Older function modules: SDN blog - Sending E-Mail from ABAP - Version 46D and Lower - API Interface: he is using SO_NEW_DOCUMENT_ATT_SEND_API1.
Also, not related to sending, but to receiving mails:
FAQ
What is the simplest code to send an email?
Use this wiki. It is based on CL_BCS, but you don't need to know how object-oriented ABAP works.
How to build lines of more than 255 characters?
With CONTENTS_TXT parameter, lines can have a maximum of 255 characters. You should pass the text as binary as explained in this wiki.
Principle: fill a unique string of characters with lines separated by cl_abap_char_utilities=>cr_lf character, then call function modules SCMS_STRING_TO_XSTRING then SCMS_XSTRING_TO_BINARY (you may also simply call the static method CL_BCS_CONVERT=>STRING_TO_SOLIX) to turn your string into an internal table of type SOLIX_TAB. Pass it to CONTENTS_HEX parameter.
How to prove that the email has been sent?
Use put_in_outbox = 'X' parameter of SO_NEW_DOCUMENT_ATT_SEND_API1, Check if RECEIVER-RETRN_CODE = 0 or not after the function module, Also check if the exception DOCUMENT_NOT_SENT is being raised or not, SCOT customizing INT enter domains to which u can send mail.
What is RFC_MAIL?
This is a C program installed on SAPGUI that you can call to send a simple mail to one person. You cannot enter any subject. This program is not supported by SAP, it could be removed at any time. ABAP statement: CALL FUNCTION 'RFC_MAIL' DESTINATION 'LOCAL_EXEC' EXPORTING user = 'mail@abcde.com' TABLES mail = lt_textitab[].
How to enter tabulation character?
Use CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB. In non-unicode systems before 6.10, use data tab type x value '09' et concatenate 'text' tab 'text2' into text.
How to send an appointment?
Use CL_APPOINTMENT class
Do you want to open a customer message?
Troubleshooting
Mails are not sent
When you use the APIs, they are only collected. Either use SOST transaction to send them manually, or from program: submit rsconn01 with mode = 'INT' with output = ' ' and return.
It may be best to schedule a job with program RSCONN02 every 10 minutes when SAP system produces many mails.
Mails are no more sent after migration from 4.6C (or before) to 6.10 (or after, ECC5 or ECC6 for example)
You must add parameter COMMIT_WORK = 'X' when calling SO_NEW_DOCUMENT_SEND_API1
Note 873845 - HTML code in binary form cannot be read
Text document was incorrectly changed from system code page to recipient code page (set in SOST), though it was already in recipient code page. From SAPKP70005, SAPKB62057, SAPKB64016, SO_NEW_DOCUMENT_ATT_SEND_API1 has been changed to wrap SO_DOCUMENT_SEND_API1 exactly. The CONTENTS_HEX parameter should be now used for binary attachments.
Documents are no longer readable after upgrade TO 620
See Note 787418 - Sent documents are not readable after upgrade. Corrected in SAPKB62053, SAPKB64013, SAPKB70003.
The email contains 'P R I N T I N G' instead of 'PRINTING'
Pass email text using CONTENTS_TXT parameter instead of CONTENTS_BIN
The sent appointment doesn't work
There is a recent note: Note 1353329 - iCalendar document that is sent is processed incorrectly
SAP template programs
SAP provides the following programs, which are in fact templates because they can't be executed as is (email is hardcoded for example):
- BCS_EXAMPLE_1: send a simple text provided as an internal table of text lines to joe.doe@crazy-company.com
- BCS_EXAMPLE_2: send a simple text provided as an internal table of text lines and text attachment in form of text lines itab to fax DE 09999-123456
- BCS_EXAMPLE_3: send a simple text provided in an internal table of text lines and an additional note to SY-UNAME
- BCS_EXAMPLE_4: send a simple text provided in an internal table of text lines recipients are selected in dialogue (default joe.doe@crazy-company.com)
- BCS_EXAMPLE_5: a simple text provided in an internal table of text lines and an attached MS word document provided in internal table SOLIX_TAB (document retrieval has to be coded) to joe.doe@crazy-company.com
- BCS_EXAMPLE_6: enter customer, carrier (flight demo data) and email, and generate corresponding FP_TEST_03 adobe form, and send it as attachment to the email
- SENDLIST_BCS: provided as attachment in SAP Note 190669 - Sending lists using SAPconnect, it uses SO_DOCUMENT_SEND_API1
- SENDLIST_BCS: provided as attachment in SAP Note 190669 - Sending lists using SAPconnect, it uses CL_BCS
- ZSSO_DOCUMENT_SEND_API1_46 and ZSSO_DOCUMENT_SEND_API1_610: provided as attachment in SAP Note 609696 - SAPoffice: Error in documentation (SO_DOCUMENT_SEND_API1). There are 2 versions, one for 4.6C, and one for 6.10 and above, the difference between the 2 is only the addition of COMMIT_WORK parameter for 6.10 version. Both call SO_DOCUMENT_SEND_API1.
- RSWNSENDMAIL1: demo of SO_NEW_DOCUMENT_ATT_SEND_API1
Examples of programs
Many wikis give more or less the same snippet of code, with few variations. Here is a summary which will help you to find quickly what you need.
CL_BCS
As explained above, the simplest generic code to send mails is provided in this wiki: Sending mail with attachment using Object Oriented Approach
Article title |
Activatable - Works immediately - It's only a mail demo |
TXT or HTML mail's main text |
Attachment |
Additional info |
---|---|---|---|---|
|
TXT |
One text attachment |
|
|
|
TXT |
ICal text file |
It uses CL_APPOINTMENT class to create easily the ICal file |
|
|
TXT |
Any number of binary attachments |
Code proposed as a generic easy-to-use function module |
|
SAP Network Blog: Unknown thus unloved?, by Eddy De Clercq |
|
TXT |
None |
He wonders why people stick on old function modules, it was a 2006 blog but still relevant. You need to use a function module from another blog and article |
|
TXT/HTML |
None |
Shows how to insert an hyperlink to start TRIP transaction from the mail. Needs SICF configuration steps which are not shown here |
|
Send Mail having Multiple Files as Attachment using object oriented technique |
|
TXT |
One text attachment |
You need to change the hard-coded email |
|
HTML |
None |
Easy-to-use function module which runs a program with variant, retrieves its list, converts it to HTML and sends the mail with this HTML |
|
Publish Webervice in ABAP, Consume Webservice in ABAP and Adobe Flex. |
|
TXT |
None |
The goal of the wiki is not to show how to send a mail but it is embedded in a function module in part 1 that can be used standalone. It can only send a simple text to one recipient. |
|
None |
Adobe form output |
It uses a non-existing Z* Adobe Form. It must be run in dialog because a dialog asks to save the PDF on presentation server too |
|
|
TXT |
One text attachment of type VCS (Outlook Calendar) |
Sends email based on selection screen parameters, program reads infotype 0105 to find the personnel number, User ID and E-mail address |
|
Code to get the content of an attachment in DM and mail it to external email id |
|
TXT |
Zero to many binaries |
Binaries come from ECC documents attached (CV01N transaction). Program can't compile directly because of many little syntax errors, but can be corrected easily |
|
TXT |
ALV output in PDF format |
The principle is to print ALV to spool directly, convert spool to PDF, and build and send the mail. Can be used in background |
Legend:
- Activatable
means that code can be activated on all NetWeaver solutions (ECC, CRM, SolMan, etc.)
- Works immediately
means that you don't need to change the program (because of hard-coded email for instance), nor create customizing or data, except SAP flight demo data that you should have initialized on your system (see wiki Flight Data Application - Demo Example for Integration Technologies). If the code is to be created using several manipulations like creating a function module or a global class, it's slow, so only a
is granted.
- Only a mail demo
means that the article only deals with sending mails
SO_DOCUMENT_SEND_API1
As explained above, this function module is obsolete and complex to use, please use CL_BCS instead.
- HTML
- (empty) Send Email in HTML format from SAP: sends mail in HTML format. No attachments.
- Mails from SAP to external mail id with HTML formatting: mail body text in HTML (HTM), no attachments
- Send Multiple Attachments of Spool with E-mail: selection screen asks for a list of ABAP List spools and a list of recipients, these lists are converted into HTML and sent to recipients as attachments
- Several binary attachments
- ABAP - Sending GOS attachments to an email address: selection screen asks for a business object type and key, gets all its attachments, and sends them as attachments
- Simplest demo
- (Empty Page) Custom FM to send Emails: it has 3 parameters, one email address, one subject, and one text. No attachments.
- Spool
- Converts spool request into PDF document and emails: this program generates an ABAP list spool "Hello World", converts it to PDF, and sends an email with this PDF as attachment
- Converting Spool to PDF and sending via mail to external email id: selection screen asks for a job name and program name, retrieves the first spool whatever it is OTF (SAPscript or Smart Form) or ABAP list, converts it to PDF and sends it as attachment of an email
- Adding multiple spool into one PDF & send mail: shows how to aggregate several OTF spools (SAPscript or Smart Form), convert it into PDF, and send it as attachment
- Code Sample: How to Convert the Spool into PDF and Send to External Email, by Palanisamy Elini: asks for a program and variant, submits it, retrieves the generated ABAP list spool, converts it to PDF and sends it as attachment
- Smartform Send via Email: prints smart form to spool, converts it to PDF and sends it as attachment
- Send an External mail through SAP Business Workflow and Recievers address in CC
- How to write an ABAP Program to place an Excel file in a path and send it as an Attachment
- ABAP - Payment Advice Sent through mail Via PDF Attachment
- SCM Inventory Management Report
- SAP Network Blog: Sending SMS notification via business workflow, by Ronen Fox
- SAP Network Blog: Send mail / fax with ~100 lines from a BSP application, by Dezso Pap
- Article: Tracking Zero Record Loads, by Pradeep Choudhari
- Code Sample: BDC Send Mails Triggering Events, by Beena Chandy
- Article: Broadcasting eMails at Custom Intervals after the Completion of Data Loads, by Rudra Pradeep Reddy Neelapu (Mahindra Satyam)
SO_NEW_DOCUMENT_ATT_SEND_API1
As explained above, this function module is obsolete and complex to use, please use CL_BCS instead.
Documents which only deal with sending email:
- SAP Network Blog: Sending E-Mail from ABAP - Version 610 and Higher - BCS Interface
- Multiple Attachments Explanation: explains how SO_NEW_DOCUMENT_ATT_SEND_API1 parameters work. I'm not sure that it compiles in a Unicode-enabled program (there is a move of characters into hexadecimal variable)
- Email from SAP: sends mail containing body in text format + 2 binary attachments
- Sending Mail: sends mail containing body in text format, without any attachment, and calls RSCONN01.
- To send 2 int tables data as two attachments to mail id outside sap system: sends mail containing body in text format + 2 "binary" attachments .XLS (but are tab delimited text files). So, it will probably not work very well if we use non Unicode system, or it's a utf-16BE system (Office prefers utf-16LE). Moreover doc_size is calculated by number of lines * 255 (instead of 510), so it should not work very well?
- Sending mail with attachment: based on SPFLI table (basis). "binary" attachment .XLS but is tab delimited text file.
- Document for configuring SAP R3 to send mail to external domain: no attachment
- Multiple attachment on e_mail: based on MARC table (ECC). "binary" attachment .XLS but is tab delimited text file.
- How to send Internal Table to E_Mail address: based on MARA table (ECC). "binary" attachment .CSV with comma separated values. SO_RAW_TO_RTF to adapt contents_bin?
- Creating a SAP shortcut for any transaction and sending it by mail: it shows how to create a shortcut (SWN_CREATE_SHORTCUT) and sends it as an attachment, with screen captures
- Send Message to External email id and SAP User id via ABAP: a simple demo with screen captures. It's a mail without attachment.
Other documents:
- Sending Microsoft Outlook Appointments from ABAP: nothing about SO_NEW_..., wiki just says to create a VCS attachment, and what it must contain. It links to http://www.sapdev.co.uk/reporting/rep_spooltopdf.htm which has the same algorithm/error that is in Smartform to Mail as PDF attachment
- ABAP- Data Conversion from Char to binary: it shows how to use cl_abap_conv_uc_number->convert_char_stream to convert string to xstring, in the context of converting a table of solisti1 into a table of solix.
- Object service files to e-mail - ( Can be used for custom portal,workflow applications): function module to send attachments of given employee trips (object type BUS2089) via emails to a given list of people
- Send Smart Form output as PDF:
- Smartform to Mail as PDF attachment: risky way to convert internal table tline_tab (following conversion from OTF to PDF) into string, by converting space into ~ then reversed, which requires that hexa value of ~ doesn't exist initially in the PDF
- Smartform as a PDF to Mail id: only PDF attachment without reference to any header, no mail body
- mail send through output controls: this wiki proposes a function module to which you send the result of a smart form execution (called with GET_OTF parameter set to 'X'), the OTF is then converted into PDF and mail is sent with this PDF attached
- Smartform Send via Email: mail body text, and PDF attachment
- Code sample: Smart form in ABAP: it prints a smart form directly into OTF format, then converts it into PDF and sends the email. This article uses the "risky way" about ~ character, already mentioned above, so forget it.
- SAP Network Blog: Issue smart form output to Email as PDF attachment: lines to change to your smart form program, to send it in an email as PDF attachment, without printing it
- Code sample: User Triggered Adhoc Loading Scenario for Fast End-closing, by Gokul Muthuswamy (Smart Modular Technologies)
- Code sample: To Check the Files/Reports in Application Server and trigger mail alerts, by Surendra Kumar Reddy Koduru (ITC Infotech India Ltd. (Bangalore/INDIA): this program sends text mails without attachments
- Hierarchy Comparisons, by Sudheer Junnuthula: it sends one email with one text attachment
- SAP Network Blog: Extract files attached to business process objects e.g. Sales Order, Service Order, and Accounting Documents etc.: it sends several binary attachments
- SAP Network Blog: Email Output to Multiple Recipients Functionality in SAP: explains ECC configuration (NACE, etc.) to send emails, and how to send to more than one email via a custom solution
SO_NEW_DOCUMENT_SEND_API1
As explained above, this function module is obsolete and complex to use, please use CL_BCS instead.
- ABAP - Mail with link to outlook
- Send mail via SAP ABAP Code
- SAP Network Blog: Automatic Notification mail to basis for user locking, by bharath padmanabhan
- Article: Creating Email Alerts for Process Chains, by Ralf Rastert
- Article: Sending Monitor Entries from Update routine via Email, by Sathish Kumar (IBM India Private Limited)
5 Comments
Cem Unal
Hello,
i am looking a way for sending a pdf document as an email body not as an attachment.
Any ideas?
Regards.
Former Member
Hello,
I am using SO_DOCUMENT_SEND_API1 to send mail to many customers (as selected by user on the screen interactively) with an attachment and a fixed sender name as for example xxx.yy@abc.com successfully.
The requestor whose address is xxx.yy@abc.com wants to see that all the sent items should also be recorded in his sent items box in outlook.
Is this possible in ABAP?
Thank you
Former Member
Hi,
I used CL_BCS class concept for sending email through background.
Most of the time the mail is sending correctly but sometimes it fails.
When i saw the job in SM37, i found the message : Exception condition "CNTL_ERROR" raised.
Can you please guide me at this point.
Thanks in advance.
Regards,
Munu
Mathieu SCIALOM
Hi!
Both SO_NEW_DOCUMENT_SEND_API1 and CL_BCS->SEND transform valid HTML to invalid HTML (for example, transforming <span></span> to <span/>). Why? How to avoid this this?
Regards,
Mathieu
Vitali Hardzeyenka
Hi all.
Could anyone explain how to send a mail to Outlook Distributed List (not to SAP DL created in so23 etc, just to external already crated list) by CL_BCS?
My DL is in *@sap,com domain, and this domain is included into SMTP settings. I can send a mail to particular participant of DL, but not to entire DL.
Everytime i receive a message:
Delivery to person works fine.
UPD:
Possible reason/solution may be described here
https://launchpad.support.sap.com/#/notes/1496168
Regards
Vitali