【问题标题】:Unable to use gmail SMTP无法使用 gmail SMTP
【发布时间】:2012-05-06 12:42:38
【问题描述】:

我正在尝试使用 node_mailer 来使用 gmail smtp。我的 nodejs 日志中出现以下错误(使用 nodester)。这是我的代码:

var email = require('mailer');
email.send({
    host : "smtp.gmail.com",              
    port : "465",                     
    ssl : true,
    domain : "domain.com",            
    to : "emailId@gmail.com",
    from : "email@gmail.com",
    subject : "You have been registered",
    body: "<B>Hello! This is a test of the node_mailer.</B>",
    authentication : "login",        // auth login is supported; anything else is no auth
    username : /* username */,
    password : /* password */      
    },
    function(err, result){
      if(err){ self.now.error(err); console.log(err); return;}
      else this.now.successfullySent(result);
});

我在堆栈中没有收到任何错误,但电子邮件未送达。

@work4liberty 和@David Ellis。感谢您的两个输入,但似乎问题不在于我的服务器代码,我从客户端 javascript 中发送了错误的 emailId 值。 Nodemailer 确实帮助我调试了错误的正确文本。

【问题讨论】:

  • 请正确缩进代码

标签: node.js smtp gmail


【解决方案1】:

几件事:

  1. 您编写的代码将引发解释器错误并且无法运行。 (例如,电子邮件地址周围没有引号。)
  2. 该库已经有一段时间没有更新了。我强烈推荐积极开发且名称相似的 Nodemailer 库,它具有 gmail 的 SMTP 等常用服务的简写。

完全披露:我为 Nodemailer 贡献了 Amazon SES 功能(尽管最近将底层架构从 0.1.x 重写为 0.3.x 意味着我没有出现在 git blame 上,不再)。

编辑:我仔细查看了您的代码。假设您的实际代码中没有拼写错误,我怀疑以下行是罪魁祸首:this.now.successfullySent(result);

基本上,回调函数的this 不是你认为的那样。您需要通过将this 对象分配给self 之类的变量来将其缓存在其范围内。 (假设我们没有处理底层库中的问题。)

【讨论】:

  • 我在这里发帖时不小心删除了电子邮件地址周围的引号,我已经更正了。我还缓存了thisself。但这不是问题。我仍然无法发送邮件。好点虽然这个库自过去 7 个月以来没有更新。将尝试 nodemailer。
【解决方案2】:

用我的密码替换 xxx 这个代码对我有用 复制并粘贴它,看看它是如何工作的 如果它对你不起作用,也许只是延迟女巫不时发生,或者(我希望不是)你的 ip 或你的 msg 中的某些东西让 gmail 认为它是垃圾邮件

var email = require('mailer');
email.send({
    host : "smtp.gmail.com",
    port : "465",
    ssl : true,
    domain : "domain.com",
    to : "work4liberty@gmail.com",
    from : "work4liberty@gmail.com",
    subject : "You have been registered",
    body: "<B>Hello! This is a test of the node_mailer.</B>",
    authentication : "login",        // auth login is supported; anything else $
    username : 'work4liberty@gmail.com',
    password : 'xxx'
    },
    function(err, result){
      if(err){ self.now.error(err); console.log(err); return;}
      else console.log('looks good')
});

【讨论】:

  • 我的代码和你的一模一样。我仍然无法发送邮件。这可能是因为我的应用程序部署在 nodester 上,对此有什么想法吗?
  • 嘿。您可以按照我通常使用的建议尝试 Nodemailer。如果您能找到该 IP,您可以检查该垃圾邮件数据库,或者甚至直接询问 gmail 是否以某种方式阻止它。也许您可以询问 nodester 是否被 gmail 阻止或以某种方式阻止。
  • 其实我都试过了,现在都可以了。问题出在我的客户端脚本上。我没有正确获取电子邮件 ID,Duh!我多么愚蠢。我已经在我的问题中写了这个。
猜你喜欢
  • 1970-01-01
  • 2014-09-13
  • 2012-06-16
  • 1970-01-01
  • 2015-07-25
  • 2018-10-29
  • 2021-07-06
  • 2011-11-30
  • 2014-05-03
相关资源
最近更新 更多