简体   繁体   English

使用 Gmail 的 API JavaScript 获取包含附件的电子邮件

[英]Get emails that contains attachment using Gmail's API JavaScript

I'm trying to get a list of emails that contain attachments, I'm getting a list of messages, each message contains 2 properties - id and threadID - I assumed that since the messages do not hold an AttachmentID property, they do not have one either.我正在尝试获取包含附件的电子邮件列表,我正在获取邮件列表,每封邮件包含 2 个属性 - id 和 threadID - 我假设由于邮件不包含 AttachmentID 属性,因此它们没有一个。

I didn't find a way to retrieve only messages with attachments.我没有找到只检索带有附件的邮件的方法。

This is the code:这是代码:

function execute() {
    return gapi.client.gmail.users.messages.list({
        "userId": "myid@gmail.com",
        "includeSpamTrash": false,
        "maxResults": 100,
    }).then(function (response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
            },
            function (err) { console.error("Execute error", err); });
}

To retrieve only the messages that contains an attachment, you should use the method users.messages.list while including a q parameter to show only messages with attachments.要仅检索包含附件的消息,您应该使用方法users.messages.list并包含q参数以仅显示带有附件的消息。 That q parameter can be set up with the string has:attachment .可以使用字符串has:attachment设置该q参数。 On JavaScript that requests looks like the following:在 JavaScript 上,请求如下所示:

  function execute() {
    return gapi.client.gmail.users.messages.list({
      "userId": "{ YOUR USERID HERE }",
      "q": "has:attachment"
    })
        .then(function(response) {
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }

Please remember to update the userId parameter to match your own scenario.请记住更新userId参数以匹配您自己的场景。

function execute() {
  return gapi.client.gmail.users.messages.list({
    "userId": "myid@gmail.com",
    "q": "has:attachment", // This right here
    "includeSpamTrash": false,
    "maxResults": 100,
  })
    .then(
      function (response) {
        // Handle the results here (response.result has the parsed body).
        console.log("Response", response);
      },
      function (err) {
        console.error("Execute error", err)
      }
    )
}

The "q" parameter has a horde of options you can use to filter the output. “q”参数有一大堆选项可用于过滤 output。 This also works in the Gmail UI.这也适用于 Gmail UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM