title: How to use Apache Commons Net to fetch an email via IMAP?
body: <p>I've successfully connected and logged in to the IMAP server and can use the IMAP fetch command to retrieve a list of emails. However, when I try to retrieve a single email with full headers I end up having a problem.</p>
  
  <p>This is the stack trace I'm getting:</p>
  
  <pre><code>Exception in thread "main" org.apache.commons.net.MalformedServerReplyException: Received unexpected IMAP protocol response from server: 'Return-Path: &lt;[removed]&gt;'.
  at org.apache.commons.net.imap.IMAPReply.getReplyCode(IMAPReply.java:128)
  at org.apache.commons.net.imap.IMAPReply.getReplyCode(IMAPReply.java:93)
  at org.apache.commons.net.imap.IMAP.__getReply(IMAP.java:120)
  at org.apache.commons.net.imap.IMAP.__getReply(IMAP.java:90)
  at org.apache.commons.net.imap.IMAP.sendCommandWithID(IMAP.java:225)
  at org.apache.commons.net.imap.IMAP.sendCommand(IMAP.java:238)
  at org.apache.commons.net.imap.IMAP.sendCommand(IMAP.java:263)
  at org.apache.commons.net.imap.IMAP.doCommand(IMAP.java:276)
  at org.apache.commons.net.imap.IMAPClient.fetch(IMAPClient.java:339)
  </code></pre>
  
  <p>My code that generates the above stack trace is:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      IMAPClient client = new IMAPClient();
      client.connect(SERVER);
      client.login(USERNAME, PASSWORD);
      client.select("INBOX");
      client.fetch("1", "body[header]");
  }
  </code></pre>
  
  <p>I'm using commons-net-3.1</p>
  
  <p>When I connect to the IMAP server via telnet and issue the same command I end up with a result like this (I've elided some of the data with underscores):</p>
  
  <pre><code>08 fetch 1 body[header]
  * 1 FETCH (BODY[HEADER] {608}
  Return-Path: &lt;____@____.com&gt;
  X-Original-To: ____@____.com
  Delivered-To: ____@____.com
  Received: from ____.____.com (unknown [____])
      by ____.____.____ (Postfix) with ESMTP id 1926514D20
      for &lt;____@____.com&gt;; Mon,  1 Feb 2010 16:52:36 +1100 (EST)
  Date: Mon, 1 Feb 2010 16:52:35 +1100 (EST)
  From: ____@____.com
  To: ____@____.com
  Message-ID: &lt;__________________________@_____&gt;
  Subject: ____
  MIME-Version: 1.0
  Content-Type: multipart/mixed; 
      boundary="----=_Part_13575_1460176524.1265003555921"
  
  )
  * 79690 EXISTS
  * 13 RECENT
  08 OK Fetch completed.
  </code></pre>
  
  <p>Am I using the correct method (fetch) to view the email?
  Any clues on where I'm going wrong?</p>