How to send emails from ABAP® with CL_BCS

Usage of the class CL_BCS in order to send eMails from ABAP™ applications via the Business Communication Service.

With our development tips we address special, related to praxis topics around the exciting and inexhaustible topic software development.

Valid for: SAP NetWeaver ABAP Web AS from 6.10

Index

  • General
  • e-mail dispatch from SAP
  • Class CL_BCS
  • Method CREATE_PERSISTENT – compile of a sending task
  • Method SET_DOCUMENT – attach text, data files and other objects
  • Method ADD_ATTACHMENT of the class CL_DOCUMENT_BCS
  • Method ADD_RECIPIENT – transfer of receiver
  • Method SEND – Sending of e-mail
  • Cases of application
  • Copyright & disclaimer

General

Often it is important to have necessary information at disposal in order to react quickly and in the right way. Especially, when the information procurement, like a report, takes a long time and cannot be constantly watched, it is useful to release an action upon completion. That can for example be an eMail with the status of a batch job or even the ABAP list.

eMail dispatch from SAP

SAP provided with the web AS 6.10 (6.20) the Business Communication Services (BCS) to send objects from ABAP applications. This object orientated interface is the successor of the Business Communication Interface (BCI), which is implemented as objects of the Business Object Repository (BOR).

In this Cadaxo development tip I will give an overview of the e-mail dispatch via BCS.

Class CL_BCS

Via BCS different objects with attachments and notes for several receivers can be sent also in different communication ways. Consequently the interface – the class CL_BCS – is constructed complex. In this development tip we therefore concentrate on sending an e-mail with attachment only.

A sending process breaks down into the following steps:

  • Generation of a sending task
  • Attaching an object
  • Choice of receivers
  • Sending of the mail

For all those tasks the class CL_BCA provides the following methods:

  • CREATE_PERSISTENT Compilation of a sending task
  • SET_DOCUMENT Attaching a text, data or other objects
  • ADD_RECIPIENT Add the receiver
  • SEND Sending of e-mail

Method CREATE_PERSISTENT – Compilation of a sending order

This method generates a sending task and returns an object of the class CL_BCS. All further steps are used upon that object.

Method SET_DOCUMENT – Attachment oft ext, data and other objects

With this method the content, meaning the actual text and data attachments of the e-mail is committed. The method expects an object with the interface IF_DOCUMENTS_BCS. Attachments are committed via this object too.

Via the statistic method CREATE_DOCUMENT of the class CL_DOCUMENT_BCS a respective object can be created.

The method expects the following parameters:

I_TYPE: Type of committed data, e.g. HTM für eine HTML Datei, TXT for text data, BIN for binary data like PDFs

I_TEXT/I_HEX: A chart with the actual text resp. the binary data according to the choosen type

I_SUBJECT: subject of the e-mail

Method ADD_ATTACHMENT of class CL_DOCUMENT_BCS

This method is similarly used to the CREATE_DOCUMENT:

I_ATTACHMENT_TYPE: Type of committed data, e.g. HTM for HTML data, TXT for text data, BIN for binary data like PDFs

I_ATTACHMENT_TEXT/

I_ATTACHMENT_HEX: An internal table with the actuall text resp. the binary data according to the choosen type

I_ATTACHMENT_SUBJECT: Name of the attachment file

Method ADD_RECIPIENT – Transfer of receiver

Depending on the way of communication and type of the sent object different receivers can be transferred to the mail. E.g. no business object relation can be per e-mail.

The method expect at least one object which implements the interface IF_RECIPIENT_BCS:

I_RECIPIENT: receiver address (Interface IF_RECIPIENT_BCS)

I_EXPRESS: Express message; if the receiver is a SAP user, a pop-up appears at the next processing of the PBO event with the notice of a new message

I_COPY: The receiver receives the message as a copy

I_BLIND_COPY: The receiver receives the message as a blind copy

I_NO_FORWARD: no forwarding; if the receiver is a SAP user, he cannot forward the message

The two most important receiver types are probably e-mail addresses and SAP users. The respective objects are generated by the following statistic methods:

CL_SAPUSER_BCS=>CREATE For SAP users

CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS For e-mail addresses

