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);
}
static String getSortKey(Address[] addresses) {
if (addresses == null || addresses.length == 0)
return null;
InternetAddress address = (InternetAddress) addresses[0];
String personal = address.getPersonal();
if (TextUtils.isEmpty(personal))
return address.getAddress();
else
return personal;
}
String getHtml() throws MessagingException, IOException {
return getHtml(imessage);
}
private static String readStream(InputStream is, String charset) throws IOException {
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);
return new String(os.toByteArray(), charset);
}
private static String getHtml(Part part) throws MessagingException, IOException {
if (part.isMimeType("text/*")) {
String s;
try {
Object content = part.getContent();
try {
if (content instanceof String)
s = (String) content;
else if (content instanceof InputStream)
// Typically com.sun.mail.util.QPDecoderStream
s = readStream((InputStream) content, "UTF-8");
else
s = content.toString();
} catch (UnsupportedEncodingException ex) {
// x-binaryenc
// https://javaee.github.io/javamail/FAQ#unsupen
Log.w("Unsupported encoding: " + part.getContentType());
return readStream(part.getInputStream(), "US-ASCII");
}
} catch (IOException ex) {
// IOException; Unknown encoding: none
Log.w(ex);
return "" + ex + "
" + android.util.Log.getStackTraceString(ex) + "
";
}
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(ex);
text = "" + ex + "
" + android.util.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 (ParseException ex) {
Log.w(ex);
return "" + ex + "
" + android.util.Log.getStackTraceString(ex) + "
";
}
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 (IOException ex) {
if (ex.getCause() instanceof MessagingException)
Log.w(ex);
else
throw ex;
} catch (ParseException ex) {
Log.w(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("attachment content type=" + part.getContentType());
content = part.getInputStream();
} catch (ParseException ex) {
Log.w(ex);
content = null;
}
if (content instanceof InputStream || content instanceof String) {
String disposition;
try {
disposition = part.getDisposition();
} catch (MessagingException ex) {
Log.w(ex);
disposition = null;
}
String filename;
try {
filename = part.getFileName();
} catch (MessagingException ex) {
Log.w(ex);
filename = null;
}
if (Part.ATTACHMENT.equalsIgnoreCase(disposition) ||
part.isMimeType("image/*") ||
!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().toLowerCase();
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) ||
"message/disposition-notification".equals(attachment.type)) {
String extension = Helper.getExtension(attachment.name);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (type != null) {
Log.w("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;
}
static boolean equal(Address[] a1, Address[] a2) {
if (a1 == null && a2 == null)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.length != a2.length)
return false;
for (int i = 0; i < a1.length; i++)
if (!a1[i].toString().equals(a2[i].toString()))
return false;
return true;
}
}