When you run “sudo ufw status” you can see that port 143 is in the list of allowed ports? If so, then the next question would be, have you checked if dovecot has the port open?
You can see the open listen ports by running “sudo netstat -plant”.
I already figured it out. The problem was in the dovecot file 99-local.conf, where there is a redefinition of inet_listener imap with binding only to local ip 127.0.0.1 …, just cancel the binding to a specific IP address.