Files
wahyd4.github.com/source/_posts/node-js-send-e-mail.md
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

111 lines
3.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "使用Node.js发送电子邮件(E-mail"
date: 2011-10-26 00:00:00
tags: [Node.js, email, 发送]
---
10月27日更新,可以发送附件
今天晚上花了点时间试了一下使用node.js发送电子邮件,也挺简单的,~~不过有个问题一直没有解决,就是发送附件,~~我弄明白的时候,会补充资料的。
首先我们还是得安装node的第三方用户发送email的module 。
```
npm install nodemailer
```
安装好之后,还有一个问题需要注意,我们应该确保用来发送邮件的邮箱地址是打开只是IMAP 、SMTP功能的,这样才可以发送邮件成功。
接下来我们就开始写代码,其实很简单,官方网站也都已经说得很清楚了。
```jscript
var mail = require('nodemailer'); //包含发送邮件所需module
mail.SMTP = {
use_authentication: true, //如果我们使用QQ等邮箱,这个必须写且为true
host: 'smtp.qq.com', //定义用来发送邮件的邮箱服务器,一般是QQ这些的
port:25, //定义服务器端口,一般是25 ,如果是使用SSL端口一般为465,或者587
ssl:false, //默认不适用SSL,可以省略不写
user: 'wahyd4@qq.com', //邮箱用户名
pass:'*****' //输入邮箱密码
}
mail.send_mail(
{
sender:'wahyd4@qq.com', //发送邮件的地址
to:'wahyd4@gmail.com', //发给谁
subject:'随便把', //主题
body:'Hello,这个邮件来自node.js'
},
//回调函数,用户判断发送是否成功,如果失败,输出失败原因。
function(error,success){
if(!error){
console.log('message success');
}else{
console.log('failed'+error);
}
}
);
```
这里我没有使用SSL进行发送,而且发送的是纯文本,也可以发送html,如果发送html则在send\_mail函数里面加上:
```
html: ' 你好。。邮件来自node.js',
```
如果使用SSL进行发送我们则需要将mail.SMTP改为:
```jscript
mail.SMTP = {
use_authentication: true, //如果我们使用QQ等邮箱,这个必须写且为true
host: 'smtp.qq.com', //定义用来发送邮件的邮箱服务器,一般是QQ这些的
port:465, //定义服务器端口,一般是25 ,如果是使用SSL端口一般为465
ssl:true, //默认不适用SSL,可以省略不写
user: 'wahyd4@qq.com', //邮箱用户名
pass:'*****' //输入邮箱密码
}
```
好了。现在你运行程序试试,是不是成功了。
我今天晚上遇到一个错误 :**503 ERROR,need EHLO and AUTH first**。就是因为没有添加use\_authentication = true的缘故。加上之后,搞定。
———————————————————分割线———————-
发送附件的话。首先我们得使用
```jscript
var fs = require('fs');    //导入文件系统相关的包
```
饭后我们将文件读入内存,并将其发送。
```jscript
//读入文件
var img = fs.readFileSync(__dirname+"/cat.jpg");
var attachment = [{
'filename': "cat.jpg", //这里只是给附件取名称,而不是导入文件内容
'contents': img //导入文件
}] //定义我们需要发送的附件
```
接下来我们需要在send\_mail函数中加入发送附件即可
```jscript
mail.send_mail(
{
sender:'wahyd4@qq.com', //发送邮件的地址
to:'wahyd4@gmail.com', //发给谁
subject:'测试发送文件', //主题
attachments: attachment, //添加附件
body:'Hello,这个邮件来自node.js,包含附件哦,亲!'
},
```
附上Nodemailer的网址:<http://www.nodemailer.org/>
enjoy it!
最后还是感谢 [andris9](https://github.com/andris9) 大叔很快帮我解决了问题!