Search in sources :

Example 76 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 77 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 78 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 79 with BodyPart

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

the class MessageCryptoStructureDetector method findPrimaryPartInMixed.

@Nullable
private static Part findPrimaryPartInMixed(Part part, List<Part> outputExtraParts) {
    Body body = part.getBody();
    boolean isMultipartMixed = part.isMimeType("multipart/mixed") && body instanceof Multipart;
    if (!isMultipartMixed) {
        return null;
    }
    Multipart multipart = (Multipart) body;
    if (multipart.getCount() == 0) {
        return null;
    }
    BodyPart firstBodyPart = multipart.getBodyPart(0);
    Part foundPart;
    if (isPartEncryptedOrSigned(firstBodyPart)) {
        foundPart = firstBodyPart;
    } else {
        foundPart = findPrimaryPartInAlternative(firstBodyPart);
    }
    if (foundPart != null && outputExtraParts != null) {
        for (int i = 1; i < multipart.getCount(); i++) {
            outputExtraParts.add(multipart.getBodyPart(i));
        }
    }
    return foundPart;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Body(com.fsck.k9.mail.Body) Nullable(androidx.annotation.Nullable)

Example 80 with BodyPart

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

the class MessageCryptoStructureDetector method findMultipartEncryptedParts.

public static List<Part> findMultipartEncryptedParts(Part startPart) {
    List<Part> encryptedParts = new ArrayList<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(startPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        Body body = part.getBody();
        if (isPartMultipartEncrypted(part)) {
            encryptedParts.add(part);
            continue;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (int i = multipart.getCount() - 1; i >= 0; i--) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                partsToCheck.push(bodyPart);
            }
        }
    }
    return encryptedParts;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Aggregations

BodyPart (com.fsck.k9.mail.BodyPart)86 Test (org.junit.Test)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)65 Part (com.fsck.k9.mail.Part)64 Message (com.fsck.k9.mail.Message)58 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)50 Multipart (com.fsck.k9.mail.Multipart)28 Body (com.fsck.k9.mail.Body)25 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)22 ArrayList (java.util.ArrayList)13 RobolectricTest (com.fsck.k9.RobolectricTest)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 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 OutputStream (java.io.OutputStream)11 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)10 MessagingException (com.fsck.k9.mail.MessagingException)10 Stack (java.util.Stack)9 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)8