'SMTP ERROR using Nodemailer to send email
i am building a web app and i want to send emails to registered users that forgot their password. But I am having problem with sending the mail through Node mail. I keep getting a 421 response error that the server is busy with too many connections ans i dont understand why.
below is my code and the error i keep getting.
require('dotenv').config()
const nodeMailer = require('nodemailer')
const sendMail = async (email, token) => {
const transporter = nodeMailer.createTransport({
host: process.env.EMAIL_HOST,
port: 587,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
});
console.log("passed transportrt ");
await transporter.sendMail({
from: process.env.EMAIL_USER,
to: email,
subject: "Password RESET",
text: `Follow this link to reset your password. It expires in 15minutes. \n\n
http://localhost:4000/reset/${token}`,
});
console.log('out')
}
module.exports = sendMail
the other parts and work flow works well till it gets to await transporter.sendMail({...
and this is the error i keep getting below
Error: Invalid greeting. response=421 Service not available: 421 Service not available
at SMTPConnection._actionGreeting (.../node_modules/nodemailer/lib/smtp-connection/index.js:1205:27)
at SMTPConnection._processResponse (.../node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
at SMTPConnection._onData (.../node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at Socket.SMTPConnection._onSocketData (.../node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at Socket.emit (node:events:526:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
code: 'EPROTOCOL',
response: '421 Service not available',
responseCode: 421,
command: 'CONN'
}
Is there something i am possibly doing wrong? Plus i am running this on unbuntu(if it has any connection to the error)
Solution 1:[1]
A 421 error with an EPROTOCOL error usually means the mail client could not connect to the mail server. Two easy possibilities to check:
process.env.EMAIL_HOSTis not defined (not set in.envfile or OS)- Your server cannot connect to the mail host at port 587 (easily checked with telnet).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tony BenBrahim |
