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
formatted.add(personal);
} else
formatted.add(address.toString());
return TextUtils.join(", " , formatted);
}
String getHtml() throws MessagingException {
return getHtml(imessage);
}
private String getHtml(Part part) throws MessagingException {
if (part.isMimeType("text/*"))
try {
String s = part.getContent().toString();
if (part.isMimeType("text/plain"))
s = "" + s.replaceAll("\\r?\\n" , "
") + "";
return s;
} catch (IOException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return null;
}
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 (IOException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
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 (IOException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
}
return null;
}
public List getAttachments() throws IOException, MessagingException {
List result = new ArrayList<>();
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)));
}
return result;
}
private List getAttachments(BodyPart part) throws IOException, MessagingException {
List result = new ArrayList<>();
Object content = part.getContent();
if (content instanceof InputStream || content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || !TextUtils.isEmpty(part.getFileName())) {
ContentType ct = new ContentType(part.getContentType());
EntityAttachment attachment = new EntityAttachment();
attachment.name = part.getFileName();
attachment.type = ct.getBaseType();
attachment.size = part.getSize();
attachment.part = part;
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;
}
}