Skip to Content

Some time ago I created a document about how to send custom email notification with attachment in SAP Cloud Applications Studio How to send an email with attachments in SAP Cloud Application Studio.

The body text in this example was plain text.

Now I want to show you how to send an HTML email.

Step 1.

Implement the Event-BeforeSave for your business object.

Step 2.

Write the following ABSL code:

import ABSL;

import AP.FO.Activity.Global as prop;

import AP.Common.GDT as GlobalDataTypes;

import BASIS.Global; //for the pdf

import DocumentServices.Global;

var elEmailRoot : elementsof EmailActivity; 

var elEmailParty: elementsof EmailActivity.Party;

var instEmail; 

var emailBody; 

var emailSubject; 

var elEmailTxtColl: elementsof EmailActivity.TextCollection;

var elEmailTxtCollTxt: elementsof EmailActivity.TextCollection.Text; 

var elEmailTxtCollTxtCntnt: elementsof EmailActivity.TextCollection.Text.TextContent; 

var instEmailTxtColl; 

var instEmailTxtCollTxt; 

var instEmailTxtCollTxtCntnt;

var instEmailAttFld;      

//Email instance creation

emailSubject = "Html test"; 

elEmailRoot.Name.content = emailSubject; 

instEmail = EmailActivity.Create(elEmailRoot);       

//Add parties

elEmailParty.PartyKey.PartyID.content = "8000000102"; 

instEmail.MessageToParty.Create(elEmailParty);  

elEmailParty.PartyKey.PartyID.content = "8000000020"; 

instEmail.MessageToParty.Create(elEmailParty); 

//Create att folder

instEmailAttFld = instEmail.AttachmentFolder.Create();

//Add attachment1 (html body)

var doctype : GlobalDataTypes:DocumentTypeCode;

var binaryObject : BinaryObject;

var docDesc : GlobalDataTypes:Description;

var docName : GlobalDataTypes:LANGUAGEINDEPENDENT_Name;

var docAltName : GlobalDataTypes:LANGUAGEINDEPENDENT_Name;

var bin : BinaryObject.content;

docName = "body.html";

doctype.content = "10001";

binaryObject.mimeCode = "text/html";

binaryObject.content = Binary.ParseFromString("<html><head></head><body><div style=\”font-weight:bold;\”>test</div><body></html>");

instEmailAttFld.CreateFile(doctype, docName, docAltName, docDesc, binaryObject);

//Add attachment 2(pdf)

var FormTemplateLanguage = "E";

var PDF : BinaryObject;

var FormTemplateCode : OutputRequestFormTemplateCode;

FormTemplateCode.content = "Y6Y527NH_PMH9V"; //Code is Form Template Header Code

// Reuse Service Call

PDF = OutputManagementUtilities.GetPDF(this,FormTemplateCode,FormTemplateLanguage);

docName = "test2.pdf";

doctype.content = "10001";

binaryObject = PDF;

instEmailAttFld.CreateFile(doctype, docName, docAltName, docDesc, binaryObject);

instEmail.Send();

Step 3.

Here you go!

htmlemail.PNG

To report this post you need to login first.

