First install Fail2ban.
sudo apt-get install fail2ban
Then edit the configuration file at /etc/fail2ban/jail.conf
We are only showing the changed sections.
ignoreip = 127.0.0.1/8 202.166.0.0/16
bantime = 3600
maxretry = 3
[postfix]
enabled = true
port = smtp,ssmtp
filter = postfix
logpath = /var/log/mail.log
maxretry = 3
[sasl]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = sasl
logpath = /var/log/mail.log
maxretry = 3
[dovecot]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = dovecot
logpath = /var/log/mail.log
maxretry = 3
Then restart
sudo service fail2ban restart
Check the log at /var/log/fail2ban.log to see if everything is running smoothly