Search in sources :

Example 21 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project k-9 by k9mail.

the class MessageBuilder method buildBody.

private void buildBody(MimeMessage message) throws MessagingException {
    // Build the body.
    // TODO FIXME - body can be either an HTML or Text part, depending on whether we're in
    // HTML mode or not.  Should probably fix this so we don't mix up html and text parts.
    TextBody body = buildText(isDraft);
    // text/plain part when messageFormat == MessageFormat.HTML
    TextBody bodyPlain = null;
    final boolean hasAttachments = !attachments.isEmpty();
    if (messageFormat == SimpleMessageFormat.HTML) {
        // HTML message (with alternative text part)
        // This is the compiled MIME part for an HTML message.
        MimeMultipart composedMimeMessage = createMimeMultipart();
        composedMimeMessage.setSubType("alternative");
        // Let the receiver select either the text or the HTML part.
        bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT);
        composedMimeMessage.addBodyPart(MimeBodyPart.create(bodyPlain, "text/plain"));
        MimeBodyPart htmlPart = MimeBodyPart.create(body, "text/html");
        if (inlineAttachments != null && inlineAttachments.size() > 0) {
            MimeMultipart htmlPartWithInlineImages = new MimeMultipart("multipart/related", boundaryGenerator.generateBoundary());
            htmlPartWithInlineImages.addBodyPart(htmlPart);
            addInlineAttachmentsToMessage(htmlPartWithInlineImages);
            composedMimeMessage.addBodyPart(MimeBodyPart.create(htmlPartWithInlineImages));
        } else {
            composedMimeMessage.addBodyPart(htmlPart);
        }
        if (hasAttachments) {
            // If we're HTML and have attachments, we have a MimeMultipart container to hold the
            // whole message (mp here), of which one part is a MimeMultipart container
            // (composedMimeMessage) with the user's composed messages, and subsequent parts for
            // the attachments.
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(composedMimeMessage));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // If no attachments, our multipart/alternative part is the only one we need.
            MimeMessageHelper.setBody(message, composedMimeMessage);
        }
    } else if (messageFormat == SimpleMessageFormat.TEXT) {
        // Text-only message.
        if (hasAttachments) {
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(body, "text/plain"));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // No attachments to include, just stick the text body in the message and call it good.
            MimeMessageHelper.setBody(message, body);
        }
    }
    // If this is a draft, add metadata for thawing.
    if (isDraft) {
        // Add the identity to the message.
        message.addHeader(K9.IDENTITY_HEADER, buildIdentityHeader(body, bodyPlain));
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 22 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartPlainTextMessage.

@Test
public void testMultipartPlainTextMessage() throws MessagingException {
    String bodyText1 = "text body 1";
    String bodyText2 = "text body 2";
    // Create text/plain bodies
    TextBody body1 = new TextBody(bodyText1);
    TextBody body2 = new TextBody(bodyText2);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(body1, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(body2, "text/plain");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = bodyText1 + "\r\n\r\n" + "------------------------------------------------------------------------\r\n\r\n" + bodyText2;
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + bodyText1 + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; " + "border-bottom: 1px solid #000\"></p>" + "<pre dir=\"auto\" class=\"k9mail\">" + bodyText2 + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 23 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testTextPlusRfc822Message.

@Test
public void testTextPlusRfc822Message() throws MessagingException {
    setLanguage("en");
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));
    String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment.";
    // Create text/plain body
    TextBody textBody = new TextBody(BODY_TEXT);
    // Create inner text/plain body
    TextBody innerBody = new TextBody(innerBodyText);
    // Create message/rfc822 body
    MimeMessage innerMessage = new MimeMessage();
    innerMessage.addSentDate(new Date(112, 2, 17), false);
    innerMessage.setHeader("To", "to@example.com");
    innerMessage.setSubject("Subject");
    innerMessage.setFrom(new Address("from@example.com"));
    MimeMessageHelper.setBody(innerMessage, innerBody);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    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);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = BODY_TEXT + "\r\n\r\n" + "----- message.eml ------------------------------------------------------" + "\r\n\r\n" + "From: from@example.com" + "\r\n" + "To: to@example.com" + "\r\n" + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012" + "\r\n" + "Subject: Subject" + "\r\n" + "\r\n" + innerBodyText;
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + BODY_TEXT_HTML + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: " + "1px solid #000\">message.eml</p>" + "<table style=\"border: 0\">" + "<tr>" + "<th style=\"text-align: left; vertical-align: top;\">From:</th>" + "<td>from@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">To:</th>" + "<td>to@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>" + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>" + "<td>Subject</td>" + "</tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">" + innerBodyText + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : Address(com.fsck.k9.mail.Address) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 24 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project k-9 by k9mail.

the class MessageBuilderTest method build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder.

@Test
public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() {
    MessageBuilder messageBuilder = createHtmlMessageBuilder();
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MimeMultipart.class, message.getBody().getClass());
    assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType());
    List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts();
    // RFC 2046 - 5.1.4. - Best type is last displayable
    assertEquals("text/plain", parts.get(0).getMimeType());
    assertEquals("text/html", parts.get(1).getMimeType());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 25 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project k-9 by k9mail.

the class LocalStoreTest method findPartById__withNestedLocalBodyPart.

@Test
public void findPartById__withNestedLocalBodyPart() throws Exception {
    LocalBodyPart searchRoot = new LocalBodyPart(null, null, 1L, -1L);
    LocalBodyPart needlePart = new LocalBodyPart(null, null, 123L, -1L);
    MimeMultipart mimeMultipart = new MimeMultipart("boundary");
    mimeMultipart.addBodyPart(needlePart);
    searchRoot.setBody(mimeMultipart);
    Part part = LocalStore.findPartById(searchRoot, 123L);
    assertSame(needlePart, part);
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)23 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)15 BodyPart (com.fsck.k9.mail.BodyPart)11 Test (org.junit.Test)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)10 Part (com.fsck.k9.mail.Part)8 TextBody (com.fsck.k9.mail.internet.TextBody)8 Body (com.fsck.k9.mail.Body)4 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)2 RobolectricTest (com.fsck.k9.RobolectricTest)2 Message (com.fsck.k9.mail.Message)2 MessagingException (com.fsck.k9.mail.MessagingException)2 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 Viewable (com.fsck.k9.mail.internet.Viewable)2 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)2 TempFileBody (com.fsck.k9.mailstore.TempFileBody)2 Callback (com.fsck.k9.message.MessageBuilder.Callback)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2