【问题标题】:Invalid arguments when sending basic email through GMail API通过 GMail API 发送基本电子邮件时参数无效
【发布时间】:2020-06-16 12:29:28
【问题描述】:

我正在尝试使用 Gmail API 文档中的示例,通过表格中的 Google Apps 脚本通过 Gmail API 发送电子邮件,但我收到:

异常:提供的参数数量无效。仅预期 2-4 行(第 240 行,文件“代码”)。

Line 240 是我打电话给Gmail.Users.Messages.send 的时候。这看起来是我可以构建的最基本的电子邮件。有人可以帮我解决我所缺少的吗?

function senDAMail() {

  var bdy =  "From: steve@gmail.com\n\r, To: steve@gmail.com\n\r, Subject: test\n\r\n\r, You first build the message with headers like From and Subject as you mentioned, but you have to encode the message before sending it. There is no way around that.";

  var encodemsg = Utilities.base64EncodeWebSafe(bdy);

  Gmail.Users.Messages.send({
    auth: this.oAuth2Client,
    'userId': 'steve@gmail.com',
    'resource': {
      'raw': encodemsg
    }
  });
}

【问题讨论】:

    标签: google-apps-script gmail-api


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • 请删除\n\r, To\n\r, Subject\n\r\n\r, YouP中的,
    • 请将\n\r 替换为\r\n 或仅替换为\n
    • Gmail.Users.Messages.send(resource, userId) 的参数是resource, userId
      • 就您而言,我认为resourceuserId 分别是{raw: encodemsg}"me"

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    function senDAMail() {
      var bdy =  "From: steve@gmail.com\nTo: steve@gmail.com\nSubject: test\n\nYou first build the message with headers like From and Subject as you mentioned, but you have to encode the message before sending it. There is no way around that.";
      var encodemsg = Utilities.base64EncodeWebSafe(bdy);
      Gmail.Users.Messages.send({raw: encodemsg}, "me");
    }
    

    参考:

    如果这不是您问题的直接解决方案,我深表歉意。

    【讨论】:

    • Tanaike,谢谢,这行得通。您是否有可能不是由 Google 维护的 Gmail API 的当前资源?我已经多次浏览您上面引用的链接,但他们的示例或解释都不够清楚,我无法成功构建电子邮件。他们进行的 API 调用甚至都不正确或过时。有一天,我偶然发现一条错误消息,让我可以盲目地将其调试到 Gmail.Users.Messages.send。
    • @vaportrailfacilities 感谢您的回复。我很高兴你的问题得到了解决。关于示例资源,我通过检查this document 中的示例脚本发布了我的答案。例如,在这种情况下,我检查了 python 的示例脚本。而且,this thread 也可能有用。但如果这对您的情况没有帮助,我深表歉意。
    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2020-01-06
    • 2012-01-07
    • 2023-03-19
    相关资源
    最近更新 更多