public static BodyPart bodypart(String type, Body body) throws MessagingException { return new MimeBodyPart(body, type); } }
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; }
private void mimeBuildEncryptedMessage(@NonNull Body encryptedBodyPart) throws MessagingException { MimeMultipart multipartEncrypted = createMimeMultipart(); multipartEncrypted.setSubType("encrypted"); multipartEncrypted.addBodyPart(new MimeBodyPart(new TextBody("Version: 1"), "application/pgp-encrypted")); MimeBodyPart encryptedPart = new MimeBodyPart(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\""); encryptedPart.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"encrypted.asc\""); multipartEncrypted.addBodyPart(encryptedPart); MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartEncrypted); String contentType = String.format( "multipart/encrypted; boundary=\"%s\";\r\n protocol=\"application/pgp-encrypted\"", multipartEncrypted.getBoundary()); currentProcessedMimeMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); }
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 void addContentType(MimeBodyPart bodyPart, String contentType, String name) throws MessagingException { String value = Headers.contentType(contentType, name); bodyPart.addHeader(MimeHeader.HEADER_CONTENT_TYPE, value); if (!MimeUtil.isMessage(contentType)) { bodyPart.setEncoding(MimeUtility.getEncodingforType(contentType)); } }
@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 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 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()); } }
@Test public void extractInfoForDb__withNoBody__shouldReturnContentAvailable() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setBody(new TextBody("data")); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertTrue(attachmentViewInfo.isContentAvailable()); }
@Override public void setEncoding(String encoding) throws MessagingException { if (mBody != null) { mBody.setEncoding(encoding); } setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); }
@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()); }
@Override public String getMimeType() { return MimeUtility.getHeaderParameter(getContentType(), null); }
private void addContentDisposition(MimeBodyPart bodyPart, String fileName, Long size) { String value = Headers.contentDisposition("attachment", fileName, size); bodyPart.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, value); }
public boolean hasAutocryptGossipHeader(MimeBodyPart part) { return part.getHeader(AutocryptGossipHeader.AUTOCRYPT_GOSSIP_HEADER).length > 0; }
@Test public void getTextFromPart_withNoBody_shouldReturnNull() throws Exception { part.setBody(null); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@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 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); }
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 MimeBodyPart(Body body, String contentType) throws MessagingException { mHeader = new MimeHeader(); if (contentType != null) { addHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); } MimeMessageHelper.setBody(this, body); }
public static BodyPart bodypart(String type) throws MessagingException { return new MimeBodyPart(null, type); }