使用 nodemailer 发送邮件
Send Email using nodemailer
我正在使用 nodemailer 在 ReactJS 中发送电子邮件。
我引用了 https://www.w3schools.com/nodejs/nodejs_email.asp
中的代码并执行相同的操作。
但我得到错误:net.isIP 不是一个函数
这是我的代码:
import React from "react";
import nodemailer from "nodemailer";
export default class App extends React.Component {
componentDidMount() {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "xuanle.khtn@gmail.com",
pass: "********",
},
});
var mailOptions = {
from: "xuanle.khtn@gmail.com",
to: "leanhxuan_t59@hus.edu.vn",
subject: "Sending Email using Node.js",
text: "That was easy!",
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
}
render() {
return <div></div>;
}
}
当我 运行 应用程序时,我收到来自浏览器的消息:
Uncaught TypeError: net.isIP is not a function
at Object.push../node_modules/nodemailer/lib/shared/index.js.module.exports.resolveHostname (index.js:36)
at SMTPConnection.connect (index.js:299)
at index.js:263
at index.js:70
at run (setImmediate.js:40)
at runIfPresent (setImmediate.js:69)
at onGlobalMessage (setImmediate.js:109)
I'm newbie so i don't have any idea with this error.
How to
resolve this problem?
您在 createTransport 对象中缺少主机和端口:
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
});
nodemailer 模块不能在客户端工作,在那里使用它是不安全的。
如果是在后端,错误基本上是在解析主机名时遇到一些问题。
因此,如果您使用的是 gmail,您的代码应如下所示:
//Create transport + auth
let transporter = mailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: creds.userName,
pass: creds.password
}
});
请记住为 nodemailer 设置一个应用程序密码,而不是使用您的真实密码。
这是获取我们项目的工作代码。
或许可以作为某种参考。
'use strict'
//Dependencies
const mailer = require('nodemailer');
//Credentials
const creds = require('./creds/gmail');
//Mailing Function
const mailFunc = (subject, recipient, msg, cbMail) => {
//Create transport + auth
let transporter = mailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: creds.userName,
pass: creds.password
}
});
//Create the message object
let message = {
from: 'example@gmail.com',
to: recipient,
subject: subject,
text: msg,
// html: "<p>HTML version of the message</p>"
};
//Send mail
transporter.sendMail(message)
.then(res => {
cbMail(res.response);
})
.catch(err => {
cbMail(err);
})
}
//Export the module
module.exports = { mailFunc }
我正在使用 nodemailer 在 ReactJS 中发送电子邮件。
我引用了 https://www.w3schools.com/nodejs/nodejs_email.asp
中的代码并执行相同的操作。
但我得到错误:net.isIP 不是一个函数
这是我的代码:
import React from "react";
import nodemailer from "nodemailer";
export default class App extends React.Component {
componentDidMount() {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "xuanle.khtn@gmail.com",
pass: "********",
},
});
var mailOptions = {
from: "xuanle.khtn@gmail.com",
to: "leanhxuan_t59@hus.edu.vn",
subject: "Sending Email using Node.js",
text: "That was easy!",
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
}
render() {
return <div></div>;
}
}
当我 运行 应用程序时,我收到来自浏览器的消息:
Uncaught TypeError: net.isIP is not a function
at Object.push../node_modules/nodemailer/lib/shared/index.js.module.exports.resolveHostname (index.js:36)
at SMTPConnection.connect (index.js:299)
at index.js:263
at index.js:70
at run (setImmediate.js:40)
at runIfPresent (setImmediate.js:69)
at onGlobalMessage (setImmediate.js:109)
I'm newbie so i don't have any idea with this error.
How to resolve this problem?
您在 createTransport 对象中缺少主机和端口:
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
});
nodemailer 模块不能在客户端工作,在那里使用它是不安全的。
如果是在后端,错误基本上是在解析主机名时遇到一些问题。
因此,如果您使用的是 gmail,您的代码应如下所示:
//Create transport + auth
let transporter = mailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: creds.userName,
pass: creds.password
}
});
请记住为 nodemailer 设置一个应用程序密码,而不是使用您的真实密码。
这是获取我们项目的工作代码。
或许可以作为某种参考。
'use strict'
//Dependencies
const mailer = require('nodemailer');
//Credentials
const creds = require('./creds/gmail');
//Mailing Function
const mailFunc = (subject, recipient, msg, cbMail) => {
//Create transport + auth
let transporter = mailer.createTransport({
host: "smtp.gmail.com",
port: 587,
auth: {
user: creds.userName,
pass: creds.password
}
});
//Create the message object
let message = {
from: 'example@gmail.com',
to: recipient,
subject: subject,
text: msg,
// html: "<p>HTML version of the message</p>"
};
//Send mail
transporter.sendMail(message)
.then(res => {
cbMail(res.response);
})
.catch(err => {
cbMail(err);
})
}
//Export the module
module.exports = { mailFunc }