Method SEND – Sending of an e-mail

The message is passed on to SAPconnect and sent via the respective interface. The actual sending process is usually scheduled by a batch job with the report RSCONN01, which periodically sends all upcoming messages. In order to avoid the waiting perioduntil the next job performance, the message can be tagged as “to be sent immediately”: Method SET_SEND_IMMEDIATELY( abap_true ) of the sending task.

Method SHORT_MESSAGE

With this method the SAPOffice dialogue opens itself for new messages, on which the user can maintain receivers, texts, attachments and their settings himself. Via parameters suggestion values for the respective fields and objects, e.g. receiver addresses, can be taken with at the time of selection, which later will be displayed in the message.

ABAP-Klasse CL_BCS – Mailversand aus ABAP

Verwendung der Klasse CL_BCS zum Versenden von eMails aus ABAP™ Anwendungen über das Business Communication Service.

Mit unseren Development Tipps behandeln wir spezielle, praxisbezogene Themen rund um das spannende und unerschöpfliche Thema Softwareentwicklung.
Gültig für: SAP NetWeaver ABAP Web AS ab 6.10

Inhalt

  • Allgemeines
  • Mail versenden aus SAP bzw. ABAP
  • Klasse CL_BCS
    • Methode CREATE_PERSISTENT: Anlegen eines Sendeauftrags
    • Methode SET_DOCUMENT: Anfügen von Text, Dateien und anderen Objekten
    • Methode ADD_ATTACHMENT: der Klasse CL_DOCUMENT_BCS
    • Methode ADD_RECIPIENT: Übergabe der Empfänger 4
    • Methode SEND: Senden der E-Mail
  • Anwendungsfälle

Allgemeines

Oft ist es wichtig, benötigte Informationen schnell zur Verfügung zu haben, um rasch und richtig reagieren zu können. Besonders, wenn die Informationsbeschaffung, etwa ein Report, lange dauert und nicht ständig überwacht werden kann, ist es sinnvoll nach Beendigung eine Aktion auszulösen. Das kann z. B. eine eMail mit dem Status eines Batchjobs oder gar schon der fertigen ABAP-Liste sein.

Mail versenden aus SAP bzw. ABAP

SAP hat mit dem Web AS 6.10 (6.20) die Business Communication Services (BCS) zum Versenden von Objekten aus ABAP Anwendungen zur Verfügung gestellt. Dieses objektorientierte Interface ist der Nachfolger des Business Communication Interface (BCI), das über Objekte des Business Object Repository (BOR) realisiert ist.

In diesem CADAXO Development Tipp werde ich einen kurzen Überblick zum E-Mail Versand mittels BCS geben.

Klasse CL_BCS

Über das BCS können unterschiedlichste Objekte mit Anhängen und Notizen an mehrere Empfänger und auf unterschiedliche Kommunikationsarten geschickt werden. Dementsprechend komplex ist das Interface – die Klasse CL_BCS – auch aufgebaut. In diesem Development Tipp beschränken wir uns auf das Versenden einer E-Mail mit Anhang.

Ein Sendevorgang gliedert sich üblicherweise in folgende Schritte.

  1. Erzeugen des Sendeauftrags
  2. Anfügen eines Objekts
  3. Auswahl der Empfänger
  4. Versenden der Nachricht

Für all diese Aufgaben stellt die Klasse CL_BCS Methoden zu Verfügung:

  • CREATE_PERSISTENT Anlegen eines Sendeauftrags
  • SET_DOCUMENT Anfügen von Text, Dateien und anderen Objekten
  • ADD_RECIPIENT Übergabe der Empfänger
  • SEND Senden der E-Mail

Methode CREATE_PERSISTENT – Anlegen eines Sendeauftrags

Diese Methode erzeugt einen Sendeauftrag und liefert diesen als Objekt der Klasse CL_BCS zurück. Alle weiteren Schritte werden auf dieses Objekt angewendet.

Methode SET_DOCUMENT – Anfügen von Text, Dateien und anderen Objekten

Mit dieser Methode wird der Content, also der eigentliche Text und Dateianhänge der E-Mail, übergeben. Die Methode erwartet ein Objekt mit dem Interface IF_DOCUMENT_BCS. Auch Attachments werden über dieses Objekt übergeben.

