Search in sources :

Example 41 with Multipart

use of com.fsck.k9.mail.Multipart 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 42 with Multipart

use of com.fsck.k9.mail.Multipart 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 43 with Multipart

use of com.fsck.k9.mail.Multipart 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 44 with Multipart

use of com.fsck.k9.mail.Multipart 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 45 with Multipart

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

the class MessageCryptoHelper method getDataSourceForSignedData.

private OpenPgpDataSource getDataSourceForSignedData(final Part signedPart) throws IOException {
    return new OpenPgpDataSource() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try {
                Multipart multipartSignedMultipart = (Multipart) signedPart.getBody();
                BodyPart signatureBodyPart = multipartSignedMultipart.getBodyPart(0);
                Timber.d("signed data type: %s", signatureBodyPart.getMimeType());
                signatureBodyPart.writeTo(os);
            } catch (MessagingException e) {
                Timber.e(e, "Exception while writing message to crypto provider");
            }
        }
    };
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)

Aggregations

BodyPart (com.fsck.k9.mail.BodyPart)57 Part (com.fsck.k9.mail.Part)51 Test (org.junit.Test)47 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)38 Multipart (com.fsck.k9.mail.Multipart)33 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)29 Message (com.fsck.k9.mail.Message)25 Body (com.fsck.k9.mail.Body)22 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)21 ArrayList (java.util.ArrayList)14 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)13 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)13 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 TextBody (com.fsck.k9.mail.internet.TextBody)7 MessagingException (com.fsck.k9.mail.MessagingException)6 Stack (java.util.Stack)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 FetchProfile (com.fsck.k9.mail.FetchProfile)5 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)5