How to install and configure mail server in ubuntu 16.04

Installing and configuring a mail server in Ubuntu 16.04 involves several steps. Here's a step-by-step guide to help you set up a basic mail server using Postfix, Dovecot, and MySQL:

Step 1: Install required packages

Open a terminal and run the following command to install the required packages:

sudo apt-get update
sudo apt-get install postfix dovecot-imapd dovecot-pop3d mysql-server

Step 2: Configure Postfix

Postfix is the mail transfer agent (MTA) that will handle email delivery. Edit the Postfix configuration file:

sudo nano /etc/postfix/main.cf

Add the following lines to the end of the file:

mydestination = localhost
myhostname = mail.example.com
myorigin = /etc/mailname

Replace example.com with your domain name.

Step 3: Configure Dovecot

Dovecot is the IMAP and POP3 server that will handle email retrieval. Edit the Dovecot configuration file:

sudo nano /etc/dovecot/dovecot.conf

Add the following lines to the end of the file:

protocols = imap pop3
listen = *

Step 4: Configure MySQL

MySQL will store the email accounts and passwords. Create a new database and user:

sudo mysql -u root -p

Create a new database:

CREATE DATABASE mail;

Create a new user and grant privileges:

CREATE USER 'mailuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mail.* TO 'mailuser'@'localhost';

Step 5: Configure Postfix to use MySQL

Edit the Postfix configuration file:

sudo nano /etc/postfix/mysql.cf

Add the following lines to the end of the file:

hosts = localhost
user = mailuser
password = password
dbname = mail

Step 6: Configure Dovecot to use MySQL

Edit the Dovecot configuration file:

sudo nano /etc/dovecot/dovecot-sql.conf.ext

Add the following lines to the end of the file:

driver = mysql
connect = host=localhost dbname=mail user=mailuser password=password

Step 7: Restart services

Restart the Postfix and Dovecot services:

sudo service postfix restart
sudo service dovecot restart

Step 8: Test the mail server

Use the following command to test the mail server:

telnet localhost 25

This should connect you to the Postfix server. You can then send an email using the mail command:

mail -s "Test email" [email protected]

Replace [email protected] with the email address you want to send the email to.

Step 9: Configure email clients

To configure email clients such as Thunderbird or Outlook, you'll need to set up the mail server's hostname and port. For example, you can use the following settings:

That's it! You should now have a basic mail server set up in Ubuntu 16.04.