文档

SMTP 之 Nodejs 调用示例

更新时间:
一键部署

使用 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, the port is 465
  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. replyTo:'xxxxx@xxxxx.com',//custom reply address
  25. html:'<b>Hello world</b><img src="cid:01" style="width:200px;height:auto">', // html body
  26. attachments: [
  27. {
  28. filename: 'text0.txt',
  29. content: 'hello world!'
  30. },
  31. {
  32. filename: 'text1.txt',
  33. path: './app.js'
  34. },{
  35. filename:'test.JPG',
  36. path:'./Desert.jpg',
  37. cid:'01'
  38. }
  39. ],
  40. };
  41. // send mail with defined transport object
  42. transporter.sendMail(mailOptions, function(error, info){
  43. if(error){
  44. return console.log(error);
  45. }
  46. console.log('Message sent: ' + info.response);
  47. });
  • 本页导读 (0)
文档反馈