简体   繁体   English

我还想获取消息 Gmail API 随附的附件

[英]I want to get attachments as well that comes with the message Gmail API

Okay so with this code I am able to get the actual mail content which comes by Email but what I want is I want to get message and attachment too.好的,因此使用此代码,我可以获得 Email 提供的实际邮件内容,但我想要的是我也想获得消息和附件。 what can be done in order to list out attachments and later on giving option to download the attachments gmail api.可以做些什么来列出附件,然后再提供下载附件的选项 gmail api。

    var ifrm = document.getElementById("iframe").contentWindow.document;
    ifrm.body.innerHTML = getMessageBody(message.payload);
  };

  let getMessageBody = (message) => {
    var encodedBody = "";
    if (typeof message.parts === "undefined") {
      encodedBody = message.body.data;
    } else {
      encodedBody = getHTMLPart(message.parts);
    }

    return Base64.decode(encodedBody);
  };

  let getHTMLPart = (arr) => {
    for (var x = 0; x <= arr.length; x++) {
      if (typeof arr[x].parts === "undefined") {
        if (arr[x].mimeType === "text/html") {
          return arr[x].body.data;
        }
      } else {
        return getHTMLPart(arr[x].parts);
      }
    }
    return "";
  };

Gmail API when I click on message.当我单击消息时,Gmail API。

 getOneMessage = (messageId) => {
    return window.gapi.client.gmail.users.messages
      .get({
        userId: "me",
        id: messageId,
      })
      .then(
        (response) => {
          this.setState({
            message: response.result,
          });
        },
        (err) => {
          console.error("getMessage error", err);
        }
      );
  }; 
  

  handleMessageClick = (e) => {
    const messageId = e.currentTarget.getAttribute("id");
    this.getOneMessage(messageId);
    

Solution解决方案

You are using the Users.messages: get endpoint.您正在使用Users.messages: get端点。 This is fine to retrieve the message body, but to retrieve attachments you will have to use the Users.messages.attachments: get .这可以检索邮件正文,但要检索附件,您必须使用Users.messages.attachments: get Here you can find a link to the documentation. 在这里您可以找到文档的链接。

Proposed code editing:建议的代码编辑:

getAttachments = (message, callback) => {
  var parts = message.payload.parts;
  for (var i = 0; i < parts.length; i++) {
    var part = parts[i];
    if (part.filename && part.filename.length > 0) {
      var attachId = part.body.attachmentId;
      var request = gapi.client.gmail.users.messages.attachments.get({
        'id': attachId,
        'messageId': message.id,
        'userId': userId
      });
      request.execute(function(attachment) {
        callback(part.filename, part.mimeType, attachment);
      });
    }
  }
}

 getOneMessage = (messageId) => {
    return window.gapi.client.gmail.users.messages
      .get({
        userId: "me",
        id: messageId,
      })
      .then(
        (response) => {
          this.setState({
            message: response.result,
          });
          // Get the attachment and do something with it
          getAttachments(response.result, callback);
        },
        (err) => {
          console.error("getMessage error", err);
        }
      );
  }; 
  

  handleMessageClick = (e) => {
    const messageId = e.currentTarget.getAttribute("id");
    this.getOneMessage(messageId);

Reference参考

Users.messages.attachments 用户.消息.附件

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

相关问题 从gmail api中的&#39;message id&#39;获取消息 - get message from 'message id' in gmail api 如何以HTML显示来自Gmail API的内联图像和附件-难题的最后一部分 - How do I display Inline images and Attachments from Gmail API in HTML — Last part of the puzzle 如何使用gmail api javascript获取读取和未读消息列表 - how to get read and unread message list using gmail api javascript 我无法通过 gmail-api 中的 id 检索邮件 - I am unable to retrieve the message by its id in gmail-api 我想在 iam 输入 gmail id 时自动建议 @gmail.com - i want to get auto suggest @gmail.com when iam type gmail id Sending email with plain text, attachments and html using gmail api in reactjs - Sending email with plain text, attachments and html using gmail api in reactjs 如何使用Gmail API和Javascript发送带有本地附件的电子邮件? - How to send emails with Local Attachments using Gmail API and Javascript? 如何在 Google Apps 脚本(Gmail API)中调整附件的存在? - How to condition the existance of attachments in Google Apps Script (Gmail API)? 我想隐藏表格并显示书面的成功消息,以及用哈希值重新加载页面 - I want to hide the form and show a written success message as well as reload the page with a hash value 在Gmail中搜索xls附件 - search Gmail for xls attachments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM