javascript - 在nodejs中通过google api发送邮件失败

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

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

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

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

'raw' RFC822 payload message string or uploading message via /upload/* URL required

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

请帮忙。

我的代码:

    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){});

最佳答案

对 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

关于javascript - 在nodejs中通过google api发送邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207217/

相关文章:

node.js - 如何将 "CTRL+C"发送到 Node.js 中的子进程?

javascript - 如何从选项列表中获取一项

android - keystore 文件不存在

javascript - jsPDF-AutoTable 中的 Rowspan 问题

javascript - 为所有内容添加一个外部 js 文件?

javascript - 对于连接速度较慢的用户,我想用图像标签替换视频标签

api - 谷歌博客搜索 API 的替代 API

javascript - 如何在鼠标悬停在 MVC 中的链接上时在链接页面的小弹出窗口中显示实时预览?

node.js:修复 ev_ref/ev_unref 警告

django - oauth2client 与 django flowfield