question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I am having trouble retrieving a decoded version of the message when I call GetMimeMessage()

See original GitHub issue

I asked this question on stack overflow a few times (here) but have never been able to get a response. So I apologize if this is not the best place to be posting this but I really don’t know who tot turn to anymore. Anyways here is my problemL

In my script I need to extract a set of emails that match some query. I decided to use GMail’s API python client for this. Now, my understanding was that the GetMimeMessage() was supposed to return a set of decoded base 64 messages. Here is my code:

def GmailInput():

    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    defaultList= ListMessagesMatchingQuery(service, 'me', 'subject:infringement label:unread ')
    print(defaultList)
    for msg in defaultList:
        currentMSG=GetMimeMessage(service, 'me', msg['id'])
        ....then I parse the text of the emails and extract some things

The problem is, I am unable to actually parse the message body because GetMimeMessage is not returning a base64 decoded message. So what I am actually parsing ends up being completely unreadable by humans.

I find this peculiar because GetMimeMessage (copied below for convenience) literally does a url-safe base 64 decode of the message data. Anyone have any suggestion? Im really stumped on this.

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'])

    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)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
theacodescommented, Jul 1, 2016

Great, thanks for posting your solution. This can hopefully help any other users who run into the same issue.

0reactions
keepfocusedcommented, Dec 31, 2017

Here is the solution: Gmail API - “Users.messages: get” method has in response message.payload.body.data parted base64 data, it’s separated by “-” symbol. It’s not entire base64 encoded text, it’s parts of base64 text. You have to try to decode every single part of this or make one mono string by unite and replace “-” symbol.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MimeMessage.getAllHeaders() returns two different classes in ...
Header class. So when you call MimeMessage.getAllHeaders(), it is only safe to assume that the Enumeration will return instances of javax.mail.
Read more >
MimeMessage (JavaMail API documentation) - Java EE
This class represents a MIME style email message. It implements the Message abstract class and the MimePart interface. Clients wanting to create new...
Read more >
Get MIME content of a message using the Outlook mail API
In this article. What is MIME? Get MIME content of an Outlook message; Get MIME content of an Outlook message attached to an...
Read more >
Jakarta Mail
Use the getContent method to obtain the Multipart object. This code sample below shows the retrieval of a Multipart object.
Read more >
Features - Simple Java Mail
Getting the generated email id after sending back. Sometimes you need the actual ID used in the MimeMessage that went out to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found