Setting Mail Server Inside VPS

Setting Mail Server Inside CentOS VPS

A CentOS Elastic VPS can host its own mail server. This guide uses Postfix to demonstrate how to install the mail service, verify outgoing email delivery, define which clients are allowed to relay messages, restart Postfix after configuration changes, and connect an application to the local SMTP service.
Mail Server Postfix is used as the SMTP mail-transfer agent in the source tutorial.
Access Method Connect to the CentOS VPS through SSH before installing and configuring Postfix.

Before You Begin

  • Prepare a CentOS Elastic VPS.
  • Connect to the required VPS container through SSH.
  • Use an account with sufficient privileges to install packages and modify Postfix configuration.
  • Have an external email address available for delivery testing.
The source tutorial uses a CentOS-based VPS and the historical yum package workflow. Commands can differ on newer operating-system releases.

Install Postfix

1Run the Postfix installer

Install Postfix through the CentOS package manager.

yum install postfix

During installation, confirm the prompts required to continue. The source specifically shows confirmation of the total download size and the package-signing GPG key.

Package verification: Review package names, repositories, and signing-key prompts before approving installation on a production server.

Test Email Delivery

After Postfix is installed, send a simple test message to an external email address:

echo thisistestmail | mail -s test {your_email}

Replace {your_email} with the recipient address that should receive the test message.

1Send the message

Run the test command from the VPS console.

2Check the recipient inbox

Confirm that the test email was delivered successfully before proceeding with relay configuration.

If the message is not delivered, check Postfix logs, DNS configuration, outbound SMTP restrictions, sender reputation, and any provider-specific mail limitations before assuming that the Postfix package itself is faulty.

Configure Authorized Networks

Postfix can be configured to relay mail only for selected trusted clients. Edit the main configuration file:

/etc/postfix/main.cf

If Postfix should forward mail only for the local machine, enable the following setting:

mynetworks_style = host

Alternatively, define the trusted clients explicitly:

mynetworks = {server1 IP address}, {server2 IP address}
Relay security: Do not configure Postfix as an unrestricted open relay. Limit trusted networks to the systems that genuinely need to submit mail through this server.

Restart Postfix

After editing the configuration, restart Postfix so the new settings are loaded:

/etc/init.d/postfix restart
The source uses the historical SysV-style service command above. Newer Linux releases may use systemctl instead.

After the restart, the local VPS or the IP addresses included in mynetworks can submit mail according to the configured Postfix relay policy.

Connect an Application to Postfix

The application hosted on the VPS must use the local Postfix server as its SMTP transport. The source demonstrates the concept with a JavaMail example using SMTP authentication and STARTTLS disabled and port 25.

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");

The remaining application logic creates a mail session, prepares the sender, recipient, subject and message body, and submits the message through Transport.send().

Session session = Session.getInstance(props);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@example.com"));
message.setRecipients(
    Message.RecipientType.TO,
    InternetAddress.parse("to-email@example.com")
);
message.setSubject("Testing Subject");
message.setText("Text of the message");

Transport.send(message);
Security note: The source example disables SMTP authentication and STARTTLS because it demonstrates a trusted internal relay. Do not copy those settings blindly for Internet-facing SMTP. Use authentication and encryption wherever the actual mail architecture requires them.

Production Considerations

  • Confirm whether the hosting provider allows outbound SMTP traffic, especially TCP port 25.
  • Configure proper DNS records such as PTR/reverse DNS, SPF, DKIM, and DMARC when the server will send Internet email.
  • Use a valid server hostname and appropriate mail domain.
  • Protect the server against unauthorized relay attempts.
  • Keep Postfix and the operating system updated.
  • Use TLS and authentication where required.
  • Monitor mail queues and logs for rejected or deferred messages.
Exact mail-server settings can differ between hosting-provider installations. Review the current Postfix documentation and provider-specific mail policies before using the server for production email.
Mail server setup summary: Connect to the CentOS VPS by SSH, install Postfix, verify outgoing mail with a test message, restrict relay access with mynetworks_style or mynetworks, restart Postfix, and configure the hosted application to use the SMTP service.

What’s next?