Harry Mad
Harry Mad

Reputation: 498

Can I open my email thread in Gmail App If I have id or threadId?

I am using gmail APIs in my android app and I am able to get id and threadId.So is there any way I can populate gmail using these ids.

Upvotes: 3

Views: 2451

Answers (2)

rusty
rusty

Reputation: 712

You can get the raw message in following way This is in python but in Java it would be similar. I hope I answered what you are looking for.

def GetMimeMessage(service, user_id, msg_id):   
"""Get a Message and use it to create a MIME Message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    msg_id: The ID of the Message required.

  Returns:
    A MIME Message, consisting of data from Message.   """   
    try:
        message = service.users().messages().get(userId=user_id, id=msg_id, format = 'raw').execute()

        print 'Message snippet: %s' % message['snippet']
        print 'keys ', message.keys()
        msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))

        mime_msg = email.message_from_string(msg_str)

        return mime_msg   
    except errors.HttpError, error:
        print 'An error occurred: %s' % error

for i in range(0,len(th['messages'])):

    print 'snippent --- ', th['messages'][i]['snippet']
    id = th['messages'][i]['id']
    print 'id---', id
    msg = GetMimeMessage(service, 'me', id )
    pay = msg.get_payload()
    pay1 = pay[0]
    print 'msg --- ', pay1.get_payload()

Upvotes: 1

Tholle
Tholle

Reputation: 112807

Once you have the messageId representing the message you want, you can simply use the Users.messages: get-operation to get the email. You could either do the request manually

GET https://www.googleapis.com/gmail/v1/users/me/messages/<MESSAGE_ID>

or with the help of a library:

Message message = service.users().messages().get('me', messageId).execute();

If you want to get all the mails in the thread, it is just as easy to do that also.

Look at code examples and explore the API in the links I provided.

Upvotes: 1

Related Questions