Sending email in SAP ABAP using Cl_BCS class

Step1:Design selection-screen for email id input Normal 

This tutorial explains you sending emails using SAP ABAP programming language.

Go to SE38, create a program ZSAPN_SEND_EMAIL, save it in a local object.

Design a screen to enter email id, email subject, send immediatly flag.
PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Emai input
PARAMETERS: P_SUB TYPE CHAR50. "email subject
PARAMETERS : P_SEND AS CHECKBOX. "send immediatly flag

Send email in SAP ABAP

Step2:Set email subject and body Important 

Declare and prepare email object for cl_bcs

*Prepare Mail Object
DATA:  LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
CLASS CL_BCS DEFINITION LOAD.
LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

Technically email is a binary document, we need to add document to the email object.Document contains parameters like email type (HTM, TXT etc), email subject, email body.

EMAIL BODY: We can send email body of 255 characters per line, email body might be more than 255 characters, so we need to pass it in the form of lines in an internal table (see example below).

Limitation: While using CL_BCS class to send email, we can set maximum 50 character subject only.
* Message body and subject
DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL. "document object
DATA : I_TEXT TYPE BCSY_TEXT. "Table for body
DATA : W_TEXT LIKE LINE OF I_TEXT. "work area for message body
*Set body
W_TEXT-LINE = 'This is the first tutorial of sending email using SAP ABAP programming by SAPNuts.com'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.
W_TEXT-LINE = 'SAPNuts.com is the best SAP ABAP learning portal'.
APPEND W_TEXT TO I_TEXT.
CLEAR W_TEXT.
*Create Email document
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( "create document
I_TYPE = 'TXT' "Type of document HTM, TXT etc
I_TEXT =  I_TEXT "email body internal table
I_SUBJECT = P_SUB ). "email subject here p_sub input parameter
Pass the email document with subject, body and type to send request.
* Pass the document to send request
  LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).

 
Step3:Set Sender and Recipient to send request Important 

For every email, there is a sender and reciever (recipient), set sender and recipient for send request.

TRY...ENDTRY  is used for exception handleing in Object Oriented programming.
*Set Sender
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL.
TRY.
  LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). "sender is the logged in user
* Set sender to send request
  LO_SEND_REQUEST->SET_SENDER(
  EXPORTING
  I_SENDER = LO_SENDER ).
*    CATCH CX_ADDRESS_BCS.
****Catch exception here
ENDTRY.
Set recipient for the send request
**Set recipient
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL.
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_EMAIL ). "Here Recipient is email input p_email
TRY.
  LO_SEND_REQUEST->ADD_RECIPIENT(
      EXPORTING
      I_RECIPIENT = LO_RECIPIENT
      I_EXPRESS = 'X' ).
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.

Step4:Set send email for send request Important 

In some servers network management team (BASIS) set mail sending frequency to reduce server load in SMTP(Simple Mail Transfer Protocol) configuration, in such cases the mails will be sent based on server load (may be some time later depends on server load)...In such cases we can set send immediately for the send request.

*Set immediate sending
TRY.
  CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY
    EXPORTING
      I_SEND_IMMEDIATELY = 'X'.
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
ENDTRY.
Finally send email using send request.
TRY.
** Send email
  LO_SEND_REQUEST->SEND(
  EXPORTING
  I_WITH_ERROR_SCREEN = 'X' ).
  COMMIT WORK.
  IF SY-SUBRC = 0.
    WRITE :/ 'Mail sent successfully'.
  ENDIF.
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
*catch exception here
ENDTRY.

Step5:Code to send email in SAP ABAP programming Important 

The final code for sending email in SAP ABAP using CL_BCS class is below.

Send email in SAP ABAP

REPORT ZSAPN_SEND_EMAIL.
*Prepare Mail Object
DATA:  LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
CLASS CL_BCS DEFINITION LOAD.
DATA: LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL. "document object
DATA : I_TEXT TYPE BCSY_TEXT. "Table for body
DATA : W_TEXT LIKE LINE OF I_TEXT. "work area for message body
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL. "sender
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL. "recipient
**Selection screen
PARAMETERS : P_EMAIL TYPE ADR6-SMTP_ADDR. "Emai input
PARAMETERS: P_SUB TYPE CHAR50. "email subject
PARAMETERS : P_SEND AS CHECKBOX. "send immediatly flag

START-OF-SELECTION.
  LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).
* Message body and subject
*Set body
  W_TEXT-LINE = 'This is the first tutorial of sending email using SAP ABAP programming by SAPNuts.com'.
  APPEND W_TEXT TO I_TEXT.
  CLEAR W_TEXT.
  W_TEXT-LINE = 'SAPNuts.com is the best SAP ABAP learning portal'.
  APPEND W_TEXT TO I_TEXT.
  CLEAR W_TEXT.
  LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( "create document
  I_TYPE = 'TXT' "Type of document HTM, TXT etc
  I_TEXT =  I_TEXT "email body internal table
  I_SUBJECT = P_SUB ). "email subject here p_sub input parameter
* Pass the document to send request
  LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).


  TRY.
    LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). "sender is the logged in user
* Set sender to send request
    LO_SEND_REQUEST->SET_SENDER(
    EXPORTING
    I_SENDER = LO_SENDER ).
*    CATCH CX_ADDRESS_BCS.
****Catch exception here
  ENDTRY.
**Set recipient
  LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_EMAIL ). "Here Recipient is email input p_email
  TRY.
    LO_SEND_REQUEST->ADD_RECIPIENT(
        EXPORTING
        I_RECIPIENT = LO_RECIPIENT
        I_EXPRESS = 'X' ).
*  CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
  ENDTRY.

  TRY.
    CALL METHOD LO_SEND_REQUEST->SET_SEND_IMMEDIATELY
      EXPORTING
        I_SEND_IMMEDIATELY = P_SEND. "here selection screen input p_send
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
**Catch exception here
  ENDTRY.
  TRY.
** Send email
    LO_SEND_REQUEST->SEND(
    EXPORTING
    I_WITH_ERROR_SCREEN = 'X' ).
    COMMIT WORK.
    IF SY-SUBRC = 0. "mail sent successfully
      WRITE :/ 'Mail sent successfully'.
    ENDIF.
*    CATCH CX_SEND_REQ_BCS INTO BCS_EXCEPTION .
*catch exception here
 ENDTRY.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChampaignWolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值