Skip to Content

HTML custom email notifications in SAP Cloud Applications Studio

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

31 Comments
You must be Logged on to comment or reply to a post.
  • 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!

      • 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

        • 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 🙁

          • 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

  • 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

  • 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

    • 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.

    • 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); 

        • 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);

  •  

    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