Skip to Content
Author's profile photo Alessandro Iannacci

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

Assigned Tags

      31 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      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!

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      Yes unfortunately sometimes the backend antivirus system block html email. 🙁 we can't do anything about it. Let's wait for next releases.

      Author's profile photo Jacques-Antoine Ollier
      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

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog 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 🙁

      Author's profile photo Jacques-Antoine Ollier
      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

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      Hi, yes I was able to see the attachment as a body with Gmail and with Outlook. Try to remove the text (leave only the html body)

      Author's profile photo Jacques-Antoine Ollier
      Jacques-Antoine Ollier

      As usual, you were right! 😉

      Best regards

      JA

      Author's profile photo San Ka Ran
      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

      Author's profile photo Swapnil Borkar
      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.

      Author's profile photo San Ka Ran
      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

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      Did you restart the solution after saving the configuration?

      Author's profile photo San Ka Ran
      San Ka Ran

      Yes I did....

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      disable the mime type check please

      Capture.JPG

      Author's profile photo San Ka Ran
      San Ka Ran

      yep I have done. But still facing same problem. /wp-content/uploads/2015/12/email_841852.jpg

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      Check if the mime settings are in project, if not add

      0.JPG

      Check also other options: is your domain allowed to send?

      1.JPG

      Disable signing and encryption

      2.JPG

      Author's profile photo San Ka Ran
      San Ka Ran

      hi, Thank you so much.

      I have checked all the things. Yep all the setup are configured successfully. Even I'm facing same problem.

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      In that case better if you raise an incident to SAP 🙁

      Author's profile photo San Ka Ran
      San Ka Ran

      Thanks @alessandro. I'll 🙂

      Author's profile photo San Ka Ran
      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. 🙂 🙂 🙂

      Author's profile photo San Ka Ran
      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

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      Hi there is a screenshot in my previous post

      Author's profile photo Hanumath K
      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

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog 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.

      Author's profile photo Sankaran A
      Sankaran A

      Hi Alessandro,

      How to add CC, BCC in scripting?

      Author's profile photo Former Member
      Former Member

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

      Author's profile photo Sankaran A
      Sankaran A

      Hi Godsall. Thanks a lot. it is possible to add more than one cc / Bcc at a time?

      Author's profile photo Former Member
      Former Member

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

      Author's profile photo Sankaran A
      Sankaran A

      thanks a lot godsall.. I have one more doubt. How to set custom "From" Address instead of using default "Address"?

      Author's profile photo Alessandro Iannacci
      Alessandro Iannacci
      Blog Post Author

      using the activity email you cant you need to use the "absl" version (check my other blogs)

      Author's profile photo Former Member
      Former Member

       

      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

      Author's profile photo Former Member
      Former Member

      Hello everyone,

       

      any updates on this?

       

      Thanks and kind regards

       

      Lars