---
title: "How to handle \"Email Sending Failed\" in Express.js?"  
description: "How to handle \"Email Sending Failed\" in Express.js?"  
author: "Utpal Vishwas"  
published: 2023-08-03  
updated: 2023-08-04  
canonical: https://www.mindstick.com/forum/159454/how-to-handle-email-sending-failed-in-express-js  
category: "javascript"  
tags: ["exception handling", "javascript", "express js"]  
reading_time: 2 minutes  

---

# How to handle "Email Sending Failed" in Express.js?

How to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) "[Email Sending](https://www.mindstick.com/interview/34184/what-is-the-difference-between-synchronous-and-asynchronous-email-sending-in-c-sharp) [Failed](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window)" in Express.js?

## Replies

### Reply by Aryan Kumar

There are a few ways to handle [email](https://www.mindstick.com/articles/12870/10-easy-ways-to-manage-your-business-email-inbox) sending failed in Express.js:

- **Use the** `.catch()` **handler method:** The `.catch()` method takes a function as its argument, which will be called if the email sending fails. The function passed to `.catch()` can handle the error however you want, such as logging it to the console, returning an error message, or retrying the operation.

```plaintext
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your_email@gmail.com',
    pass: 'your_password',
  },
});

const sendMail = (email, subject, text) => {
  const mailOptions = {
    from: email,
    to: 'recipient_email@gmail.com',
    subject: subject,
    text: text,
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log('An error occurred:', error);
    } else {
      console.log('Email sent:', info.response);
    }
  });
};

sendMail('your_email@gmail.com', 'Test email', 'This is a test email');
```

- **Add a second function to the** `.then()` **handler method:** You can also handle email sending failures by adding a second function to the `.then()` handler method. The second function will be called if the email sending fails. The second function can handle the error however you want, just like the `.catch()` method.

```plaintext
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your_email@gmail.com',
    pass: 'your_password',
  },
});

const sendMail = (email, subject, text) => {
  const mailOptions = {
    from: email,
    to: 'recipient_email@gmail.com',
    subject: subject,
    text: text,
  };

  transporter.sendMail(mailOptions).then((info) => {
    console.log('Email sent:', info.response);
  }).catch((error) => {
    console.log('An error occurred:', error);
  });
};

sendMail('your_email@gmail.com', 'Test email', 'This is a test email');
```

- **Use the** `process.on('unhandledRejection')` **event:** The `process.on('unhandledRejection')` event will be emitted whenever an email sending fails and no handler has been attached to it. You can use this event to log the error to the console, return an error message, or retry the operation.

```plaintext
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your_email@gmail.com',
    pass: 'your_password',
  },
});

const sendMail = (email, subject, text) => {
  const mailOptions = {
    from: email,
    to: 'recipient_email@gmail.com',
    subject: subject,
    text: text,
  };

  transporter.sendMail(mailOptions);

  process.on('unhandledRejection', (error) => {
    console.log('An error occurred:', error);
  });
};

sendMail('your_email@gmail.com', 'Test email', 'This is a test email');
```

It is important to handle email sending failures in Express.js so that you can notify your users of the failure and take appropriate action. You can use any of the methods above to handle email sending failures in Express.js.


---

Original Source: https://www.mindstick.com/forum/159454/how-to-handle-email-sending-failed-in-express-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
