Support for multiple body parts

pull/174/head
M66B 6 years ago
parent d5cef39de0
commit 7347b161aa

@ -1221,78 +1221,90 @@ public class MessageHelper {
} }
class MessageParts { class MessageParts {
private Part plain = null; private List<Part> plain = new ArrayList<>();
private Part html = null; private List<Part> html = new ArrayList<>();
private List<AttachmentPart> attachments = new ArrayList<>(); private List<AttachmentPart> attachments = new ArrayList<>();
private ArrayList<String> warnings = new ArrayList<>(); private ArrayList<String> warnings = new ArrayList<>();
Boolean isPlainOnly() { Boolean isPlainOnly() {
if (plain == null && html == null) if (plain.size() + html.size() == 0)
return null; return null;
return (html == null); return (html.size() == 0);
} }
Long getBodySize() throws MessagingException { Long getBodySize() throws MessagingException {
Part part = (html == null ? plain : html); Long size = null;
if (part == null)
return null; List<Part> all = new ArrayList<>();
int size = part.getSize(); all.addAll(plain);
if (size < 0) all.addAll(html);
return null; for (Part p : all) {
else int s = p.getSize();
return (long) size; if (s >= 0)
if (size == null)
size = (long) s;
else
size += (long) s;
}
return size;
} }
String getHtml(Context context) throws MessagingException, IOException { String getHtml(Context context) throws MessagingException, IOException {
if (plain == null && html == null) { if (plain.size() + html.size() == 0) {
Log.i("No body part"); Log.i("No body part");
return null; return null;
} }
String result; StringBuilder sb = new StringBuilder();
Part part = (html == null ? plain : html);
if (part.getSize() > MAX_MESSAGE_SIZE) {
warnings.add(context.getString(R.string.title_insufficient_memory));
return null;
}
try { for (Part part : html.size() > 0 ? html : plain) {
Object content = part.getContent(); if (part.getSize() > MAX_MESSAGE_SIZE) {
Log.i("Content class=" + (content == null ? null : content.getClass().getName())); warnings.add(context.getString(R.string.title_insufficient_memory));
return null;
}
String result;
try {
Object content = part.getContent();
Log.i("Content class=" + (content == null ? null : content.getClass().getName()));
if (content == null) {
warnings.add(context.getString(R.string.title_no_body));
return null;
}
if (content == null) { if (content instanceof String)
warnings.add(context.getString(R.string.title_no_body)); result = (String) content;
else if (content instanceof InputStream)
// Typically com.sun.mail.util.QPDecoderStream
result = Helper.readStream((InputStream) content, StandardCharsets.UTF_8.name());
else
result = content.toString();
} catch (IOException | FolderClosedException | MessageRemovedException ex) {
throw ex;
} catch (Throwable ex) {
Log.w(ex);
warnings.add(Log.formatThrowable(ex, false));
return null; return null;
} }
if (content instanceof String) try {
result = (String) content; ContentType ct = new ContentType(part.getContentType());
else if (content instanceof InputStream) String charset = ct.getParameter("charset");
// Typically com.sun.mail.util.QPDecoderStream if (UnknownCharsetProvider.charsetForMime(charset) == null)
result = Helper.readStream((InputStream) content, StandardCharsets.UTF_8.name()); warnings.add(context.getString(R.string.title_no_charset, charset));
else } catch (ParseException ex) {
result = content.toString(); Log.e(ex);
} catch (IOException | FolderClosedException | MessageRemovedException ex) { }
throw ex;
} catch (Throwable ex) {
Log.w(ex);
warnings.add(Log.formatThrowable(ex, false));
return null;
}
try { if (part.isMimeType("text/plain"))
ContentType ct = new ContentType(part.getContentType()); result = "<div>" + HtmlHelper.formatPre(result) + "</div>";
String charset = ct.getParameter("charset");
if (UnknownCharsetProvider.charsetForMime(charset) == null)
warnings.add(context.getString(R.string.title_no_charset, charset));
} catch (ParseException ex) {
Log.e(ex);
}
if (part == plain) sb.append(result);
result = "<div>" + HtmlHelper.formatPre(result) + "</div>"; }
return result; return sb.toString();
} }
List<AttachmentPart> getAttachmentParts() { List<AttachmentPart> getAttachmentParts() {
@ -1637,14 +1649,11 @@ public class MessageHelper {
contentType = new ContentType(Helper.guessMimeType(filename)); contentType = new ContentType(Helper.guessMimeType(filename));
} }
if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && TextUtils.isEmpty(filename)) {
TextUtils.isEmpty(filename) && if ("text/plain".equalsIgnoreCase(contentType.getBaseType()))
((parts.plain == null && "text/plain".equalsIgnoreCase(contentType.getBaseType())) || parts.plain.add(part);
(parts.html == null && "text/html".equalsIgnoreCase(contentType.getBaseType())))) { else if ("text/html".equalsIgnoreCase(contentType.getBaseType()))
if ("text/html".equalsIgnoreCase(contentType.getBaseType())) parts.html.add(part);
parts.html = part;
else
parts.plain = part;
} else { } else {
AttachmentPart apart = new AttachmentPart(); AttachmentPart apart = new AttachmentPart();
apart.disposition = disposition; apart.disposition = disposition;

Loading…
Cancel
Save