Sending email using SAP Cloud SDK

Objective

After completing this lesson, you will be able to Sending emails using SAP Cloud SDK.

Sending emails using SAP Cloud SDK

The SAP Cloud SDK enables the sending of emails from applications deployed on SAP Business Technology Platform (BTP) to your email servers. The destination service on SAP BTP enables you to define email servers accessible both publicly on the internet and On-Premise.

Step 1 - Install npm module

  • Install the npm module @sap-cloud-sdk/mail-client to your project by running the following command
Code Snippet
Copy code
Switch to dark mode
1
npm install @sap-cloud-sdk/mail-client
  • Use the following command to load the module
Code Snippet
Copy code
Switch to dark mode
1
const { sendMail } = require("@sap-cloud-sdk/mail-client");

Step 2 - Create destination configuratio in SAP BTP

  • Create the destination configuration in SAP BTP for your mail server
  • The following destination configuration is used to connect to the Ethereal mail server (https://ethereal.email)
  • The additional properties of the destination are listed below...
mail.smtp.authtrue
mail.smtp.fromuser@ethereal.email
mail.smtp.hostsmtp.ethereal.email
mail.smtp.port587
mail.smtp.ssl.enabledfalse
mail.transport.protocolsmtp

Note

Currently, only basic authentication is supported.

Step 3 - Code to send email

  • Create a MailConfig object that contains all the essential information of an email
  • Send the email by calling the sendMail() function with mailConfig as argument

Note

For an On-Premise mail server, you MUST deploy the application to SAP BTP to make it work. Running in hybrid mode will not work for an On-Premise mail server
Code Snippet
Copy code
Switch to dark mode
12345678910111213141516
srv.on("sendEmail", async (req) => { try { const mailConfig = { from: "user@ethereal.email", to: "someone@anywhere.com", subject: "e-mail subject", text: "e-mail text", }; sendMail({ destinationName: "maildestination" }, [mailConfig]); return "OK"; } catch (error) { console.log(error); } });

Step 4 - Sending multiple emails

Code Snippet
Copy code
Switch to dark mode
123
sendMail({ destinationName: "maildestination" }, [mailConfig, mailConfig]);
  • To send emails in a sequence, set the parallel option to false
Code Snippet
Copy code
Switch to dark mode
123456
sendMail({ destinationName: "maildestination" }, [mailConfig], { sdkOptions: { parallel: false } });

Log in to track your progress & complete quizzes