Using Azure communication service to send email

Karan S 21 Reputation points
2022-09-22T06:21:01.05+00:00

I am using azure communication services to send email.

import { EmailClient } from "@azure/communication-email";  
  
 function App() {  
  const connectionString = "*****************************************************************************"  
  const client = new EmailClient(connectionString);  
  const sender = "******@hexaware.com";  
  const emailContent = {  
    subject: "Send email quick start test- JS sample",  
    plainText:  
      "Test Email from JS Send Email Sample Application\n\n This email is part of testing of email communication service. \\n Best wishes",  
    html: "<html><head><title>ACS Email as a Service</title></head><body><h1>ACS Email as a Service - Html body</h1><h2>This email is part of testing of email communication service</h2></body></html>",  
  };  
  const toRecipients = {  
    to: [  
      { email: "******@gmail.com", displayName: "Karan S" },  
    ],  
  };  
  
  async function main() {  
    try {  
      const emailMessage = {  
        sender: sender,  
        content: emailContent,  
        recipients: toRecipients,  
      };  
  
      const sendResult = await client.send(emailMessage);  
  
      if (sendResult && sendResult.messageId) {  
        // check mail status, wait for 5 seconds, check for 60 seconds.  
        const messageId = sendResult.messageId;  
        if (messageId === null) {  
          console.log("Message Id not found.");  
          return;  
        }  
  
        console.log("Send email success, MessageId :", messageId);  
  
        let counter = 0;  
        const statusInterval = setInterval(async function () {  
          counter++;  
          try {  
            const response = await client.getSendStatus(messageId);  
            if (response) {  
              console.log(  
                `Email status for {${messageId}} : [${response.status}]`  
              );  
              if (response.status.toLowerCase() !== "queued" || counter > 12) {  
                clearInterval(statusInterval);  
              }  
            }  
          } catch (e) {  
            console.log("Error in checking send mail status: ", e);  
          }  
        }, 5000);  
      } else {  
        console.error(  
          "Something went wrong when trying to send this email: ",  
          sendResult  
        );  
      }  
    } catch (e) {  
      console.log(  
        "################### Exception occoured while sending email #####################",  
        e  
      );  
    }  
  }  
  
  return <h3>hello ${connectionString}</h3>;  
}  
  
export default App;  

But it is showing invalid connection string as error.

In my connection string, it is having a url, followed by semicolon and a access key.

Is that the correct connection string Or am i missing something?

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
1,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ebraheem Al-Muneyeer (MSFT) 961 Reputation points Microsoft Employee
    2022-09-25T10:14:02.52+00:00

    Hi @Karan S , thank you for reaching out!

    You can get your connection string by navigating to your Azure Communication Services (ACS) resource and open the Keys blade.

    You should copy the whole connection string, and it should look like something as follows: "endpoint=https://{Your ACS resource name}.communication.azure.com/;accesskey={Access key}".

    If you still have the issue, is it possible to capture network traces and send them to me to have a look?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.