Search in sources :

Example 81 with Multipart

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

the class MessageTest method sampleMessage.

private MimeMessage sampleMessage() throws MessagingException, IOException {
    MimeMessage message = new MimeMessage();
    message.setFrom(new Address("from@example.com"));
    message.setRecipient(RecipientType.TO, new Address("to@example.com"));
    message.setSubject("Test Message");
    message.setHeader("Date", "Wed, 28 Aug 2013 08:51:09 -0400");
    message.setEncoding(MimeUtil.ENC_7BIT);
    MimeMultipart multipartBody = new MimeMultipart("multipart/mixed", generateBoundary());
    multipartBody.addBodyPart(textBodyPart());
    multipartBody.addBodyPart(binaryBodyPart());
    MimeMessageHelper.setBody(message, multipartBody);
    return message;
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart)

Example 82 with Multipart

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

the class MessageTest method nestedMessage.

private MimeMessage nestedMessage(MimeMessage subMessage) throws MessagingException, IOException {
    BinaryTempFileMessageBody tempMessageBody = new BinaryTempFileMessageBody(MimeUtil.ENC_8BIT);
    OutputStream out = tempMessageBody.getOutputStream();
    try {
        subMessage.writeTo(out);
    } finally {
        out.close();
    }
    MimeBodyPart bodyPart = new MimeBodyPart(tempMessageBody, "message/rfc822");
    bodyPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment");
    bodyPart.setEncoding(MimeUtil.ENC_7BIT);
    MimeMessage parentMessage = sampleMessage();
    ((Multipart) parentMessage.getBody()).addBodyPart(bodyPart);
    return parentMessage;
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) BinaryTempFileMessageBody(com.fsck.k9.mail.internet.BinaryTempFileMessageBody) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 83 with Multipart

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

the class MessageCryptoHelper method getDataSourceForEncryptedOrInlineData.

private OpenPgpDataSource getDataSourceForEncryptedOrInlineData() throws IOException {
    return new OpenPgpApi.OpenPgpDataSource() {

        @Override
        public Long getSizeForProgress() {
            Part part = currentCryptoPart.part;
            CryptoPartType cryptoPartType = currentCryptoPart.type;
            Body body;
            if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                body = encryptionPayloadPart.getBody();
            } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                body = part.getBody();
            } else {
                throw new IllegalStateException("part to stream must be encrypted or inline!");
            }
            if (body instanceof SizeAware) {
                long bodySize = ((SizeAware) body).getSize();
                if (bodySize > PROGRESS_SIZE_THRESHOLD) {
                    return bodySize;
                }
            }
            return null;
        }

        @Override
        @WorkerThread
        public void writeTo(OutputStream os) throws IOException {
            try {
                Part part = currentCryptoPart.part;
                CryptoPartType cryptoPartType = currentCryptoPart.type;
                if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                    Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                    BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                    Body encryptionPayloadBody = encryptionPayloadPart.getBody();
                    encryptionPayloadBody.writeTo(os);
                } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                    String text = MessageExtractor.getTextFromPart(part);
                    os.write(text.getBytes());
                } else {
                    throw new IllegalStateException("part to stream must be encrypted or inline!");
                }
            } catch (MessagingException e) {
                Timber.e(e, "MessagingException 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) SizeAware(com.fsck.k9.mail.internet.SizeAware) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body)

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