NodeJs 无法使用 gmail smtp 发送电子邮件

NodeJs fails to send email with gmail smtp

看了一些帖子,我不知道为什么这个流星代码无法将邮件发送到我自己的电子邮件地址。非常感谢任何想法。

//server.main.js

import { Email } from 'meteor/email'

smtp = {
  username: 'my@gmail.com',
  password: 'pass',
  server: 'smtp.gmail.com',
  port: '465'
};

process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;

//then inside a method call
Testimonies.insert(testamonyObj, function(err, res) {
      if (!err) {
        let add = 'myAdd@gmail.com'
        let mess = 'alosh bel awee'
        Email.send({ add, add, res, mess });
      }

你的代码乍一看还不错。您需要在您的 gmail 帐户中允许安全性较低的应用程序。

您可以在此处阅读更多相关信息:https://support.google.com/accounts/answer/6010255?hl=en

好吧,您传递给 Email.send 的选项对象不包含必要的字段,最值得注意的是 to 字段,请参阅 https://docs.meteor.com/api/email.html#Email-send。您当前的对象仅包含三个字段 addresmess、none,其中 send 函数可以理解的有效字段名称。

试试这个:

Email.send({ 
  to: add,
  from: add,
  subject: res,
  text: mess
});