代码之家  ›  专栏  ›  技术社区  ›  Hanoch Giner

通过nodejs中的google api发送邮件失败

  •  13
  • Hanoch Giner  · 技术社区  · 11 年前

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

    我正在使用 googleapis 用于node.js中的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){});
    
    1 回复  |  直到 11 年前
        1
  •  13
  •   user1445240    11 年前

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

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

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