@Override public void setEncoding(String encoding) throws MessagingException { if (mBody != null) { mBody.setEncoding(encoding); } setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); }
private void moveSubjectIntoEncryptedPayload() { String[] subjects = currentProcessedMimeMessage.getHeader(MimeHeader.SUBJECT); if (subjects.length > 0) { messageContentBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, messageContentBodyPart.getContentType() + "; protected-headers=\"v1\""); messageContentBodyPart.setHeader(MimeHeader.SUBJECT, subjects[0]); currentProcessedMimeMessage.setHeader(MimeHeader.SUBJECT, resourceProvider.encryptedSubject()); } }
private MimeBodyPart createBodyPartFromMessageContent() throws MessagingException { MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart(); String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE); if (contentType.length > 0) { bodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType[0]); } if (isDraft()) { String[] identityHeader = currentProcessedMimeMessage.getHeader(K9.IDENTITY_HEADER); bodyPart.setHeader(K9.IDENTITY_HEADER, identityHeader[0]); currentProcessedMimeMessage.removeHeader(K9.IDENTITY_HEADER); } return bodyPart; }
public static MimeBodyPart multipart(String type, String typeParameters, BodyPart... subParts) throws MessagingException { MimeMultipart multiPart = MimeMultipart.newInstance(); multiPart.setSubType(type); for (BodyPart subPart : subParts) { multiPart.addBodyPart(subPart); } MimeBodyPart mimeBodyPart = new MimeBodyPart(multiPart); if (typeParameters != null) { mimeBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeBodyPart.getContentType() + "; " + typeParameters); } return mimeBodyPart; }
@Test public void getTextFromPart_withExceptionThrownGettingInputStream_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); Body body = mock(Body.class); when(body.getInputStream()).thenThrow(new MessagingException("Test")); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@Test public void getTextFromPart_withUnknownEncoding_shouldReturnUnmodifiedBodyContents() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); String bodyText = "Sample text body"; BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), "unknown encoding"); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals(bodyText, result); }
@Test public void getTextFromPart_withTextBody_shouldReturnText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; charset=utf-8"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("Sample text body", result); }
@Test public void getTextFromPart_withPlainTextWithCharsetInContentTypeRawDataBody_shouldReturnText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; charset=UTF-8"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("Sample text body", result); }
@Test public void getTextFromPart_withRawDataBodyWithNonText_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpeg"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@Test public void getTextFromPart_withHtmlWithCharsetInContentTypeRawDataBody_shouldReturnHtmlText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=UTF-8"); BinaryMemoryBody body = new BinaryMemoryBody( "<html><body>Sample text body</body></html>".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("<html><body>Sample text body</body></html>", result); }
@Test public void extractInfoForDb__withDispositionSizeParam__shouldReturnThatSize() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; size=\"" + TEST_SIZE + "\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(TEST_SIZE, attachmentViewInfo.size); }
@Test public void extractInfoForDb__withDispositionInvalidSizeParam__shouldReturnUnknownSize() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; size=\"notanint\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(AttachmentViewInfo.UNKNOWN_SIZE, attachmentViewInfo.size); }
@Test public void extractMessage_openPgpEncrypted_withProtectedSubject() throws Exception { MimeBodyPart encryptedPayload = bodypart("text/plain", "encrypted text"); Message message = messageFromBody(multipart("encrypted", "protocol=\"application/pgp-encrypted\"", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") )); encryptedPayload.setHeader("Content-Type", encryptedPayload.getHeader("Content-Type")[0] + "; protected-headers=v1"); encryptedPayload.setHeader("Subject", PROTECTED_SUBJECT); MessageCryptoAnnotations cryptoAnnotations = new MessageCryptoAnnotations(); OpenPgpDecryptionResult decryptionResult = new OpenPgpDecryptionResult(OpenPgpDecryptionResult.RESULT_ENCRYPTED); CryptoResultAnnotation openPgpResultAnnotation = CryptoResultAnnotation.createOpenPgpResultAnnotation( decryptionResult, null, null, null, encryptedPayload, false); cryptoAnnotations.put(message, openPgpResultAnnotation); MessageViewInfo messageViewInfo = messageViewInfoExtractor.extractMessageForView(message, cryptoAnnotations, true); assertSame(openPgpResultAnnotation, messageViewInfo.cryptoResultAnnotation); assertEquals("<pre class=\"k9mail\">encrypted text</pre>", messageViewInfo.text); assertEquals(PROTECTED_SUBJECT, messageViewInfo.subject); assertTrue(messageViewInfo.attachments.isEmpty()); assertTrue(messageViewInfo.extraAttachments.isEmpty()); }
@Test public void getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText() throws Exception { String bodyText = "<html><head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "</head><body>Sample text body</body></html>"; BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), MimeUtil.ENC_8BIT); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNotNull(result); assertEquals(bodyText, result); } }
@Test public void extractInfoForDb__withTextMimeType__shouldReturnTxtExtension() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); // MimeUtility.getExtensionByMimeType("text/plain"); -> "txt" assertEquals("noname.txt", attachmentViewInfo.displayName); assertEquals("text/plain", attachmentViewInfo.mimeType); }
@Test public void extractInfoForDb__withDispositionAttach__shouldReturnNamedAttachment() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; filename=\"filename.ext\"; meaningless=\"dummy\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(Uri.EMPTY, attachmentViewInfo.internalUri); assertEquals("filename.ext", attachmentViewInfo.displayName); assertFalse(attachmentViewInfo.inlineAttachment); }
@Test public void extractInfoForDb__withContentTypeAndName__shouldReturnNamedAttachment() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE + "; name=\"filename.ext\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(Uri.EMPTY, attachmentViewInfo.internalUri); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertEquals("filename.ext", attachmentViewInfo.displayName); assertFalse(attachmentViewInfo.inlineAttachment); }
@Test public void extractInfo__withDeferredFileBody() throws Exception { attachmentInfoExtractor = new AttachmentInfoExtractor(context) { @Nullable @Override protected Uri getDecryptedFileProviderUri(DeferredFileBody decryptedTempFileBody, String mimeType) { return TEST_URI; } }; DeferredFileBody body = mock(DeferredFileBody.class); when(body.getSize()).thenReturn(TEST_SIZE); MimeBodyPart part = new MimeBodyPart(); part.setBody(body); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfo(part); assertEquals(TEST_URI, attachmentViewInfo.internalUri); assertEquals(TEST_SIZE, attachmentViewInfo.size); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertFalse(attachmentViewInfo.inlineAttachment); assertTrue(attachmentViewInfo.isContentAvailable()); } }
MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain"); MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822"); bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\""); multipart.addBodyPart(bodyPart1); multipart.addBodyPart(bodyPart2);
private static MimeBodyPart createPart(Body body, final String filename, final String contentType) throws MessagingException { MimeBodyPart part = new MimeBodyPart(body, contentType); String contentTypeHeader = TextUtils.isEmpty(contentType) ? "application/octet-stream" : contentType; String disposition = "attachment"; if (!TextUtils.isEmpty(filename)) { // should set both name and filename parameters // http://www.imc.org/ietf-smtp/mail-archive/msg05023.html disposition += encodeRFC2231("filename", filename); contentTypeHeader += encodeRFC2231("name", filename); } part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentTypeHeader); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, disposition); part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); return part; }