title: Unable to get email body containing text or html content using Gmail Java API?
body: <p>I am trying to fetch the emails body using Gmail Java APIs. I am getting all the fields including <code>to, from, subject</code>.</p>
  
  <p>But I am not able to get the textual body, also html body of email here is my code fragment : </p>
  
  <pre><code> List&lt;MessagePart&gt; parts =  message.getPayload().getParts();
              StringBuilder textSb = new StringBuilder();
              StringBuilder htmlSb = new StringBuilder();
              for (MessagePart part : parts) {
                  if (part.getMimeType().equalsIgnoreCase("text/plain")) {
                      try {
                          textSb.append(new String(Base64.getDecoder().decode(part.getBody().getData()), "UTF-8"));
                      } catch (UnsupportedEncodingException e) {
                          e.printStackTrace();
                      }
                  }
  
                  if (part.getMimeType().equalsIgnoreCase("text/html")) {
                      try {
                          htmlSb.append(new String(Base64.getDecoder().decode(part.getBody().getData()), "UTF-8"));
                      } catch (UnsupportedEncodingException e) {
                          e.printStackTrace();
                      }
                  }
              }
  </code></pre>
  
  <p>An short email body text-snippet can be obtained using below method ( But not full body ) :</p>
  
  <pre><code>message.getSnippet();
  </code></pre>
  
  <p>But thats not enough in my case I need whole body contents.</p>
  
  <p>While iterating over <code>MessageParts</code> in above loop in debugger I am getting two parts first with MIME type <code>multipart/alternative</code> and other of MIME type <code>application/pdf</code> which is for pdf file attachment.</p>
  
  <p>What should I do to get the body of email ?</p>