send an email using Azure Communication Service takes a while

Faysal Missi 0 Reputation points
2023-07-27T17:25:10.27+00:00

Hello,

I have an issue with sending an email in my backend is taking a 30 second or more so it cause me a late response to my request in client

my code

const sendMail = async data => {

  const POLLER_WAIT_TIME = 10
  try {

    const message = {
      senderAddress: `<${data.From.Email}>`,
      content: {
        subject: data.Subject,
        html: data.HTMLPart,
      },
      recipients: {
        to: [
          {
            address: `<${data.To[0].Email}>`,
            displayName: `<${data.To[0].Name}>`,
          },
        ],
      },
    };


    const poller = await emailClient.beginSend(message);

    if (!poller.getOperationState().isStarted) {
      throw "Poller was not started."
    }

    let timeElapsed = 0;
    while (!poller.isDone()) {
      poller.poll();
      console.log("Email send polling in progress");

      await new Promise(resolve => setTimeout(resolve, POLLER_WAIT_TIME * 1000));
      timeElapsed += 10;

      if (timeElapsed > 18 * POLLER_WAIT_TIME) {
        console.log("Polling timed out.");
        return true
      }
    }

    if (poller.getResult().status === KnownEmailSendStatus.Succeeded) {
      console.log(`Successfully sent the email (operation id: ${poller.getResult().id})`);
      return true;
    }
    else {
      console.log(poller.getResult().error);
      return true
    }
  } catch (e) {
    console.log(e);
  }
};

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
632 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 14,361 Reputation points
    2023-08-08T23:47:15.37+00:00

    Hi @Faysal Missi thanks for the additional information. No, it's not normal for it to take that long.

    you might be hitting a certain limit. Like the documentation states "If you see that your application is hanging it could be due to email sending being throttled. You can handle this through logging or by implementing a custom policy."

    0 comments No comments