use of com.fsck.k9.mail.Multipart in project k-9 by k9mail.
the class ReconstructMessageTest method testMessage.
@Test
public void testMessage() throws IOException, MessagingException {
String messageSource = "From: from@example.com\r\n" + "To: to@example.com\r\n" + "Subject: Test Message \r\n" + "Date: Thu, 13 Nov 2014 17:09:38 +0100\r\n" + "Content-Type: multipart/mixed;\r\n" + " boundary=\"----Boundary\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "MIME-Version: 1.0\r\n" + "\r\n" + "This is a multipart MIME message.\r\n" + "------Boundary\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n" + "\r\n" + "------Boundary\r\n" + "Content-Type: text/plain\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "VGhpcyBpcyBhIHRl\r\n" + "c3QgbWVzc2FnZQ==\r\n" + "\r\n" + "------Boundary--\r\n" + "Hi, I'm the epilogue";
BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
MimeMessage message;
try {
message = MimeMessage.parseMimeMessage(messageInputStream, true);
} finally {
messageInputStream.close();
}
ByteArrayOutputStream messageOutputStream = new ByteArrayOutputStream();
try {
message.writeTo(messageOutputStream);
} finally {
messageOutputStream.close();
}
String reconstructedMessage = new String(messageOutputStream.toByteArray());
assertEquals(messageSource, reconstructedMessage);
}
use of com.fsck.k9.mail.Multipart 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 mimeType = multipart.getMimeType();
String contentType = String.format("%s; boundary=\"%s\"", mimeType, 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) {
String contentType;
if (MimeUtility.mimeTypeMatches(part.getMimeType(), "text/*")) {
contentType = String.format("%s;\r\n charset=utf-8", part.getMimeType());
String name = MimeUtility.getHeaderParameter(part.getContentType(), "name");
if (name != null) {
contentType += String.format(";\r\n name=\"%s\"", name);
}
} else {
contentType = part.getMimeType();
}
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
setEncoding(part, MimeUtil.ENC_QUOTED_PRINTABLE);
} else if (body instanceof RawDataBody) {
String encoding = ((RawDataBody) body).getEncoding();
part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
}
}
use of com.fsck.k9.mail.Multipart in project k-9 by k9mail.
the class MessageExtractor method findAttachments.
/**
* Traverse the MIME tree and add everything that's not a known text part to 'attachments'.
*
* @param multipart
* The {@link Multipart} to start from.
* @param knownTextParts
* A set of known text parts we don't want to end up in 'attachments'.
* @param attachments
* A list that will receive the parts that are considered attachments.
*/
private static void findAttachments(Multipart multipart, Set<Part> knownTextParts, @NonNull List<Part> attachments) {
for (Part part : multipart.getBodyParts()) {
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart innerMultipart = (Multipart) body;
findAttachments(innerMultipart, knownTextParts, attachments);
} else if (!knownTextParts.contains(part)) {
attachments.add(part);
}
}
}
use of com.fsck.k9.mail.Multipart in project k-9 by k9mail.
the class MimeBodyPart method getContentType.
@Override
public String getContentType() {
String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
if (contentType != null) {
return MimeUtility.unfoldAndDecode(contentType);
}
Multipart parent = getParent();
if (parent != null && "multipart/digest".equals(parent.getMimeType())) {
return "message/rfc822";
}
return "text/plain";
}
use of com.fsck.k9.mail.Multipart in project k-9 by k9mail.
the class MessageCryptoHelper method getDataSourceForSignedData.
private OpenPgpDataSource getDataSourceForSignedData(final Part signedPart) throws IOException {
return new OpenPgpDataSource() {
@Override
public void writeTo(OutputStream os) throws IOException {
try {
Multipart multipartSignedMultipart = (Multipart) signedPart.getBody();
BodyPart signatureBodyPart = multipartSignedMultipart.getBodyPart(0);
Timber.d("signed data type: %s", signatureBodyPart.getMimeType());
signatureBodyPart.writeTo(os);
} catch (MessagingException e) {
Timber.e(e, "Exception while writing message to crypto provider");
}
}
};
}
Aggregations