From b81af0a63a44a99dd5dd62159035b7144356ed9d Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 30 Aug 2020 19:13:01 +0200 Subject: [PATCH] Close SMTP connection before post processing --- .../java/eu/faircode/email/ServiceSend.java | 143 +++++++++--------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 3c9612c5cc..83bd964e82 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -577,6 +577,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar } // Create transport + long start, end; + Long max_size = null; try (EmailService iservice = new EmailService( this, ident.getProtocol(), ident.realm, ident.encryption, ident.insecure, debug)) { iservice.setUseIp(ident.use_ip, ident.ehlo); @@ -589,11 +591,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar throw new IOException("Test"); db.identity().setIdentityState(ident.id, "connected"); - if (ident.max_size == null) { - Long max_size = iservice.getMaxSize(); - if (max_size != null) - db.identity().setIdentityMaxSize(ident.id, max_size); - } + if (ident.max_size == null) + max_size = iservice.getMaxSize(); Address[] to = imessage.getAllRecipients(); String via = "via " + ident.host + "/" + ident.user + @@ -601,73 +600,10 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar // Send message EntityLog.log(this, "Sending " + via); - long start = new Date().getTime(); + start = new Date().getTime(); iservice.getTransport().sendMessage(imessage, to); - long end = new Date().getTime(); + end = new Date().getTime(); EntityLog.log(this, "Sent " + via + " elapse=" + (end - start) + " ms"); - - try { - db.beginTransaction(); - - // Delete from outbox - db.message().deleteMessage(message.id); - - // Show in sent folder - if (sid != null) { - db.message().setMessageReceived(sid, start); - db.message().setMessageSent(sid, end); - db.message().setMessageUiHide(sid, false); - } - - if (message.inreplyto != null) { - List replieds = db.message().getMessagesByMsgId(message.account, message.inreplyto); - for (EntityMessage replied : replieds) - EntityOperation.queue(this, replied, EntityOperation.ANSWERED, true); - } - - if (message.wasforwardedfrom != null) { - List forwardeds = db.message().getMessagesByMsgId(message.account, message.wasforwardedfrom); - for (EntityMessage forwarded : forwardeds) - EntityOperation.queue(this, forwarded, EntityOperation.KEYWORD, "$Forwarded", true); - } - - // Reset identity - db.identity().setIdentityConnected(ident.id, new Date().getTime()); - db.identity().setIdentityError(ident.id, null); - - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - - ServiceSynchronize.eval(this, "sent"); - - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancel("send:" + message.id, 1); - - // Check sent message - if (sid != null) { - try { - Thread.sleep(EXISTS_DELAY); - } catch (InterruptedException ex) { - Log.e(ex); - } - - try { - db.beginTransaction(); - - // Message could have been deleted - EntityMessage orphan = db.message().getMessage(sid); - if (orphan != null) - EntityOperation.queue(this, orphan, EntityOperation.EXISTS); - - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - - ServiceSynchronize.eval(this, "orphan"); - } } catch (MessagingException ex) { Log.e(ex); @@ -680,6 +616,73 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar } finally { db.identity().setIdentityState(ident.id, null); } + + try { + db.beginTransaction(); + + // Delete from outbox + db.message().deleteMessage(message.id); + + // Show in sent folder + if (sid != null) { + db.message().setMessageReceived(sid, start); + db.message().setMessageSent(sid, end); + db.message().setMessageUiHide(sid, false); + } + + // Mark replied + if (message.inreplyto != null) { + List replieds = db.message().getMessagesByMsgId(message.account, message.inreplyto); + for (EntityMessage replied : replieds) + EntityOperation.queue(this, replied, EntityOperation.ANSWERED, true); + } + + // Mark forwarded + if (message.wasforwardedfrom != null) { + List forwardeds = db.message().getMessagesByMsgId(message.account, message.wasforwardedfrom); + for (EntityMessage forwarded : forwardeds) + EntityOperation.queue(this, forwarded, EntityOperation.KEYWORD, "$Forwarded", true); + } + + // Update identity + if (max_size != null) + db.identity().setIdentityMaxSize(ident.id, max_size); + db.identity().setIdentityConnected(ident.id, new Date().getTime()); + db.identity().setIdentityError(ident.id, null); + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + ServiceSynchronize.eval(this, "sent"); + + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel("send:" + message.id, 1); + + // Check sent message + if (sid != null) { + try { + Thread.sleep(EXISTS_DELAY); + } catch (InterruptedException ex) { + Log.e(ex); + } + + try { + db.beginTransaction(); + + // Message could have been deleted + EntityMessage orphan = db.message().getMessage(sid); + if (orphan != null) + EntityOperation.queue(this, orphan, EntityOperation.EXISTS); + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + ServiceSynchronize.eval(this, "orphan"); + } } static void boot(final Context context) {