Search in sources :

Example 21 with Body

use of com.fsck.k9.mail.Body 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)

Example 22 with Body

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

the class MessageBuilder method buildText.

/**
 * Build the {@link Body} that will contain the text of the message.
 *
 * <p>
 * Draft messages are treated somewhat differently in that signatures are not appended and HTML
 * separators between composed text and quoted text are not added.
 * </p>
 *
 * @param isDraft
 *         If {@code true} we build a message that will be saved as a draft (as opposed to
 *         sent).
 * @param simpleMessageFormat
 *         Specifies what type of message to build ({@code text/plain} vs. {@code text/html}).
 *
 * @return {@link TextBody} instance that contains the entered text and possibly the quoted
 *         original message.
 */
private TextBody buildText(boolean isDraft, SimpleMessageFormat simpleMessageFormat) {
    TextBodyBuilder textBodyBuilder = new TextBodyBuilder(text);
    /*
         * Find out if we need to include the original message as quoted text.
         *
         * We include the quoted text in the body if the user didn't choose to
         * hide it. We always include the quoted text when we're saving a draft.
         * That's so the user is able to "un-hide" the quoted text if (s)he
         * opens a saved draft.
         */
    boolean includeQuotedText = (isDraft || quotedTextMode == QuotedTextMode.SHOW);
    boolean isReplyAfterQuote = (quoteStyle == QuoteStyle.PREFIX && this.isReplyAfterQuote);
    textBodyBuilder.setIncludeQuotedText(false);
    if (includeQuotedText) {
        if (simpleMessageFormat == SimpleMessageFormat.HTML && quotedHtmlContent != null) {
            textBodyBuilder.setIncludeQuotedText(true);
            textBodyBuilder.setQuotedTextHtml(quotedHtmlContent);
            textBodyBuilder.setReplyAfterQuote(isReplyAfterQuote);
        }
        if (simpleMessageFormat == SimpleMessageFormat.TEXT && quotedText.length() > 0) {
            textBodyBuilder.setIncludeQuotedText(true);
            textBodyBuilder.setQuotedText(quotedText);
            textBodyBuilder.setReplyAfterQuote(isReplyAfterQuote);
        }
    }
    textBodyBuilder.setInsertSeparator(!isDraft);
    boolean useSignature = (!isDraft && identity.getSignatureUse());
    if (useSignature) {
        textBodyBuilder.setAppendSignature(true);
        textBodyBuilder.setSignature(signature);
        textBodyBuilder.setSignatureBeforeQuotedText(isSignatureBeforeQuotedText);
    } else {
        textBodyBuilder.setAppendSignature(false);
    }
    TextBody body;
    if (simpleMessageFormat == SimpleMessageFormat.HTML) {
        body = textBodyBuilder.buildTextHtml();
    } else {
        body = textBodyBuilder.buildTextPlain();
    }
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody)

Example 23 with Body

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

the class MessageHelper method isCompletePartAvailable.

public static boolean isCompletePartAvailable(Part part) {
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(part);
    while (!partsToCheck.isEmpty()) {
        Part currentPart = partsToCheck.pop();
        Body body = currentPart.getBody();
        boolean isBodyMissing = body == null;
        if (isBodyMissing) {
            return false;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (BodyPart bodyPart : multipart.getBodyParts()) {
                partsToCheck.push(bodyPart);
            }
        }
    }
    return true;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) 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) Stack(java.util.Stack)

Example 24 with Body

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

the class TextPartFinder method findTextPartInMultipart.

private Part findTextPartInMultipart(Multipart multipart) {
    for (BodyPart bodyPart : multipart.getBodyParts()) {
        String mimeType = bodyPart.getMimeType();
        Body body = bodyPart.getBody();
        if (body instanceof Multipart) {
            Part candidatePart = findFirstTextPart(bodyPart);
            if (candidatePart != null) {
                return candidatePart;
            }
        } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) {
            return bodyPart;
        }
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body)

Example 25 with Body

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

the class TextPartFinder method findFirstTextPart.

@Nullable
public Part findFirstTextPart(@NonNull Part part) {
    String mimeType = part.getMimeType();
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        if (isSameMimeType(mimeType, "multipart/alternative")) {
            return findTextPartInMultipartAlternative(multipart);
        } else {
            return findTextPartInMultipart(multipart);
        }
    } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) {
        return part;
    }
    return null;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body) Nullable(androidx.annotation.Nullable)

Aggregations

Body (com.fsck.k9.mail.Body)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7