这是我第一次使用 Nodemailer,我在与 godaddy 验证我的详细信息时遇到问题。
这是我在node.js/express 后端路由器中的功能:
const router = require("express").Router();
const nodemailer = require("nodemailer");
const simplesmtp = require("simplesmtp");
const MailParser = require("mailparser").MailParser;
const imaps = require("imap-simple");
// Function to send an email
function sendEmail(data) {
// SMTP configuration
const smtpTransport = nodemailer.createTransport({
host: "smtp.office365.com",
port: 587,
secure: true,
tls: {
ciphers: "SSLv3",
minVersion: "TLSv1.2", // Use TLS 1.2 or higher
},
auth: {
user: "myemail@mydomain.com",
pass: "mypassword",
},
});
const mailOptions = {
to: data.email,
from: "myemail@mydomain.com",
subject: "Test Email",
text: data.message,
};
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(`Error sending email: ${error}`);
} else {
console.log(`Email sent: ${info.response}`);
}
smtpTransport.close();
});
}
// Function to create the SMTP server for receiving emails
function createSMTPServer() {
// Create a simple SMTP server to receive emails
const server = simplesmtp.createServer({
debug: false,
});
server.on("startData", (connection) => {
console.log(`Message from: ${connection.from}`);
console.log(`Message to: ${connection.to}`);
connection.saveStream = new MailParser();
connection.saveStream.on("end", (mail) => {
console.log("Received email:");
console.log(`Subject: ${mail.subject}`);
console.log(`From: ${mail.from.text}`);
console.log(`To: ${mail.to.text}`);
console.log(`Body: ${mail.text}`);
});
connection.pipe(connection.saveStream);
});
server.listen(25);
}
// Function to connect to the IMAP server and start listening for emails
function connectToIMAPServer() {
// IMAP configuration
const imapConfig = {
imap: {
user: "myemail@mydomain.com", // Replace with your Office365 email address
password: "mypassword", // Replace with your Office365 email password
host: "outlook.office365.com",
port: 993,
tls: true, // Use TLS encryption
authTimeout: 10000, // Set an authentication timeout
},
onmail: (numNewMsgs) => {
console.log(`You have ${numNewMsgs} new email(s).`);
},
};
// Connect to the IMAP server and start listening for emails
imaps
.connect(imapConfig)
.then((connection) => {
return connection.openBox("INBOX").then(() => {
console.log("Connected to IMAP server");
});
})
.catch((err) => {
console.error(`Error connecting to IMAP server: ${err}`);
});
}
// Handle the /contact route
router.post("/contact", (req, res) => {
let data = req.body;
console.log(data);
if (
data.name.length === 0 ||
data.email.length === 0 ||
data.message.length === 0
) {
return res.json({ msg: "Please fill out all the fields" });
}
// Send an email
sendEmail(data);
// You can also start the SMTP server for receiving emails if needed
// createSMTPServer();
// Connect to the IMAP server and start listening for emails
connectToIMAPServer();
res.json({ msg: "Email sent and listening for incoming emails" });
});
module.exports = router;
当然我有我的真实电子邮件和密码。
这是我在 VSCode 控制台中收到的错误消息:
[0] Error sending email: Error: 34400000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:
我尝试过此代码的其他版本,但只收到其他错误消息,例如身份验证失败。我使用的是登录我的 godaddy/office365 电子邮件地址的普通密码。
我与 goDaddy 交谈,他们给了我我需要的配置设置 - 用户名: 电子邮件地址 密码:邮箱密码 接收设置 服务器名称: Outlook.office365.com (POP/IMAP)端口: POP:995,选择 SSL IMAP:993,选择 SSL 加密方法: TLS (POP/IMAP) 传出设置 服务器名称: smtp.office365.com端口: 587加密方法: 启动TLS
当我告诉他们我仍然遇到问题时,他们说这是我的问题,而不是他们的问题。
有谁可以帮我吗?
更新: 当我使用该函数的更简单版本时:
const router = require("express").Router();
const nodemailer = require("nodemailer");
router.post("/contact", (req, res) => {
let data = req.body;
if (
data.name.length === 0 ||
data.email.length === 0 ||
data.message.length === 0
) {
return res.json({ msg: "Please fill out all the fields" });
}
let smtpTransport = nodemailer.createTransport({
service: "Godaddy",
host: "smtpout.secureserver.net",
secureConnection: false,
port: 587,
auth: {
user: "myemail@mydomain.com", // Your GoDaddy email address
pass: "mypassword", // Your GoDaddy email password
},
});
let mailOptions = {
from: data.email,
to: "myemail@mydomain.com", // Your GoDaddy email address
subject: `Message from ${data.name}`,
html: `
<h3>Informations</h3>
<ul>
<li>Name: ${data.name}</li>
<li>Email: ${data.email}</li>
</ul>
<h3>Message</h3>
<p>${data.message}</p>
`,
};
smtpTransport.sendMail(mailOptions, (err) => {
try {
if (err) {
console.log(err);
return res.status(400).json({ msg: "Failed to send email" });
}
console.log("success");
res.status(200).json({ msg: "Message was sent successfully" });
} catch (err) {
if (err)
return res
.status(500)
.json({ msg: "There was an unexpected server error." });
}
});
});
module.exports = router;
它给了我以下错误:
[0] Error: Invalid login: 535 Authentication Failed for myemail@mydomain.com
[0] at SMTPConnection._formatError (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
[0] at SMTPConnection._actionAUTHComplete (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:1564:34)[0] at SMTPConnection.<anonymous> (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:546:26)
[0] at SMTPConnection._processResponse (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:969:20)
[0] at SMTPConnection._onData (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:755:14)
[0] at SMTPConnection._onSocketData (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:193:44)
[0] at TLSSocket.emit (node:events:513:28)
[0] at addChunk (node:internal/streams/readable:324:12)
[0] at readableAddChunk (node:internal/streams/readable:297:9)
[0] at Readable.push (node:internal/streams/readable:234:10) {
[0] code: 'EAUTH',
[0] response: '535 Authentication Failed for myemail@mydomain.com',
[0] responseCode: 535,
[0] command: 'AUTH PLAIN'
[0] }
然后它开始给我这个错误:
[0] Error: Mail command failed: 550 User myemail@mydomain.com has exceeded its 24-hour sending limit. Messages to 5 recipients out of 5 allowed have been sent. Relay quota will reset in 13.69 hours.
[0] at SMTPConnection._formatError (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
[0] at SMTPConnection._actionMAIL (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:1594:34)
[0] at SMTPConnection.<anonymous> (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:1063:18)
[0] at SMTPConnection._processResponse (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:969:20)
[0] at SMTPConnection._onData (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:755:14)
[0] at SMTPConnection._onSocketData (C:\Users\Baily\Desktop\nodemailer\nodemailer-form\node_modules\nodemailer\lib\smtp-connection\index.js:193:44)
[0] at TLSSocket.emit (node:events:513:28)
[0] at addChunk (node:internal/streams/readable:324:12)
[0] at readableAddChunk (node:internal/streams/readable:297:9)
[0] at Readable.push (node:internal/streams/readable:234:10) {
[0] code: 'EENVELOPE',
[0] response: '550 User myemail@mydomain.com has exceeded its 24-hour sending limit. Messages to 5 recipients out of 5 allowed have been sent. Relay
quota will reset in 13.69 hours.',
[0] responseCode: 550,
[0] command: 'MAIL FROM'
[0] }
尽管我还没有成功发送/接收这样的一封电子邮件。 同样奇怪的是,每 24 小时内只能发送 5 封电子邮件......
你们有2FA认证吗?也许您需要配置您的邮件以获得第三方软件登录权限。