Setting Mail Server Inside VPS
Setting Mail Server Inside CentOS VPS
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.
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.
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.
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}
Restart Postfix
After editing the configuration, restart Postfix so the new settings are loaded:
/etc/init.d/postfix restart
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);
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.
mynetworks_style or mynetworks, restart Postfix, and configure the hosted application to use the SMTP service.
