diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index fab84574..c04ea857 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -675,9 +675,9 @@ public class ImapConnection extends AbstractConnection { } } - InputStream partInputStream; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - OutputStream partOutputStream; + InputStream partInputStream = null; + OutputStream partOutputStream = null; // load message MimeMessage mimeMessage = message.getMimeMessage(); @@ -692,9 +692,20 @@ public class ImapConnection extends AbstractConnection { partOutputStream = new PartialOutputStream(baos, startIndex, maxSize); partInputStream = mimeMessage.getRawInputStream(); } else if ("RFC822.HEADER".equals(param) || partIndexString.startsWith("HEADER")) { - // write headers only - partOutputStream = new PartOutputStream(baos, true, false, startIndex, maxSize); - partInputStream = message.getRawInputStream(); + // Header requested fetch headers + String[] requestedHeaders = getRequestedHeaders(partIndexString); + if (requestedHeaders != null) { + Enumeration headerEnumeration = message.getMimeMessage().getMatchingHeaderLines(requestedHeaders); + while (headerEnumeration.hasMoreElements()) { + baos.write(((String) headerEnumeration.nextElement()).getBytes("UTF-8")); + baos.write(13); + baos.write(10); + } + } else { + // write headers only + partOutputStream = new PartOutputStream(baos, true, false, startIndex, maxSize); + partInputStream = message.getRawInputStream(); + } } else { MimePart bodyPart = mimeMessage; String[] partIndexStrings = partIndexString.split("\\."); @@ -734,15 +745,15 @@ public class ImapConnection extends AbstractConnection { } // copy selected content to baos - IOUtil.write(partInputStream, partOutputStream); - partInputStream.close(); - partOutputStream.close(); + if (partInputStream != null && partOutputStream != null) { + IOUtil.write(partInputStream, partOutputStream); + partInputStream.close(); + partOutputStream.close(); + } baos.close(); if ("RFC822.HEADER".equals(param)) { buffer.append(" RFC822.HEADER "); - } else if (partIndexString.startsWith("HEADER.FIELDS")) { - buffer.append(" BODY[HEADER.FIELDS ()]"); } else { buffer.append(" BODY[").append(partIndexString).append(']'); } @@ -764,6 +775,16 @@ public class ImapConnection extends AbstractConnection { message.dropMimeMessage(); } + protected String[] getRequestedHeaders(String partIndexString) { + int startIndex = partIndexString.indexOf('('); + int endIndex = partIndexString.indexOf(')'); + if (startIndex >= 0 && endIndex >= 0) { + return partIndexString.substring(startIndex + 1, endIndex - 1).split(" "); + } else { + return null; + } + } + protected void handleStore(String commandId, AbstractRangeIterator rangeIterator, String action, String flags) throws IOException { while (rangeIterator.hasNext()) { DavGatewayTray.switchIcon(); diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index d1a4c0c9..01bbf78b 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -283,6 +283,11 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } + public void testHeaderFetch() throws IOException { + writeLine(". UID FETCH " + messageUid + " (BODY[HEADER.FIELDS (DATE SUBJECT FROM CONTENT-TYPE TO CC BCC MESSAGE-ID IN-REPLY-TO REFERENCES)])"); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); + } + public void testFetchInternalDate() throws IOException { writeLine(". UID FETCH " + messageUid + " (INTERNALDATE)"); assertEquals(". OK UID FETCH completed", readFullAnswer("."));