Search in sources :

Example 1 with TempFileBody

use of com.fsck.k9.mailstore.TempFileBody in project k-9 by k9mail.

the class MessageBuilder method addAttachmentsToMessage.

/**
     * Add attachments as parts into a MimeMultipart container.
     * @param mp MimeMultipart container in which to insert parts.
     * @throws MessagingException
     */
private void addAttachmentsToMessage(final MimeMultipart mp) throws MessagingException {
    for (Attachment attachment : attachments) {
        if (attachment.state != Attachment.LoadingState.COMPLETE) {
            continue;
        }
        String contentType = attachment.contentType;
        if (MimeUtil.isMessage(contentType)) {
            contentType = "application/octet-stream";
        // TODO reencode message body to 7 bit
        // body = new TempFileMessageBody(attachment.filename);
        }
        Body body = new TempFileBody(attachment.filename);
        MimeBodyPart bp = new MimeBodyPart(body);
        /*
             * Correctly encode the filename here. Otherwise the whole
             * header value (all parameters at once) will be encoded by
             * MimeHeader.writeTo().
             */
        bp.addHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\r\n name=\"%s\"", contentType, EncoderUtil.encodeIfNecessary(attachment.name, EncoderUtil.Usage.WORD_ENTITY, 7)));
        bp.setEncoding(MimeUtility.getEncodingforType(contentType));
        /*
             * TODO: Oh the joys of MIME...
             *
             * From RFC 2183 (The Content-Disposition Header Field):
             * "Parameter values longer than 78 characters, or which
             *  contain non-ASCII characters, MUST be encoded as specified
             *  in [RFC 2184]."
             *
             * Example:
             *
             * Content-Type: application/x-stuff
             *  title*1*=us-ascii'en'This%20is%20even%20more%20
             *  title*2*=%2A%2A%2Afun%2A%2A%2A%20
             *  title*3="isn't it!"
             */
        bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, String.format(Locale.US, "attachment;\r\n filename=\"%s\";\r\n size=%d", attachment.name, attachment.size));
        mp.addBodyPart(bp);
    }
}
Also used : TempFileBody(com.fsck.k9.mailstore.TempFileBody) Attachment(com.fsck.k9.activity.misc.Attachment) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) TempFileBody(com.fsck.k9.mailstore.TempFileBody)

Example 2 with TempFileBody

use of com.fsck.k9.mailstore.TempFileBody 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)

Aggregations

MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)2 Attachment (com.fsck.k9.activity.misc.Attachment)1 Body (com.fsck.k9.mail.Body)1 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)1 TextBody (com.fsck.k9.mail.internet.TextBody)1 TempFileBody (com.fsck.k9.mailstore.TempFileBody)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1