Sending email using SAP Cloud SDK

Objectives

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
npm install @sap-cloud-sdk/mail-client
Expand
  • Use the following command to load the module
Code snippet
const { sendMail } = require("@sap-cloud-sdk/mail-client");
Expand

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

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

Step 4 - Sending multiple emails

Code snippet

sendMail({ destinationName: "maildestination" }, [mailConfig, mailConfig]);
Expand
  • To send emails in a sequence, set the parallel option to false
Code snippet

sendMail({ destinationName: "maildestination" }, [mailConfig], {
  sdkOptions: {
    parallel: false
  }
});
Expand

Log in to track your progress & complete quizzes