使用node.js实现点击按钮发送信息到qq邮箱
时间: 2024-03-10 08:51:20 浏览: 41
要使用 Node.js 实现点击按钮发送信息到 qq 邮箱,你需要使用 Node.js 的第三方邮件发送库。这里以 nodemailer 为例进行说明。
首先,你需要在你的项目中安装 nodemailer:
```
npm install nodemailer
```
然后,在你的 Express 应用中添加路由处理 `/sendemail`,处理前端发送过来的邮件发送请求。示例代码如下:
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/sendemail', (req, res) => {
let transporter = nodemailer.createTransport({
service: 'qq',
secure: true,
auth: {
user: '你的 QQ 邮箱地址',
pass: '你的 QQ 邮箱密码'
}
});
let mailOptions = {
from: '你的 QQ 邮箱地址',
to: req.body.to,
subject: req.body.subject,
text: req.body.message
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.send('邮件发送失败');
} else {
console.log('邮件发送成功:' + info.response);
res.send('邮件发送成功');
}
});
});
app.listen(3000, () => {
console.log('服务器已启动');
});
```
这段代码使用 nodemailer 创建一个邮件传输对象 `transporter`,并设置服务提供商为 `qq`,使用 QQ 邮箱的 SMTP 服务器发送邮件。在创建 `mailOptions` 对象时,我们使用了表单数据中的收件人地址、主题和正文。最后,我们使用 `transporter.sendMail()` 方法发送邮件,并在邮件发送完成后向前端发送响应。
在前端页面中,你可以使用 jQuery 或其他类库监听按钮的点击事件,并使用 AJAX 技术将表单数据发送到 `/sendemail` 路由。示例代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>发送邮件</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form>
<label for="to">收件人:</label>
<input type="email" id="to" name="to" required><br>
<label for="subject">主题:</label>
<input type="text" id="subject" name="subject" required><br>
<label for="message">内容:</label>
<textarea id="message" name="message" required></textarea><br>
<button type="button" id="send">发送</button>
</form>
<script>
$(function() {
$('#send').click(function() {
var to = $('#to').val();
var subject = $('#subject').val();
var message = $('#message').val();
$.post('/sendemail', {to: to, subject: subject, message: message}, function(response) {
alert(response);
});
});
});
</script>
</body>
</html>
```
这里使用了 jQuery 的 `$.post()` 方法将表单数据发送到 `/sendemail` 路径,然后在后端服务器上监听该路径。当邮件发送完成后,我们在前端弹出一个提示框,显示邮件发送结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)