nodejs发送邮件
时间: 2023-06-30 20:20:09 浏览: 69
要使用Node.js发送电子邮件,您需要使用一个称为NodeMailer的npm包。
以下是使用NodeMailer发送电子邮件的基本步骤:
1. 安装NodeMailer:
```
npm install nodemailer
```
2. 导入NodeMailer模块:
```
const nodemailer = require('nodemailer');
```
3. 创建一个传输对象:
```
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your_email_address@gmail.com',
pass: 'your_email_password'
}
});
```
4. 创建一个邮件选项对象:
```
let mailOptions = {
from: 'your_email_address@gmail.com',
to: 'recipient_email_address',
subject: 'Subject of the email',
text: 'Body of the email'
};
```
5. 发送电子邮件:
```
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
```
替换上述代码中的“ your_email_address”和“ your_email_password”为您的电子邮件地址和密码,将“ recipient_email_address”替换为您要发送电子邮件的收件人的电子邮件地址,将“ Subject of the email”替换为您的电子邮件主题,将“ Body of the email”替换为您的电子邮件正文。
请注意,如果您使用的是Gmail帐户,则需要启用“ 您的帐户的低安全性应用程序访问权限”,以便NodeMailer能够发送电子邮件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)