First create your mailbox in the /var/mail because ubuntu doesn't creates the user mailbox by default, to create run this command:
echo "this is test message" | mail my_user_name
To enable for other users to creat mailbox by default, open the /etc/default/useradd and uncomment the line:
CREATE_MAIL_SPOOL=yes
Install the mail server exim4 and the imap and pop3 server dovecot:
apt install exim4-daemon-heavy dovecot-imapd dovecot-pop3d
The exim4 server by default only accepts the mails from the local machine, configure it for the out side internet run:
dpkg-reconfigure exim4-config
Follow these steps: internet site; mail is sent and received directly using SMTP
Write the system mail name:
mail.example.com
IP-addresses to listen on for incoming SMTP connections:leave this field blank if there is 127.0.0.1 clear it.
Other destinations for which mail is accepted, write the domains for witch want to accept mail with, separate with semicolon:
example.com;example.net
Domains to relay mail for:
leave blank
Machines to relay mail for:
leave blank
Keep number of DNS-queries minimal (Dial-on-Demand)? select No:
no
Delivery method for local mail, select:
mbox format in /var/mail/
Split configuration into small files? select:
no
Save and exit the configuration.