IMAP connection with Dovecot keeps failing (time-out)

Solution 1:

You have a DROP rule before ufw rules which deny imaps connections. And this is not the only rule which can create problems. You should check how they got there, maybe you used -I instead of -A in before.rules or after.rules.

1381K  214M ACCEPT     all  --  lo     any     anywhere             anywhere            
 222K   45M ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
  398 23248 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh
 7903  443K ACCEPT     tcp  --  any    any     anywhere             anywhere             multiport dports http,https
 7460  441K DROP       all  --  any    any     anywhere             anywhere 

All above rules are inserted before ufw rules and can make problems. For example, if you configure ufw to deny http or https, these rules will allow these ports anyway.