Search in sources :

Example 51 with Text

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

the class ReconstructMessageTest method testMessage.

@Test
public void testMessage() throws IOException, MessagingException {
    String messageSource = "From: from@example.com\r\n" + "To: to@example.com\r\n" + "Subject: Test Message \r\n" + "Date: Thu, 13 Nov 2014 17:09:38 +0100\r\n" + "Content-Type: multipart/mixed;\r\n" + " boundary=\"----Boundary\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "MIME-Version: 1.0\r\n" + "\r\n" + "This is a multipart MIME message.\r\n" + "------Boundary\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n" + "\r\n" + "------Boundary\r\n" + "Content-Type: text/plain\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "VGhpcyBpcyBhIHRl\r\n" + "c3QgbWVzc2FnZQ==\r\n" + "\r\n" + "------Boundary--\r\n" + "Hi, I'm the epilogue";
    BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
    InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
    MimeMessage message;
    try {
        message = MimeMessage.parseMimeMessage(messageInputStream, true);
    } finally {
        messageInputStream.close();
    }
    ByteArrayOutputStream messageOutputStream = new ByteArrayOutputStream();
    try {
        message.writeTo(messageOutputStream);
    } finally {
        messageOutputStream.close();
    }
    String reconstructedMessage = new String(messageOutputStream.toByteArray());
    assertEquals(messageSource, reconstructedMessage);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 52 with Text

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

the class MimeMessageHelper method setBody.

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 mimeType = multipart.getMimeType();
        String contentType = String.format("%s; boundary=\"%s\"", mimeType, 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) {
        String contentType;
        if (MimeUtility.mimeTypeMatches(part.getMimeType(), "text/*")) {
            contentType = String.format("%s;\r\n charset=utf-8", part.getMimeType());
            String name = MimeUtility.getHeaderParameter(part.getContentType(), "name");
            if (name != null) {
                contentType += String.format(";\r\n name=\"%s\"", name);
            }
        } else {
            contentType = part.getMimeType();
        }
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
        setEncoding(part, MimeUtil.ENC_QUOTED_PRINTABLE);
    } else if (body instanceof RawDataBody) {
        String encoding = ((RawDataBody) body).getEncoding();
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Message(com.fsck.k9.mail.Message)

Example 53 with Text

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

the class MessageExtractor method findAttachments.

/**
     * Traverse the MIME tree and add everything that's not a known text part to 'attachments'.
     *
     * @param multipart
     *         The {@link Multipart} to start from.
     * @param knownTextParts
     *         A set of known text parts we don't want to end up in 'attachments'.
     * @param attachments
     *         A list that will receive the parts that are considered attachments.
     */
private static void findAttachments(Multipart multipart, Set<Part> knownTextParts, @NonNull List<Part> attachments) {
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            findAttachments(innerMultipart, knownTextParts, attachments);
        } else if (!knownTextParts.contains(part)) {
            attachments.add(part);
        }
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) Body(com.fsck.k9.mail.Body)

Example 54 with Text

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

the class MimeBodyPart method getContentType.

@Override
public String getContentType() {
    String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
    if (contentType != null) {
        return MimeUtility.unfoldAndDecode(contentType);
    }
    Multipart parent = getParent();
    if (parent != null && "multipart/digest".equals(parent.getMimeType())) {
        return "message/rfc822";
    }
    return "text/plain";
}
Also used : Multipart(com.fsck.k9.mail.Multipart)

Example 55 with Text

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

the class MessageCompose method updateMessageFormat.

public void updateMessageFormat() {
    MessageFormat origMessageFormat = account.getMessageFormat();
    SimpleMessageFormat messageFormat;
    if (origMessageFormat == MessageFormat.TEXT) {
        // The user wants to send text/plain messages. We don't override that choice under
        // any circumstances.
        messageFormat = SimpleMessageFormat.TEXT;
    } else if (quotedMessagePresenter.isForcePlainText() && quotedMessagePresenter.includeQuotedText()) {
        // Right now we send a text/plain-only message when the quoted text was edited, no
        // matter what the user selected for the message format.
        messageFormat = SimpleMessageFormat.TEXT;
    } else if (recipientPresenter.isForceTextMessageFormat()) {
        // Right now we only support PGP inline which doesn't play well with HTML. So force
        // plain text in those cases.
        messageFormat = SimpleMessageFormat.TEXT;
    } else if (origMessageFormat == MessageFormat.AUTO) {
        if (action == Action.COMPOSE || quotedMessagePresenter.isQuotedTextText() || !quotedMessagePresenter.includeQuotedText()) {
            // If the message format is set to "AUTO" we use text/plain whenever possible. That
            // is, when composing new messages and replying to or forwarding text/plain
            // messages.
            messageFormat = SimpleMessageFormat.TEXT;
        } else {
            messageFormat = SimpleMessageFormat.HTML;
        }
    } else {
        // In all other cases use HTML
        messageFormat = SimpleMessageFormat.HTML;
    }
    setCurrentMessageFormat(messageFormat);
}
Also used : SimpleMessageFormat(com.fsck.k9.message.SimpleMessageFormat) MessageFormat(com.fsck.k9.Account.MessageFormat) SimpleMessageFormat(com.fsck.k9.message.SimpleMessageFormat)

Aggregations

Test (org.junit.Test)74 Part (com.fsck.k9.mail.Part)64 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)37 BodyPart (com.fsck.k9.mail.BodyPart)34 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)33 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)27 Message (com.fsck.k9.mail.Message)22 TextBody (com.fsck.k9.mail.internet.TextBody)18 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)16 ArrayList (java.util.ArrayList)16 Body (com.fsck.k9.mail.Body)13 MessagingException (com.fsck.k9.mail.MessagingException)13 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)13 Multipart (com.fsck.k9.mail.Multipart)10 Viewable (com.fsck.k9.mail.internet.Viewable)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)8 Intent (android.content.Intent)6 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6