Über die statische Methode CREATE_DOCUMENT der Klasse CL_DOCUMENT_BCS kann man sich ein entsprechendes Objekt erzeugen lassen. Die Methode erwartet zumindest folgende Parameter:

  • I_TYPE: Typ der übergebenen Daten, z. B. HTM für eine HTML Datei, TXT für Textdateien, BIN für Binärdaten, etwas PDFs
  • I_TEXT/I_HEX: Eine Tabelle mit dem eigentliche Text bzw. den Binärdaten, je nach gewähltem Typ
  • I_SUBJECT: Betreff der E-Mail

Methode ADD_ATTACHMENT der Klasse CL_DOCUMENT_BCS

Diese Methode ist ähnlich der CREATE_DOCUMENT zu verwenden:

  • I_ATTACHMENT_TYPE: Typ der übergebenen Daten, z. B. HTM für eine HTML Datei, TXT für Textdateien, BIN für Binärdaten, etwas PDFs
  • I_ATTACHMENT_TEXT/I_ATTACHMENT_HEX: Eine Tabelle mit dem eigentliche Text bzw. den Binärdaten, je nach gewähltem Typ
  • I_ATTACHMENT_SUBJECT: Name der Attachmentdatei

Methode ADD_RECIPIENT – Übergabe der Empfänger

Je nach Kommunikationsart und Typ der versendeten Objekte können unterschiedliche Empfänger an die Nachricht übergeben werden. Es kann zum Beispiel keine Business Objekt Verknüpfung per E-Mail verschickt werden.

Die Methode erwartet zumindest ein Objekt, das das Interface IF_RECIPIENT_BCS implementiert:

  • I_RECIPIENT: Empfängeradresse (Interface IF_RECIPIENT_BCS)
  • I_EXPRESS: Expressnachricht; ist der Empfänger ein SAP Benutzer, so erhält er bei der nächsten Prozessierung des PBO-Events eine Pop-up mit dem Hinweis auf eine neue Nachricht
  • I_COPY: Der Empfänger erhält die Nachricht als Kopie
  • I_BLIND_COPY: Der Empfänger erhält die Nachricht als Blindkopie
  • I_NO_FORWARD: keine Weiterleitung; ist der Empfänger ein SAP Benutzer, so kann dieser die Nachricht nicht weiterleiten

Die beiden wichtigsten Empfängertypen sind wahrscheinlich E-Mailadressen und SAP Benutzer. Die entsprechenden Objekte werden durch folgende statische Methoden erzeugt:

  • CL_SAPUSER_BCS=>CREATE Für SAP Benutzer
  • CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS Für E-Mailadressen

Methode SEND – Senden der eMail

Die Nachricht wird an SAPconnect übergeben und über das entsprechende Interface verschickt. Der eigentliche Sendeprozess wird üblicherweise durch einen Batchjob mit dem Programm RSCONN01 eingeplant, der periodisch alle anstehenden Nachrichten verschickt. Um die Wartezeit bis zur nächsten Jobausführung zu umgehen, kann die Nachricht als „sofort zu verschicken“ gekennzeichnet werden: Methode SET_SEND_IMMEDIATELY( abap_true ) des Sendeauftrags.

Methode SHORT_MESSAGE

Mit dieser Methode öffnet sich der SAPOffice Dialog für neue Nachrichten, auf dem der Benutzer selbst Empfänger, Text, Attachments und deren Einstellungen pflegen kann. Über Parameter können Vorschlagswerte für die entsprechenden Felder und Objekte, z. B. Empfängeradressen beim Aufruf mitgegeben werden, die dann in der Nachricht angezeigt werden.

Anwendungsfälle

Wo ist nun eine Anwendung der Klasse CL_BCS denkbar und sinnvoll?

  • Anwender können informiert werden, oder bereits Ergebnisse zugeschickt bekommen, wenn eine Auswertung, die als Hintergrundjob eingeplant war, fertig ist.
  • In Fehlersituationen bei kritischen Anwendungen oder Schnittstellen können die zuständigen Personen sofort benachrichtigt werden und somit viel schneller reagieren.