简体   繁体   中英

Gmail API labels Android

I am using the Gmail API for Android for an application to send emails. 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 ?

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 ?

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):

// 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).

Request

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

{
 "addLabelIds": [
  "CATEGORY_SOCIAL"
 ]
}

Worked great!

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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