formatted = new ArrayList<>();
for (Address address : addresses)
if (address instanceof InternetAddress) {
InternetAddress a = (InternetAddress) address;
String personal = a.getPersonal();
if (TextUtils.isEmpty(personal))
formatted.add(address.toString());
else {
personal = personal.replaceAll("[\\,\\<\\>]", "");
if (full)
formatted.add(personal + " <" + a.getAddress() + ">");
else
formatted.add(personal);
}
} else
formatted.add(address.toString());
return TextUtils.join(", ", formatted);
}
String getHtml() throws MessagingException, IOException {
return getHtml(imessage);
}
private static String getHtml(Part part) throws MessagingException, IOException {
if (part.isMimeType("text/*")) {
String s;
try {
s = part.getContent().toString();
} catch (UnsupportedEncodingException ex) {
// x-binaryenc
Log.w(Helper.TAG, "Unsupported encoding: " + part.getContentType());
// https://javaee.github.io/javamail/FAQ#unsupen
InputStream is = part.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
s = new String(os.toByteArray(), "US-ASCII");
} catch (IOException ex) {
// IOException; Unknown encoding: none
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
s = ex.toString();
}
if (part.isMimeType("text/plain"))
s = "" + s.replaceAll("\\r?\\n", "
") + "
";
return s;
}
if (part.isMimeType("multipart/alternative")) {
String text = null;
try {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getHtml(bp);
} else if (bp.isMimeType("text/html")) {
String s = getHtml(bp);
if (s != null)
return s;
} else
return getHtml(bp);
}
} catch (ParseException ex) {
// ParseException: In parameter list boundary="...">, expected parameter name, got ";"
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
text = ex.toString();
}
return text;
}
if (part.isMimeType("multipart/*"))
try {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getHtml(mp.getBodyPart(i));
if (s != null)
return s;
}
} catch (ParseException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return ex.toString();
}
return null;
}
public List getAttachments() throws IOException, MessagingException {
List result = new ArrayList<>();
try {
Object content = imessage.getContent();
if (content instanceof String)
return result;
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++)
result.addAll(getAttachments(multipart.getBodyPart(i)));
}
} catch (ParseException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
return result;
}
private static List getAttachments(BodyPart part) throws
IOException, MessagingException {
List result = new ArrayList<>();
Object content;
try {
content = part.getContent();
} catch (UnsupportedEncodingException ex) {
Log.w(Helper.TAG, "attachment content type=" + part.getContentType());
content = part.getInputStream();
} catch (ParseException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
content = null;
}
if (content instanceof InputStream || content instanceof String) {
String disposition;
try {
disposition = part.getDisposition();
} catch (MessagingException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
disposition = null;
}
String filename;
try {
filename = part.getFileName();
} catch (MessagingException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
filename = null;
}
if (Part.ATTACHMENT.equalsIgnoreCase(disposition) || !TextUtils.isEmpty(filename)) {
ContentType ct = new ContentType(part.getContentType());
String[] cid = part.getHeader("Content-ID");
EntityAttachment attachment = new EntityAttachment();
attachment.name = filename;
attachment.type = ct.getBaseType();
attachment.size = part.getSize();
attachment.cid = (cid == null || cid.length == 0 ? null : cid[0]);
attachment.part = part;
// Try to guess a better content type
// Sometimes PDF files are sent using the wrong type
if ("application/octet-stream".equals(attachment.type) && attachment.name != null) {
String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (type != null) {
Log.w(Helper.TAG, "Guessing file=" + attachment.name + " type=" + type);
attachment.type = type;
}
}
}
if (attachment.size < 0)
attachment.size = null;
result.add(attachment);
}
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++)
result.addAll(getAttachments(multipart.getBodyPart(i)));
}
return result;
}
String getRaw() throws IOException, MessagingException {
if (raw == null) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
imessage.writeTo(os);
raw = Base64.encodeToString(os.toByteArray(), Base64.URL_SAFE);
}
return raw;
}
}