30 Comments

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

  1. R. Albers

    Hi Alessandro,

    Thanks for this tutorial. I am trying to send an HTML email (without attachments) and this is not working (using the exact same code as you are). We are on 1405, maybe something has changed?

    I opened up a topic as well: Send HTML email from C4C?

    Can you help me out?

    Thanks!

    (0) 
      1. Jacques-Antoine Ollier

        Hello Alessandro,

        I cannot succeed in putting my body.html as a body text element.

        When I receive the emails, the document is still an attachment and is not added to the body of my email.

        I exactly used your code above. Is there any step to do in order to add this attachment as a body and not as an attachment of the email?

        Thank you very much for your attention.

        Best regards.

        Jacques-Antoine

        (0) 
        1. Alessandro Iannacci Post author

          Hi Jacques, it depends on the email client (gmail is able to visualize it like a normal html email) however i did not find a way to remove the html from the attachments 🙁

          (0) 
          1. Jacques-Antoine Ollier

            Hello Alessandro and thank you for your permanent help!

            Even if I send the email to my iCloud account or Gmail account, it is the same. I have no body added, only an attachment.

            When you say: “however i did not find a way to remove the html from the attachments”, I can see that the “Test” you put in your binary object is appearing as a body text.

            Am I wrong?

            Because that is exactly what I need. I do not care if there is still an html attachment as long as I can retrieve the text I did put in it, directly in the email’s body.

            I raised another thread in order to discuss that: http://scn.sap.com/message/15937749#15937749

            Thank you very much for your attention.

            Bets regards.

            Jacques-Antoine

            (0) 
  2. San Ka Ran

    Hi Alessandro,

    I have tried above code. But I got this Error.

    Error: The document you want to upload has the MIME type text/html. Documents with this MIME type are not allowed to be uploaded according to the settings made in your solution configuration.

    I hope, I miss some configuration. Can you help me to solve it?

    Regards,

    sankaran

    (0) 
    1. Swapnil Borkar

      Hi San Ka Ran

      You need to add the mime type to the config.

      goto Business config WC -> first Implementation->fine tune -> search for Mime .

      Add the mime type code  in this setting .mime type.pngthis setting.

      (0) 
      1. San Ka Ran

        hi Swapnil,

        Thanks. I have configured as you said. Even I got an same issue. Please help me to  solve this issue.

        Configuration and Error window Screen Shots :

        /wp-content/uploads/2015/11/1_837483.jpg/wp-content/uploads/2015/11/2_837484.jpg

        (0) 
                        1. San Ka Ran

                          @Alessandro, In Other system, I have tried as you said. It is working perfectly.  Thanks a lot. I think problem in previous system. So I have raised an incident to SAP. once again thanks. 🙂 🙂 🙂

                          (0) 
                  1. San Ka Ran

                    Dear Alessandro,

                    HTML mail was successfully send in development system.

                    when i was configured in customer tenant. i got this error message while run the script

                    “The document you want to upload has the MIME type text/html. Documents with this MIME type are not allowed to be uploaded according to the settings made in your solution configuration”

                    also can you guide me where to find the Disable signing and encryption

                    Regards,

                    Sankaran

                    (0) 
  3. Hanumath K

    Hi Alessandro,

    I have a requirement to send an attachment (PDF Doc which is available on my system hard disk), can I do this without using the HTML body as attachment? If it is not then what I have to use as HTML body in that case.

    And also Can I get this PDF from one of my drives on the system? any syntax for the same will be helpful if. Thanks.

    Regards

    Hanu

    (0) 
    1. Alessandro Iannacci Post author

      Hi! You cannot add an attachment to an email from your hard drive. The document needs to be available in the C4C System in one business object that you are processing as a binaryobject.

      (0) 
    1. Will Godsall

      Use this for BCC:

      var elEmailBCCParty: elementsof EmailActivity.Party;

      elEmailBCCParty.PartyKey.PartyID.content = “123”;

      instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty); 

      Use this for CC:

      var elEmailCCParty: elementsof EmailActivity.Party;

      elEmailCCParty.PartyKey.PartyID.content = “12345”;

      instEmail.CopyMessageToParty.Create(elEmailCCParty); 

      (0) 
        1. Will Godsall

          You can create me than one.

          var elEmailBCCParty: elementsof EmailActivity.Party;

          elEmailBCCParty.PartyKey.PartyID.content = “123”;

          instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty);

          elEmailBCCParty.PartyKey.PartyID.content = “123456789”;

          instEmail.BlindCopyMessageToParty.Create(elEmailBCCParty);

          (0) 
  4. SRI HARI

     

    Hi All,

    Since the EmailActivity is now deprecated, I am trying to make use of Activity BO to perform send mail, but could not achieve the same. i have these points unanswered:

    1. Without creating instance in Activity can I send the mail when Outlook is not configured?
    2. In Activity BO, Party.EmailURI is read only what can be done to specify the sender?

    Please guide.

    Thanks,

    Srihari

    (0) 

Leave a Reply