Skip to Content

As you know, I’m rather keen on compact code. So I was rather happy to see the “Send mail / fax with ~100 lines from a BSP application” Send mail / fax with ~100 lines from a BSP application by Dezso Pap . It proves that simple things like sending an e-mail can be programmed in an easy manner. The only point of ‘criticism’ from my end is that the well known SO_DOCUMENT_SEND_API1 is used. I see this FM, together with SO_NE_DOCUMENT_ATT_SEND_AP1, appearing everywhere in the forums as THE method/solution for sending mail, instead of the BCS classes, in order to prevent problems. This might be the case in some circumstances, but for this simple application it is certainly not.

         

Thomas already covered this method 1.5 years ago in his Sending E-Mail from ABAP – Version 610 and Higher – BCS Interface with his usual thoroughness and in-depth explanations. The method even has a help and a BSP tutorial application (BSPTUTORIALMAIL). The problem with the help and tutorial is that they give the impression that you need to put your code in a method of the application class, and they make it all look a bit more complicated than it needs to be. From an educational point of view I do not consider this to be a good way to help users understand a new(er) way of working. I therefore thought that it was time to kind of reintroduce this method.

         

 

         

Less is more

         

I’m not going to rewrite Thomas’ web log on this though. I am only going to provide the absolute minimum code which is needed to send an e-mail. You can always fall back on Thomas’ web log to extend things. I won’t explain each line either, since they have already been thoroughly commented by Thomas. The additional comment lines should be sufficient.

         

I’ve used the parameters that Dezso used so that when you want to implement the code it is simply a matter of doing a copy/paste.

         

 

         

* some essential class defs 

         

DATA: send_request TYPE REF TO cl_bcs, 

         

document TYPE REF TO cl_document_bcs, 

         

sender TYPE REF TO cl_sapuser_bcs, 

         

recipient TYPE REF TO if_recipient_bcs, 

         

exception_info TYPE REF TO if_os_exception_info, 

         

bcs_exception TYPE REF TO cx_bcs, 

         

l_mailtext TYPE BCSY_TEXT, 

         

c_address TYPE ADR6-SMTP_ADDR. 

         

  

         

* split the string and put it in a table, see also 

         

* Careful with that axe Eduard

         

CALL FUNCTION 'Z_EU_STRING_TO_TABLE' 

         

EXPORTING 

         

text = body 

         

line_length = 255 

         

tables 

         

text_tab = l_mailtext. 

         

TRY. 

         

* Create persistent send request 

         

send_request = cl_bcs=>create_persistent( ). 

         

* Create document 

         

document = cl_document_bcs=>create_document( 

         

i_type = 'RAW' 

         

i_text = l_mailtext 

         

i_subject = subject ). 

         

* Add document to send request 

         

CALL METHOD send_request->set_document( document ). 

         

* Get sender object 

         

sender = cl_sapuser_bcs=>create( sy-uname ). 

         

* Add sender 

         

CALL METHOD send_request->set_sender 

         

EXPORTING i_sender = sender. 

         

* Create recipient 

         

c_address = receiver. 

         

recipient = cl_cam_address_bcs=>create_internet_address( c_address ). 

         

  

         

* Add recipient with its respective attributes to send request 

         

CALL METHOD send_request->add_recipient 

         

EXPORTING 

         

i_recipient = recipient 

         

i_express = ' ' 

         

i_copy = ' ' 

         

i_blind_copy = ' '. 

         

* Send document 

         

CALL METHOD send_request->send( ). 

         

COMMIT WORK. 

         

CATCH cx_bcs INTO bcs_exception. 

         

RAISE EXCEPTION bcs_exception. 

         

ENDTRY. 

         

  

         

 

         

Conclusion

         

As you can see, no rocket science, or even application classes, are involved. If you crave more info. you can find it in the above mentioned references. Additionally, you can take a look at this help too.

To report this post you need to login first.

8 Comments

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

  1. Dezso Pap
    Hi Eddy,

    a real nice minimum. 🙂 I choosed the old FM as there are a lot questions concering using that FM, and I wanted to provide a working sample. Of course the BCS is a more modern way to send documents.

    Best regards,
    Dezso Pap

    (0) 
    1. Eddy De Clercq Post author
      This web log is about sending mail. I don’t see an immediate use for modifying smartform objects, unless you want to modify the data before emailing it.
      (0) 
      1. Niall Pithey
        Yes that is what I would like to see. The old way with SO_DOCUMENT_SEND_API1 allowed easy access to add/modify the document as shown in other blogs for creating attachments with changes to subject lines and body text. The new method of handing mailed docs formed by the smartform module to BCS to process is clean but does not seem as open to allowing modifications of the created mail’s data before sending.
        (0) 
  2. Suresh Datti
    Another advantage of using the BCS is the ability to put in a default sender name.. this wasn’t possible with the SO** function modules ie if an email is sent from a Program run by a batch ID, the email has this batch id as the sender.
    (0) 
  3. Zachary Willson
    Pretty much the same thing i’ve wondered since i’ve started ABAP programming 4 months ago. Why do the same cumbersome, error prone, code obfuscating use of “SO..” functions again and again and again?

    But, say, the string to line converter part could had been easily internalized. Or still, designers love to add some not-that-often-used parameters in structures which requires, though little, but not non-effort look up of the type referred. They could have instead just add a small variation of the same method for the sake of covering that flexiblity.

    To add more, some very small html generator utilities could have been minded for htm type emails, too.

    whatever…

    PS : Actually one of the “SO.. ” methods allow to set the sender.

    (0) 
  4. Shawn McDowell
    Thanks for your help with this.  I was able to get an example running in 5 minutes the sample code!  Even better using the new class!
    -S
    (0) 
  5. Jelena Perfiljeva

    Eddy, I’m wondering if you would move this blog from your personal space to ABAP, so that it has more visibility?

    Thank you for writing this, by the way. I used it as recently as just 3 years ago to finally cut the umbillical cord with the SO… function module and also magically resolved some issues with PDF attachments. Simpler and less troublesome code – it’s a win-win.

    Thanks again.

    (0) 

Leave a Reply