我使用 gmail 和 Node Mailer 在 js Node 中创建了一个登录系统。 在开发模式下我使用gmail。
const sendEmail = async options =>{
//1)Create a transporter
// const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
});
//2) Define the mail options
const mailOptions = {
from:'Creative Point <[email protected]>',
to:options.email,
subject:options.subject,
html:options.message
}
//3) Actually send the email
await transporter.sendMail(mailOptions);
};
但是在生产模式下我想使用网络邮件,我在cpanel上创建数据和端口帐户时收到的。
const transporter = nodemailer.createTransport({
service:'smtp.specialsoft.ro ',
port: 465,
auth: {
user: process.env.EMAIL_SEND_SESSIONCPANEL,
pass: process.env.EMAIL_SEND_PASSWORDCPANEL,
}
});
但这给了我下一个错误。
Error: There was an error sending the email.Try again later!Error: Invalid login: 535-5.7.8 Username
and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g20sm3266545ejz.88 - gsmtp
at C:\Users\Cosmin\Desktop\Authentification System Node
JS,Express,JsonWebToken\controllers\sessionController.js:56:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)
我在互联网上没有找到任何与此错误相关的内容。
我按照以下步骤解决了这个问题。
我安装了一个模块:nodemailer-smtp-transport。
const smtpTransport = require('nodemailer-smtp-transport');
我更正了主机名和一些选项。
host:'mail.mydomani.ro'
secureConnection: false,
tls: {
rejectUnauthorized: false
}
3)我在传输器中集成了模块nodemailer-smtp-transport。
const transporter = nodemailer.createTransport(smtpTransport({
host:'mail.mydomanin.ro',
secureConnection: false,
tls: {
rejectUnauthorized: false
},
port: 587,
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
}));
Nodemailer 设置可使用代码通过网络邮件发送电子邮件
const nodemailer = require('nodemailer');
// Create a transport for sending emails
const sendMail = nodemailer.createTransport({
host: 'smtp.email.com',
port: 587,
secure: false,
auth: {
user: `${process.env.WEBMAIL_EMAIL}`,
pass: `${process.env.WEBMAIL_PASSWORD}`
},
tls: { rejectUnauthorized: false }
});
module.exports = { sendEmail };
// Function to send an email
const sendEmail = async (to, subject, text, html) => {
const mailOptions = {
from: process.env.WEBMAIL_EMAIL, // Sender address
to, // Recipient address
subject, // Subject line
text, // Plain text body
html // HTML body
};
try {
const info = await sendMail.sendMail(mailOptions);
console.log('Email sent: ', info.response);
} catch (error) {
console.error('Error sending email: ', error);
}
};