简体   繁体   English

Gmail API标签Android

[英]Gmail API labels Android

I am using the Gmail API for Android for an application to send emails. 我正在使用适用于Android的Gmail API来发送电子邮件。 I want to send the mails such that they are received in the Social group of messages. 我想发送邮件,以便在“社交”邮件组中收到它们。

So is it possible in any way that I can set the labels for an email while sending it using the Gmail API ? 因此,可以通过任何方式在使用Gmail API发送电子邮件时设置电子邮件的标签吗?

It is possible for us to set labels while sending mails through mail.google.com so how can the same be achieved with the Gmail API ? 通过mail.google.com发送邮件时,我们可以设置标签,那么如何使用Gmail API来实现相同的目的?

You cannot specify the labels message should have when sending it . 您不能指定发送消息时应具有的标签消息 Under what tab certain messages should end up under is up to the user . 在某些选项卡下,某些消息最终应由用户决定

But if you are sending a message to the user himself howewer, it's not that hard to modify the message once it has been sent. 但是,如果您要向用户本人发送消息,则在发送完消息后修改消息并不难。 When you send a message, you get all the label that were applied to the message in the response. 发送消息时,您将获得响应中应用于该消息的所有标签。 Just send a message , and then modify the labels , and you are done. 只需发送一条消息 ,然后修改标签 ,即可完成。

Here's an example (with regular http-requests, but you could do the same with one of the client libraries): 这是一个示例(使用常规的http请求,但是您可以对其中一个客户端库执行相同的操作):

// Base64-encoding the message and making it url-safe by replacing
// all '+' with '-', and all '/' with '_'.
btoa("To: example@gmail.com\n" +
     "From: example@gmail.com\n" +
     "Subject: Cool man\n\n" +

     "Here is the message").replace(/\+/g, '-').replace(/\//g, '_');

// => "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"

Sending the message: 发送消息:

POST https://www.googleapis.com/gmail/v1/users/me/messages/send

{
  "raw": "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"
}

Response 响应

{
 "id": "150866b2f6956617",
 "threadId": "150866b2f6956617",
 "labelIds": [
  "SENT",
  "INBOX",
  "UNREAD"
 ]
}

Then, I just the add the CATEGORY_SOCIAL -label to get it to show under the social -tab (removing the INBOX -label will not show it at all). 然后,我只是添加CATEGORY_SOCIAL标签,使其显示在social -tab下(删除INBOX -label根本不会显示它)。

Request 请求

POST https://www.googleapis.com/gmail/v1/users/me/messages/150866b2f6956617/modify

{
 "addLabelIds": [
  "CATEGORY_SOCIAL"
 ]
}

Worked great! 很棒!

在此处输入图片说明

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

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