javagmail-api

Gmail API ? how to request for an email using Gmail api with format of raw and how to convert it into normal String


Can anybody help me out in this? I'm working with where I need to read a email. I have gone through the documentation of Gmail API for reading a messages, and I'm able to read email into RFC format. How do I convert it to normal Text?

If at all I need to use Java Libraries Mime classes for conversion, how do I use it specifically to read "From address", "To address" and "Message Body?"


Solution

  • Once you have a raw (RFC822) email string you can use the java MimeMessage library to parse it and then read the headers. I found a couple by searching for java MimeMessage example. You can use the MimeMessage(Session, InputStream) constructor, once you have a MimeMessage you can just call things like getFrom() or getHeader(String headerName) on it.

    While not exactly what you're trying to do, the Gmail API java guide for sending email is somewhat relevant in that it shows how to deal with MimeMessage libraries, etc:

    https://developers.google.com/gmail/api/guides/sending