Search in sources :

Example 1 with MimeMultipart

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

the class MessageDecryptVerifierTest method findEncryptedPartsShouldReturnMultipleEncryptedParts.

@Test
public void findEncryptedPartsShouldReturnMultipleEncryptedParts() throws Exception {
    MimeMessage message = new MimeMessage();
    MimeMultipart multipartMixed = MimeMultipart.newInstance();
    multipartMixed.setSubType("mixed");
    MimeMessageHelper.setBody(message, multipartMixed);
    MimeMultipart multipartEncryptedOne = MimeMultipart.newInstance();
    multipartEncryptedOne.setSubType("encrypted");
    MimeBodyPart bodyPartOne = new MimeBodyPart(multipartEncryptedOne);
    setContentTypeWithProtocol(bodyPartOne, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED);
    multipartMixed.addBodyPart(bodyPartOne);
    MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain");
    multipartMixed.addBodyPart(bodyPartTwo);
    MimeMultipart multipartEncryptedThree = MimeMultipart.newInstance();
    multipartEncryptedThree.setSubType("encrypted");
    MimeBodyPart bodyPartThree = new MimeBodyPart(multipartEncryptedThree);
    setContentTypeWithProtocol(bodyPartThree, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED);
    multipartMixed.addBodyPart(bodyPartThree);
    List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
    assertEquals(2, encryptedParts.size());
    assertSame(bodyPartOne, encryptedParts.get(0));
    assertSame(bodyPartThree, encryptedParts.get(1));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 2 with MimeMultipart

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

the class MessageDecryptVerifierTest method multipart.

MimeBodyPart multipart(String type, BodyPart... subParts) throws MessagingException {
    MimeMultipart multiPart = MimeMultipart.newInstance();
    multiPart.setSubType(type);
    for (BodyPart subPart : subParts) {
        multiPart.addBodyPart(subPart);
    }
    return new MimeBodyPart(multiPart);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 3 with MimeMultipart

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

the class ImapFolder method parseBodyStructure.

private void parseBodyStructure(ImapList bs, Part part, String id) throws MessagingException {
    if (bs.get(0) instanceof ImapList) {
        /*
             * This is a multipart/*
             */
        MimeMultipart mp = MimeMultipart.newInstance();
        for (int i = 0, count = bs.size(); i < count; i++) {
            if (bs.get(i) instanceof ImapList) {
                /*
                     * For each part in the message we're going to add a new BodyPart and parse
                     * into it.
                     */
                MimeBodyPart bp = new MimeBodyPart();
                if (id.equalsIgnoreCase("TEXT")) {
                    parseBodyStructure(bs.getList(i), bp, Integer.toString(i + 1));
                } else {
                    parseBodyStructure(bs.getList(i), bp, id + "." + (i + 1));
                }
                mp.addBodyPart(bp);
            } else {
                /*
                     * We've got to the end of the children of the part, so now we can find out
                     * what type it is and bail out.
                     */
                String subType = bs.getString(i);
                mp.setSubType(subType.toLowerCase(Locale.US));
                break;
            }
        }
        MimeMessageHelper.setBody(part, mp);
    } else {
        /*
             * This is a body. We need to add as much information as we can find out about
             * it to the Part.
             */
        /*
             *  0| 0  body type
             *  1| 1  body subtype
             *  2| 2  body parameter parenthesized list
             *  3| 3  body id (unused)
             *  4| 4  body description (unused)
             *  5| 5  body encoding
             *  6| 6  body size
             *  -| 7  text lines (only for type TEXT, unused)
             * Extensions (optional):
             *  7| 8  body MD5 (unused)
             *  8| 9  body disposition
             *  9|10  body language (unused)
             * 10|11  body location (unused)
             */
        String type = bs.getString(0);
        String subType = bs.getString(1);
        String mimeType = (type + "/" + subType).toLowerCase(Locale.US);
        ImapList bodyParams = null;
        if (bs.get(2) instanceof ImapList) {
            bodyParams = bs.getList(2);
        }
        String encoding = bs.getString(5);
        int size = bs.getNumber(6);
        if (MimeUtility.isMessage(mimeType)) {
            /*
                 * This will be caught by fetch and handled appropriately.
                 */
            throw new MessagingException("BODYSTRUCTURE message/rfc822 not yet supported.");
        }
        /*
             * Set the content type with as much information as we know right now.
             */
        StringBuilder contentType = new StringBuilder();
        contentType.append(mimeType);
        if (bodyParams != null) {
            /*
                 * If there are body params we might be able to get some more information out
                 * of them.
                 */
            for (int i = 0, count = bodyParams.size(); i < count; i += 2) {
                String paramName = bodyParams.getString(i);
                String paramValue = bodyParams.getString(i + 1);
                contentType.append(String.format(";\r\n %s=\"%s\"", paramName, paramValue));
            }
        }
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType.toString());
        // Extension items
        ImapList bodyDisposition = null;
        if ("text".equalsIgnoreCase(type) && bs.size() > 9 && bs.get(9) instanceof ImapList) {
            bodyDisposition = bs.getList(9);
        } else if (!("text".equalsIgnoreCase(type)) && bs.size() > 8 && bs.get(8) instanceof ImapList) {
            bodyDisposition = bs.getList(8);
        }
        StringBuilder contentDisposition = new StringBuilder();
        if (bodyDisposition != null && !bodyDisposition.isEmpty()) {
            if (!"NIL".equalsIgnoreCase(bodyDisposition.getString(0))) {
                contentDisposition.append(bodyDisposition.getString(0).toLowerCase(Locale.US));
            }
            if (bodyDisposition.size() > 1 && bodyDisposition.get(1) instanceof ImapList) {
                ImapList bodyDispositionParams = bodyDisposition.getList(1);
                /*
                     * If there is body disposition information we can pull some more information
                     * about the attachment out.
                     */
                for (int i = 0, count = bodyDispositionParams.size(); i < count; i += 2) {
                    String paramName = bodyDispositionParams.getString(i).toLowerCase(Locale.US);
                    String paramValue = bodyDispositionParams.getString(i + 1);
                    contentDisposition.append(String.format(";\r\n %s=\"%s\"", paramName, paramValue));
                }
            }
        }
        if (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size") == null) {
            contentDisposition.append(String.format(Locale.US, ";\r\n size=%d", size));
        }
        /*
             * Set the content disposition containing at least the size. Attachment
             * handling code will use this down the road.
             */
        part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition.toString());
        /*
             * Set the Content-Transfer-Encoding header. Attachment code will use this
             * to parse the body.
             */
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
        if (part instanceof ImapMessage) {
            ((ImapMessage) part).setSize(size);
        }
        part.setServerExtra(id);
    }
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 4 with MimeMultipart

use of com.fsck.k9.mail.internet.MimeMultipart in project sms-backup-plus by jberkel.

the class MessageGenerator method messageFromMapMms.

@Nullable
private Message messageFromMapMms(Map<String, String> msgMap) throws MessagingException {
    if (LOCAL_LOGV)
        Log.v(TAG, "messageFromMapMms(" + msgMap + ")");
    final Uri mmsUri = Uri.withAppendedPath(Consts.MMS_PROVIDER, msgMap.get(Telephony.BaseMmsColumns._ID));
    MmsSupport.MmsDetails details = mmsSupport.getDetails(mmsUri, addressStyle);
    if (details.isEmpty()) {
        Log.w(TAG, "no recipients found");
        return null;
    } else if (!includeInBackup(DataType.MMS, details.records)) {
        Log.w(TAG, "no recipients included");
        return null;
    }
    final Message msg = new MimeMessage();
    msg.setSubject(getSubject(DataType.MMS, details.getRecipient()));
    if (details.inbound) {
        // msg_box == MmsConsts.MESSAGE_BOX_INBOX does not work
        msg.setFrom(details.getRecipientAddress());
        msg.setRecipient(Message.RecipientType.TO, userAddress);
    } else {
        msg.setRecipients(Message.RecipientType.TO, details.getAddresses());
        msg.setFrom(userAddress);
    }
    Date sentDate;
    try {
        sentDate = new Date(1000 * Long.valueOf(msgMap.get(Telephony.BaseMmsColumns.DATE)));
    } catch (NumberFormatException n) {
        Log.e(TAG, ERROR_PARSING_DATE, n);
        sentDate = new Date();
    }
    final int msg_box = toInt(msgMap.get("msg_box"));
    headerGenerator.setHeaders(msg, msgMap, DataType.MMS, details.address, details.getRecipient(), sentDate, msg_box);
    MimeMultipart body = MimeMultipart.newInstance();
    for (BodyPart p : mmsSupport.getMMSBodyParts(Uri.withAppendedPath(mmsUri, MMS_PART))) {
        body.addBodyPart(p);
    }
    setBody(msg, body);
    return msg;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Uri(android.net.Uri) Date(java.util.Date) Nullable(android.support.annotation.Nullable)

Example 5 with MimeMultipart

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

the class PgpMessageBuilder method mimeBuildEncryptedMessage.

private void mimeBuildEncryptedMessage(@NonNull Body encryptedBodyPart) throws MessagingException {
    MimeMultipart multipartEncrypted = createMimeMultipart();
    multipartEncrypted.setSubType("encrypted");
    multipartEncrypted.addBodyPart(MimeBodyPart.create(new TextBody("Version: 1"), "application/pgp-encrypted"));
    MimeBodyPart encryptedPart = MimeBodyPart.create(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);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

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