Search in sources :

Example 61 with BodyPart

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

the class MessageTest method binaryBodyPart.

private MimeBodyPart binaryBodyPart() throws IOException, MessagingException {
    String encodedTestString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/\r\n";
    BinaryTempFileBody tempFileBody = new BinaryTempFileBody(MimeUtil.ENC_BASE64);
    InputStream in = new ByteArrayInputStream(encodedTestString.getBytes("UTF-8"));
    OutputStream out = tempFileBody.getOutputStream();
    try {
        IOUtils.copy(in, out);
    } finally {
        out.close();
    }
    MimeBodyPart bodyPart = new MimeBodyPart(tempFileBody, "application/octet-stream");
    bodyPart.setEncoding(MimeUtil.ENC_BASE64);
    return bodyPart;
}
Also used : BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 62 with BodyPart

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

the class MessageTest method textBodyPart.

private MimeBodyPart textBodyPart() throws MessagingException {
    TextBody textBody = new TextBody("Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n");
    textBody.setCharset("utf-8");
    MimeBodyPart bodyPart = new MimeBodyPart();
    MimeMessageHelper.setBody(bodyPart, textBody);
    CharsetSupport.setCharset("utf-8", bodyPart);
    return bodyPart;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 63 with BodyPart

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

use of com.fsck.k9.mail.BodyPart 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)49 Part (com.fsck.k9.mail.Part)38 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)33 Test (org.junit.Test)32 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Multipart (com.fsck.k9.mail.Multipart)21 Body (com.fsck.k9.mail.Body)17 Message (com.fsck.k9.mail.Message)17 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)13 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)11 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)11 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 MessagingException (com.fsck.k9.mail.MessagingException)9 OutputStream (java.io.OutputStream)8 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)7 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)7 ArrayList (java.util.ArrayList)6 Stack (java.util.Stack)6 TextBody (com.fsck.k9.mail.internet.TextBody)5