Browse Source

Handle parse exception

main
M66B 6 years ago
parent
commit
2b0dc59829
2 changed files with 50 additions and 35 deletions
  1. +50
    -27
      app/src/main/java/eu/faircode/email/MessageHelper.java
  2. +0
    -8
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 50
- 27
app/src/main/java/eu/faircode/email/MessageHelper.java View File

@ -55,6 +55,7 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import javax.mail.internet.ParseException;
public class MessageHelper { public class MessageHelper {
private MimeMessage imessage; private MimeMessage imessage;
@ -300,6 +301,10 @@ public class MessageHelper {
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len); os.write(buffer, 0, len);
s = new String(os.toByteArray(), "US-ASCII"); 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")) if (part.isMimeType("text/plain"))
@ -309,30 +314,40 @@ public class MessageHelper {
if (part.isMimeType("multipart/alternative")) { if (part.isMimeType("multipart/alternative")) {
String text = null; String text = null;
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);
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; return text;
} }
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getHtml(mp.getBodyPart(i));
if (s != null)
return s;
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; return null;
} }
@ -340,14 +355,18 @@ public class MessageHelper {
public List<EntityAttachment> getAttachments() throws IOException, MessagingException { public List<EntityAttachment> getAttachments() throws IOException, MessagingException {
List<EntityAttachment> result = new ArrayList<>(); List<EntityAttachment> 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)));
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; return result;
@ -363,7 +382,11 @@ public class MessageHelper {
} catch (UnsupportedEncodingException ex) { } catch (UnsupportedEncodingException ex) {
Log.w(Helper.TAG, "attachment content type=" + part.getContentType()); Log.w(Helper.TAG, "attachment content type=" + part.getContentType());
content = part.getInputStream(); content = part.getInputStream();
} catch (ParseException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
content = null;
} }
if (content instanceof InputStream || content instanceof String) { if (content instanceof InputStream || content instanceof String) {
String disposition; String disposition;
try { try {


+ 0
- 8
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -521,8 +521,6 @@ public class ServiceSynchronize extends LifecycleService {
synchronizeMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) imessage, false); synchronizeMessage(ServiceSynchronize.this, folder, ifolder, (IMAPMessage) imessage, false);
} catch (MessageRemovedException ex) { } catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} }
EntityOperation.process(ServiceSynchronize.this); // download small attachments EntityOperation.process(ServiceSynchronize.this); // download small attachments
} catch (Throwable ex) { } catch (Throwable ex) {
@ -585,8 +583,6 @@ public class ServiceSynchronize extends LifecycleService {
EntityOperation.process(ServiceSynchronize.this); // download small attachments EntityOperation.process(ServiceSynchronize.this); // download small attachments
} catch (MessageRemovedException ex) { } catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
@ -1286,10 +1282,6 @@ public class ServiceSynchronize extends LifecycleService {
unchanged++; unchanged++;
} catch (MessageRemovedException ex) { } catch (MessageRemovedException ex) {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} catch (FolderClosedException ex) {
throw ex;
} catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
} }
EntityOperation.process(this); // download small attachments EntityOperation.process(this); // download small attachments


Loading…
Cancel
Save