Nodejs:各位,nodemailer能实现用163邮箱发送邮件吗
Nodejs:各位,nodemailer能实现用163邮箱发送邮件吗
nodemailer能实现用163邮箱发送邮件吗,如果可以是使用nodemailer的SES吗,我看网上的例子有类似这种的, var transport = nodemailer.createTransport(“SES”, { AWSAccessKeyID: “AWSACCESSKEY”, AWSSecretKey: “/AWS/SECRET”, ServiceUrl: “https://email.us-east-1.amazonaws.com” // optional });
console.log(‘SES Configured’); 不知道有没有谁能告诉下
当然可以!nodemailer
是一个非常强大的库,可以用来发送电子邮件。它不仅可以与 Amazon SES(Simple Email Service)集成,还可以轻松地与各种其他SMTP服务器集成,包括163邮箱。
使用 nodemailer
发送邮件到163邮箱
首先,你需要安装 nodemailer
:
npm install nodemailer
然后,你可以使用以下示例代码来配置并发送邮件到163邮箱:
const nodemailer = require('nodemailer');
// 创建一个SMTP传输对象
let transporter = nodemailer.createTransport({
host: 'smtp.163.com', // 163邮箱的SMTP服务器地址
port: 465, // SMTP端口号,默认为465
secure: true, // 使用SSL,设置为true
auth: {
user: 'your-email@163.com', // 你的163邮箱账号
pass: 'your-password' // 你的163邮箱密码
}
});
// 邮件信息
let mailOptions = {
from: '"发件人名字" <your-email@163.com>', // 发件人
to: 'recipient@example.com', // 收件人
subject: 'Hello ✔', // 邮件主题
text: 'Hello world?', // 纯文本正文
html: '<b>Hello world?</b>' // HTML正文
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
解释
-
创建传输对象:
nodemailer.createTransport
方法用于创建一个传输对象。这里我们指定了163邮箱的SMTP服务器地址、端口、是否使用SSL以及认证信息(用户名和密码)。 -
邮件选项:
mailOptions
对象包含了邮件的所有必要信息,如发件人、收件人、主题、纯文本正文和HTML正文。 -
发送邮件:调用
transporter.sendMail
方法来发送邮件,并传入邮件选项和一个回调函数来处理发送结果。
注意事项
- 请确保你的163邮箱已启用SMTP服务,并且你已经获取了正确的用户名和密码。
- 如果你担心安全性,可以考虑使用环境变量来存储敏感信息,而不是直接写在代码中。
希望这些信息对你有所帮助!
当然能咯~
那我具体还要改哪里呢,那个 AWSAccessKeyID: “AWSACCESSKEY”, AWSSecretKey: “/AWS/SECRET”, ServiceUrl: “https://email.us-east-1.amazonaws.com” ,这部分是不是要改成163的配置,请问如何改呢,具体要怎么实现能说的详细点吗
api上不是都有么?https://github.com/andris9/Nodemailer
Nodejs:nodemailer
可以用来通过 163 邮箱发送邮件。nodemailer
并不直接支持 Amazon SES(Simple Email Service),所以您提到的 SES 配置方式不适合用于 163 邮箱。
示例代码
const nodemailer = require('nodemailer');
// 创建一个 SMTP 传输对象
let transporter = nodemailer.createTransport({
host: 'smtp.163.com', // 163 邮箱服务器地址
port: 465, // 163 邮箱 SMTP 端口号
secure: true, // 使用 SSL
auth: {
user: 'your-email@163.com', // 您的 163 邮箱地址
pass: 'your-password' // 您的 163 邮箱密码或授权码
}
});
// 邮件信息
let mailOptions = {
from: '"Sender Name" <your-email@163.com>', // 发件人
to: 'recipient-email@example.com', // 收件人
subject: 'Hello ✔', // 邮件主题
text: 'Hello world?', // 文本内容
html: '<b>Hello world?</b>' // HTML 内容
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('Message sent: %s', info.messageId);
});
注意事项
- 安全性:请不要在代码中硬编码您的邮箱密码,考虑使用环境变量来存储敏感信息。
- 授权码:163 邮箱可能需要使用授权码而不是密码进行 SMTP 认证。
- 防火墙和安全设置:确保您的网络环境允许通过 SMTP 端口发送邮件,并检查邮箱的安全设置,可能需要开启 SMTP 服务。
这个示例展示了如何使用 nodemailer
发送邮件到 163 邮箱服务器。您可以根据实际需求调整邮件内容和其他参数。