private void multipartToContentValues(ContentValues cv, Multipart multipart) { cv.put("data_location", DataLocation.CHILD_PART_CONTAINS_DATA); cv.put("preamble", multipart.getPreamble()); cv.put("epilogue", multipart.getEpilogue()); cv.put("boundary", multipart.getBoundary()); }
public static void setBody(Part part, Body body) throws MessagingException { part.setBody(body); if (part instanceof Message) { part.setHeader("MIME-Version", "1.0"); } if (body instanceof Multipart) { Multipart multipart = ((Multipart) body); multipart.setParent(part); String contentType = Headers.contentTypeForMultipart(multipart.getMimeType(), multipart.getBoundary()); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); // note: if this is ever changed to 8bit, multipart/signed parts must always be 7bit! setEncoding(part, MimeUtil.ENC_7BIT); } else if (body instanceof TextBody) { MimeValue contentTypeHeader = MimeParameterDecoder.decode(part.getContentType()); String mimeType = contentTypeHeader.getValue(); if (MimeUtility.mimeTypeMatches(mimeType, "text/*")) { String name = contentTypeHeader.getParameters().get("name"); String contentType = Headers.contentType(mimeType, "utf-8", name); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); } else { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType); } setEncoding(part, MimeUtil.ENC_QUOTED_PRINTABLE); } else if (body instanceof RawDataBody) { String encoding = ((RawDataBody) body).getEncoding(); part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); } }
@Test public void migrateTextHtml() throws Exception { SQLiteDatabase db = createV50Database(); insertMultipartAlternativeMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("9"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(8, msg.getDatabaseId()); Assert.assertEquals(9, msg.getHeaderNames().size()); Assert.assertEquals("multipart/alternative", msg.getMimeType()); Assert.assertEquals(0, msg.getAttachmentCount()); Multipart msgBody = (Multipart) msg.getBody(); Assert.assertEquals("------------060200010509000000040004", msgBody.getBoundary()); }
@Test public void migrateHtmlWithRelatedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertHtmlWithRelatedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("10"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(9, msg.getDatabaseId()); Assert.assertEquals(11, msg.getHeaderNames().size()); Assert.assertEquals("multipart/mixed", msg.getMimeType()); Assert.assertEquals(1, msg.getAttachmentCount()); Multipart msgBody = (Multipart) msg.getBody(); Assert.assertEquals("------------050707070308090509030605", msgBody.getBoundary()); Multipart multipartAlternativePart = (Multipart) msgBody.getBodyPart(0).getBody(); BodyPart htmlPart = multipartAlternativePart.getBodyPart(1); String msgTextContent = MessageExtractor.getTextFromPart(htmlPart); Assert.assertNotNull(msgTextContent); Assert.assertTrue(msgTextContent.contains("cid:part1.07090108.09020601@example.org")); Assert.assertEquals("image/jpeg", msgBody.getBodyPart(1).getMimeType()); }
@Test public void migratePgpMimeEncryptedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertPgpMimeEncryptedMessage(db); db.close(); LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account); LocalMessage msg = localStore.getFolder("dev").getMessage("6"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(5, msg.getDatabaseId()); Assert.assertEquals(13, msg.getHeaderNames().size()); Assert.assertEquals("multipart/encrypted", msg.getMimeType()); Assert.assertEquals(2, msg.getAttachmentCount()); Multipart body = (Multipart) msg.getBody(); Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length); Assert.assertEquals("application/pgp-encrypted", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "protocol")); Assert.assertEquals("UoPmpPX/dBe4BELn", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary")); Assert.assertEquals("UoPmpPX/dBe4BELn", body.getBoundary()); Assert.assertEquals(2, body.getCount()); Assert.assertEquals("application/pgp-encrypted", body.getBodyPart(0).getMimeType()); Assert.assertEquals("application/octet-stream", body.getBodyPart(1).getMimeType()); }