Solved!
For any of you experiencing connection issues with IMAP and Lion it has nothing to do with Mail preferences, recompiling IMAP Server or so.
This is a Mail.app bug, i've just waste all day at the office trying to sort this out: using tcpdump, connecting to the server using openssl, etc.
Mail.app tries to use AUTH PLAIN if available on the server, according to the RFC, this command must be followed by a base64 hash which it's total length must be divisible by 3. This hash is just a base64 encoding of our username and password.
So... the problem is that Mail.app does not add the necessary padding to this username + password so the hash length is divisible by 3.
So... how to just solve the problem??? Just change your password's length!! Add one or two character's (depending on your username and password length) so the base64 hash is divisible by 3. I found this solution by connecting to my imap server using openssl in the terminal by adding the padding manually.
Hope you find this usefull, I'm filing this as a bug so apple fixes it for any password/username length.