【问题标题】:Failed sending mail through google api in nodejs在nodejs中通过google api发送邮件失败
【发布时间】:2014-10-02 03:40:15
【问题描述】:

我正在尝试通过 Google API 发送电子邮件。

我在 node.js 中使用 googleapis 访问 Google API。

我的问题是,当我尝试发送没有附件的简单邮件时,出现以下错误:

'raw' RFC822 有效负载消息字符串或通过 /upload/* URL 上传消息

我没有在我的请求中定义有附件,我在电子邮件地址中没有看到任何错误。

请帮忙。

我的代码:

    var google = require('googleapis');
    var gmailClass = google.gmail('v1');

    var email_lines = [];

    email_lines.push("From: \"Some Name Here\" <rootyadaim@gmail.com>");
    email_lines.push("To: hanochg@gmail.com");
    email_lines.push('Content-type: text/html;charset=iso-8859-1');
    email_lines.push('MIME-Version: 1.0');
    email_lines.push("Subject: New future subject here");
    email_lines.push("");
    email_lines.push("And the body text goes here");
    email_lines.push("<b>And the bold text goes here</b>");

    var email =email_lines.join("\r\n").trim();

    var base64EncodedEmail = new Buffer(email).toString('base64');

    gmailClass.users.messages.send({
        auth: OAuth2Client,
        userId: "me",
        message: 
        {
             raw: base64EncodedEmail
        }           
      },
    function(err, results){});

【问题讨论】:

  • 您是否尝试让base64EncodedEmail url 安全?示例:base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_')
  • @mscdex 它确实改变了字符串中的部分,但我仍然遇到同样的错误
  • 我也有同样的问题。无论我发送什么,API 接缝都会做出相同的响应。
  • 我什至尝试发回我收到的原始消息作为 API 本身的响应。我收到了同样的错误信息。

标签: javascript node.js google-api google-api-nodejs-client


【解决方案1】:

对 1.0.3 版的 google api 进行了更改。尝试使用以下语法:

gmailClass.users.messages.send({
    auth: OAuth2Client,
    userId: "me",
    resource: 
    {
         raw: base64EncodedEmail
    }           
  }

确保 base64EncodedEmail 是 url 安全的。您可以使用 mscdex 发布的base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_') 代码。此语法适用于 v. 1.0.11

【讨论】:

猜你喜欢
  • 2018-07-12
  • 2015-08-15
  • 2011-12-10
  • 1970-01-01
  • 2021-04-18
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多