Search in sources :

Example 6 with MimeType

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

the class MessageFulltextCreator method createFulltext.

public String createFulltext(@NonNull Message message) {
    Part textPart = textPartFinder.findFirstTextPart(message);
    if (textPart == null || hasEmptyBody(textPart)) {
        return null;
    }
    String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_FTS);
    String mimeType = textPart.getMimeType();
    if (!MimeUtility.isSameMimeType(mimeType, "text/html")) {
        return text;
    }
    return HtmlConverter.htmlToText(text);
}
Also used : Part(com.fsck.k9.mail.Part)

Example 7 with MimeType

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

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

Example 9 with MimeType

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

the class MimeBodyPart method getMimeType.

@Override
public String getMimeType() {
    String mimeTypeFromHeader = MimeUtility.getHeaderParameter(getContentType(), null);
    MimeType mimeType = MimeType.parseOrNull(mimeTypeFromHeader);
    return mimeType != null ? mimeType.toString() : getDefaultMimeType();
}
Also used : MimeType(com.fsck.k9.mail.MimeType) MimeUtility.isSameMimeType(com.fsck.k9.mail.internet.MimeUtility.isSameMimeType)

Example 10 with MimeType

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

the class MimeMessageHelper method setBody.

public static void setBody(Part part, Body body) throws MessagingException {
    part.setBody(body);
    if (part instanceof Message) {
        part.setHeader("MIME-Version", "1.0");
    }
    if (body instanceof Multipart) {
        Multipart multipart = ((Multipart) body);
        multipart.setParent(part);
        String contentType = Headers.contentTypeForMultipart(multipart.getMimeType(), multipart.getBoundary());
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
        // note: if this is ever changed to 8bit, multipart/signed parts must always be 7bit!
        setEncoding(part, MimeUtil.ENC_7BIT);
    } else if (body instanceof TextBody) {
        MimeValue contentTypeHeader = MimeParameterDecoder.decode(part.getContentType());
        String mimeType = contentTypeHeader.getValue();
        if (MimeUtility.mimeTypeMatches(mimeType, "text/*")) {
            String name = contentTypeHeader.getParameters().get("name");
            String contentType = Headers.contentType(mimeType, "utf-8", name);
            part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
        } else {
            part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
        }
        setEncoding(part, MimeUtil.ENC_QUOTED_PRINTABLE);
    } else if (body instanceof RawDataBody) {
        String encoding = ((RawDataBody) body).getEncoding();
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Message(com.fsck.k9.mail.Message)

Aggregations

Body (com.fsck.k9.mail.Body)8 Multipart (com.fsck.k9.mail.Multipart)8 BodyPart (com.fsck.k9.mail.BodyPart)6 Part (com.fsck.k9.mail.Part)6 MessagingException (com.fsck.k9.mail.MessagingException)4 IOException (java.io.IOException)4 Message (com.fsck.k9.mail.Message)3 MimeType (com.fsck.k9.mail.MimeType)3 File (java.io.File)3 ContentValues (android.content.ContentValues)2 Uri (android.net.Uri)2 Nullable (androidx.annotation.Nullable)2 Account (com.fsck.k9.Account)2 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)2 MimeHeader (com.fsck.k9.mail.internet.MimeHeader)2 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)2 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)2 Cursor (android.database.Cursor)1 RobolectricTest (com.fsck.k9.RobolectricTest)1 Address (com.fsck.k9.mail.Address)1