Search in sources :

Example 26 with Body

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

the class MessageViewInfo method createWithErrorState.

public static MessageViewInfo createWithErrorState(Message message, boolean isMessageIncomplete) {
    try {
        Body emptyBody = new TextBody("");
        Part emptyPart = new MimeBodyPart(emptyBody, "text/plain");
        String subject = message.getSubject();
        return new MessageViewInfo(message, isMessageIncomplete, emptyPart, subject, false, null, null, null, null, null, null);
    } catch (MessagingException e) {
        throw new AssertionError(e);
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MessagingException(com.fsck.k9.mail.MessagingException) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body)

Example 27 with Body

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

the class MessageBuilder method addInlineAttachmentsToMessage.

private void addInlineAttachmentsToMessage(final MimeMultipart mp) throws MessagingException {
    for (String cid : inlineAttachments.keySet()) {
        Attachment attachment = inlineAttachments.get(cid);
        if (attachment.getState() != Attachment.LoadingState.COMPLETE) {
            continue;
        }
        Body body = new TempFileBody(attachment.getFileName());
        MimeBodyPart bp = MimeBodyPart.create(body);
        addContentType(bp, attachment.getContentType(), attachment.getName());
        addContentDisposition(bp, "inline", attachment.getName(), attachment.getSize());
        bp.addHeader(MimeHeader.HEADER_CONTENT_ID, cid);
        mp.addBodyPart(bp);
    }
}
Also used : TempFileBody(com.fsck.k9.mailstore.TempFileBody) 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 28 with Body

use of com.fsck.k9.mail.Body 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.getState() != Attachment.LoadingState.COMPLETE) {
            continue;
        }
        Body body = new TempFileBody(attachment.getFileName());
        MimeBodyPart bp = MimeBodyPart.create(body);
        addContentType(bp, attachment.getContentType(), attachment.getName());
        addContentDisposition(bp, "attachment", attachment.getName(), attachment.getSize());
        mp.addBodyPart(bp);
    }
}
Also used : TempFileBody(com.fsck.k9.mailstore.TempFileBody) 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 29 with Body

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

the class BodyTextExtractor method getBodyTextFromMessage.

/**
 * Fetch the body text from a messagePart in the desired messagePart format. This method handles
 * conversions between formats (html to text and vice versa) if necessary.
 */
@NonNull
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
    Part part;
    if (format == SimpleMessageFormat.HTML) {
        // HTML takes precedence, then text.
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: HTML requested, HTML found.");
            return getTextFromPartOrEmpty(part);
        }
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: HTML requested, text found.");
            String text = getTextFromPartOrEmpty(part);
            return HtmlConverter.textToHtml(text);
        }
    } else if (format == SimpleMessageFormat.TEXT) {
        // Text takes precedence, then html.
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: Text requested, text found.");
            return getTextFromPartOrEmpty(part);
        }
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: Text requested, HTML found.");
            String text = getTextFromPartOrEmpty(part);
            return HtmlConverter.htmlToText(text);
        }
    }
    // If we had nothing interesting, return an empty string.
    return "";
}
Also used : Part(com.fsck.k9.mail.Part) NonNull(androidx.annotation.NonNull)

Example 30 with Body

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

the class MessageCryptoHelper method getMultipartSignedContentPartIfAvailable.

@Nullable
private static MimeBodyPart getMultipartSignedContentPartIfAvailable(Part part) {
    MimeBodyPart replacementPart = NO_REPLACEMENT_PART;
    Body body = part.getBody();
    if (body instanceof MimeMultipart) {
        MimeMultipart multipart = ((MimeMultipart) part.getBody());
        if (multipart.getCount() >= 1) {
            replacementPart = (MimeBodyPart) multipart.getBodyPart(0);
        }
    }
    return replacementPart;
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) 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