全部产品
Search
文档中心

邮件推送:SMTP 之 Nodejs 调用示例

更新时间:Aug 09, 2017

使用 Nodejs 通过 SMTP 协议发信

  1. // load nodemailer as follows
  2. // npm install nodemailer --save
  3. var nodemailer = require('nodemailer');
  4. // create reusable transporter object using SMTP transport
  5. var transporter = nodemailer.createTransport({
  6. "host": "smtpdm.aliyun.com",
  7. "port": 25,
  8. "secureConnection": true, // use SSL
  9. "auth": {
  10. "user": 'username@userdomain', // user name
  11. "pass": 'xxxxxxx' // password
  12. }
  13. });
  14. // NB! No need to recreate the transporter object. You can use
  15. // the same transporter object for all e-mails
  16. // setup e-mail data with unicode symbols
  17. var mailOptions = {
  18. from: 'NickName<username@userdomain>', // sender address mailfrom must be same with the user
  19. to: 'x@x.com, xx@xx.com', // list of receivers
  20. cc:'haha<xxx@xxx.com>', // copy for receivers
  21. bcc:'haha<xxxx@xxxx.com>', // secret copy for receivers
  22. subject: 'Hello', // Subject line
  23. text: 'Hello world', // plaintext body
  24. html: '<b>Hello world</b><img src="cid:01" style="width:200px;height:auto">', // html body
  25. attachments: [
  26. {
  27. filename: 'text0.txt',
  28. content: 'hello world!'
  29. },
  30. {
  31. filename: 'text1.txt',
  32. path: './app.js'
  33. },{
  34. filename:'test.JPG',
  35. path:'./Desert.jpg',
  36. cid:'01'
  37. }
  38. ],
  39. };
  40. // send mail with defined transport object
  41. transporter.sendMail(mailOptions, function(error, info){
  42. if(error){
  43. return console.log(error);
  44. }
  45. console.log('Message sent: ' + info.response);
  46. });