Setting Mail Server Inside CentOS VPS

Integrating a mail server into your virtual private machine is achievable with this platform. Here, we’ll walk you through the steps to set up a mail server on a CentOS VPS. First, access your container via SSH using your preferred method (we’ll use a web-based client for CentOS and follow these steps:

Set Up and Test the Mail Server

We’ll use Postfix as our mail server in this guide. Follow these steps to install it on your VPS and ensure it functions correctly:

Step 1. Install Postfix with the following yum command:


yum install postfix

Install Postfix

Step 2. During the installation, you will need to confirm the following prompts by pressing ‘y’:

Total download size

Download Size Confirmation

Step 3. Once the installation is complete, test the mail server’s functionality with this command:


echo "thisistestmail" | mail -s "test" {your_email}

Replace {your_email} with the email address you want to send the test email to.

Step 4. Check the specified email inbox to confirm that the test email was sent successfully..

Verify Test Email

Congratulations! The email server is now installed, and we can move on to configuring it properly.

Save $100 in the next
5:00 minutes?

Register Here

Configuring Postfix Authorized Networks

To ensure the newly installed Postfix mail server operates correctly, you’ll need to tweak its main configuration file. This involves specifying which clients are allowed to relay emails through Postfix.

Step 1. Edit the main.cf file: This file is located in the /etc/postfix directory.

  • If you want Postfix to forward emails only from the local machine, find and uncomment the following line by removing the # at the beginning:

mynetworks_style = host
  • To manually set the list of trusted clients, use the mynetworks parameter (note that this will override the previous setting):

mynetworks = {server1 IP address}, {server2 IP address}

Step 2. After making changes to the configuration files, you must reload Postfix to apply them:


/etc/init.d/postfix restart

Step 3. You can now send messages from the specified clients (the local machine or the listed servers).

Additionally, you’ll need to update your application’s configuration to ensure it works seamlessly with the new mail server setup. Add the appropriate code to your application’s configuration file on the VPS container.


package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.host", "host");
props.put("mail.smtp.port", "25");
try {
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Text of the message");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

That’s it! Your mail server is now configured to work with your application deployed on the VPS container.

Save $100 in the next
5:00 minutes?

Register Here