Set Up SMTP Server – A Step-By-Step Tutorial 

On January 26, 2024
10min read
Zakhar Yung Technical Content Writer @Mailtrap
Set up smtp server

In our blog post about the best free SMTP servers, we discussed cloud-based solutions for sending emails. These are pretty common for all sizes of projects. 

But what if you don’t want to use SMTP relay services provided by Gmail or its alternatives? In this blog post, we’ll teach you how to set up a local SMTP server and get rid of any email-sending dependencies. 

Do you really need an SMTP server?

Using an outgoing SMTP server is the traditional method of sending emails. SMTP server is a computer or an app that sends emails using Simple Mail Transfer Protocol (SMTP).

An alternative solution is to bypass the SMTP server and send emails directly to the destination. This is known as Direct Send and you can use it through specific email software programs. 

Direct Send is not very reliable, though, and here is why:

  • It cannot be used if port 25 is blocked by your Internet Service Provider (ISP).
  • It’s unfit for bulk email sending due to multiple connections to many different servers.
  • It cannot send non-personalized emails.
  • It can be blocked by email filters because emails are not sent from a regular SMTP server.

So, let’s put Direct Send away and figure out why someone would run their own SMTP server.

Pros and cons of a local SMTP server

ProsCons
No daily or hourly limit on the number of outgoing emails
Instant reporting 
Privacy and control of all your outgoing mail
No shared IP
Running a local SMTP server takes time and effort
The bounce rate may increase by 20-30%

To sum up the comparison, here’s the comment we found on Reddit:

Reddit comment about how hard it is to set up SMTP server
Source: Reddit

Cloud-based sending solutions such as Mailtrap Email Sending also provide all the advantages listed above. Such third-party solutions are more secure while still providing complete control over your email infrastructure. 

However, they still have the dependability factor. So, if you want to eliminate dependability, you can of course set up an SMTP server using specific software. 

Requirements for a local SMTP server

There are no specific requirements so far. Nevertheless, you should be prepared for the following:

  • Some ISPs may block port 25. You can solve this issue by contacting your ISP provider and finding out what limitations they may impose. Also, you need to request the correct DNS server and permission for MX record resolution.
  • For bulk emails, you should probably opt for a domain and a fixed IP address associated with it – this will increase your resistance to spam filters. 

Quick SMTP server setup

Now, let’s get the job done. Since you’re going to set up an SMTP server on your computer, it’s crucial to keep the operating system or OS in mind. So, we’ll explain how to configure a local SMTP server on macOS, Windows, and Linux machines. 

Important note: If you send test emails to the real email address, you might not find them in the inbox. Don’t forget to check your spam folder. Email clients like Gmail, Yahoo, Outlook, and others are suspicious of new mail transfer agents (MTAs). 

Make sure you back up the Postfix configuration file before making any changes. 

How to set up local SMTP server on macOS

Back in the day, macOS server was a powerful app that people used to manage network services and, you guessed it, set up mail servers. It used Postfix (an MTA) by default. 

Slowly, Apple reduced the features of the macOS Server and, in 2022, discontinued it completely. The app will no longer be updated to match the new versions of macOS. 

The existing versions of the macOS Server are compatible with Monterey and earlier macOS versions. This means that you can still use it for your SMTP server configuration. 

Note: The configuration may differ slightly depending on the OS version you’re using. In the tutorial below, we’ll set up a local SMTP server on macOS Big Sur. For earlier or later versions of macOS, refer to the GitHub repository. 

Postfix should be pre-installed on your machine. If not, you can install it using a third-party package manager such as Homebrew or MacPorts. For Homebrew, run the following command: 

brew install postfix

Then create a new configuration file by using the following piece of code: 

sudo nano /etc/postfix/main.cf

If Postfix is already installed, you can access it by running:

sudo vim /etc/postfix/main.cf

Tell Postfix which name it should use to identify itself to other mail servers. Add the following lines:

myhostname = john.example.com
myorigin = example.com

If your local username is John, the mail will appear to be from john@example.com. 

Now it’s time to set the relay host. It’s the machine that will accept emails after authentication and relay them to the SMTP server.

relayhost = mail.example.com:25

The next step is to add the local network’s IP, define TLS parameters and enable SASL. The latter requires an additional configuration file in which the password is stored. 

Create the sasl_passwd file:

sudo vim /etc/postfix/sasl_passwd

Add the following line:

mail.example.com:25 john@example.com:<your password>

Save the file with:

sudo postmap /etc/postfix/sasl_passwd

Now go back to your main.cf file. 

The complete code sample will look like this: 

myhostname = mailserver.my.home
myorigin = $myhostname

mydestination = $myhostname, mailserver.my.home, localhost.localdomain, localhost
relayhost = mail.example.com:25
mynetworks = 127.0.0.0/8, [::1]/128

smtpd_client_restrictions = permit_mynetworks permit_sasl_authenticated permit
recipient_delimiter = +
tls_random_source = dev:/dev/urandom
smtpd_tls_ciphers = medium

inet_interfaces = all
smtpd_use_tls=yes
smtpd_tls_cert_file=/etc/certs/mailserver.my.home.crt
smtpd_tls_key_file=/etc/ssl/private/mailserver.my.home.key
smtpd_tls_security_level=may

smtp_tls_CApath=/opt/homebrew/etc/ssl/
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated permit_inet_interfaces defer_unauth_destination
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps =  hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

sender_canonical_maps = hash:/etc/postfix/sender_canonical

virtual_maps = hash:/etc/postfix/virtual

home_mailbox = Maildir/

sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map

Now we have to autorun Postfix on boot and restart it. For that, we need to copy Postfix master plist out of the System folder. 

sudo cp /System/Library/LaunchDaemons/com.apple.postfix.master.plist /Library/LaunchDaemons/org.postfix.custom.plist 

Then we need to run: 

sudo vi /Library/LaunchDaemons/org.postfix.custom.plist

Com.apple.postfix.master value should be substituted with org.postfix.custom

Now we should remove these lines to prevent exiting after 60s

<string>-e</string>
<string>60</string>

and add these before </dict>

<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>

It’s time to relaunch the daemon 

sudo launchctl unload /Library/LaunchDaemons/org.postfix.custom.plist
sudo launchctl load /Library/LaunchDaemons/org.postfix.custom.plist

and check if it has started.

sudo launchctl list | grep org.postfix

Then you can check the listen port 25 

sudo lsof -i -P | grep LISTEN 

and check log.

log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info

How to setup SMTP mail server on Linux

Most Linux distributions are shipped with the two most common SMTP implementations: Sendmail and Postfix. Sendmail has quite a complex design and less secure architecture; that’s why we picked Postfix again. 

Installation

If you don’t have Postfix on your machine, you’ll need to install it. Also, you’ll need the mail package Mailutils, which includes programs necessary for Postfix to function:

sudo apt install mailutils postfix

In the Postfix Configuration window, you’ll need to choose the Internet Site mail configuration. Finally, you’ll need to specify the system mail name. It should be the same as the name you assigned to the domain name pointing to your server. Let’s use example.com

Configuration

Now, let’s configure Postfix to process requests to send emails from localhost. For this, you need to tweak the main configuration file main.cf:

sudo nano /etc/postfix/main.cf

Scroll down to the line inet_interfaces = all and replace it with inet_interfaces = loopback-only. This will tell Postfix to accept the incoming emails on localhost only. 

Also, you need to modify mydestination, which specifies the list of domains. It should look like this:

mydestination = $myhostname, localhost.$example.com, $example.com

Save the file and restart Postfix.

sudo systemctl restart postfix

In the end, try to send a test email.

echo "Test email body" | mail -s "Test email subject line" receipient's_email_address

How to set up local SMTP relay server on Windows 

To set up SMTP server on Windows, you have two main options: 

  • Use software options such as MailEnable, Apache James, or hMailServer. In this tutorial, we’ll be including steps for hMailServer as it’s the most popular option; 
  • Use Windows Internet Information Services (IIS) on the BizTalk server or other servers with IIS installed. 

Installation using hMailServer

Download the latest version of hMailServer and install it. At the beginning of the installation, you need to pay attention to the following:

  • In the Select Components window, ensure that full installation is chosen. Server and Administrative components must be checked.
  • In the Select database server type window, check the built-in database engine.
  • In the hMailServer Security window, create a password for the default user.

Once installed, it’s time to run and configure hMailServer.

  • Click ‘Connect’ on the starting window and enter the password you set up during the installation. 
  • Click ‘Add domain’ and enter your domain name in the corresponding field. By the way, you can use a fake domain name since the real one is not required to send emails. But, in that case, make sure that the fake domain does not actually exist. Click ‘Save’ and you’ll see your domain created in the left tab.
  • Select the ‘Accounts’ folder in the left tab and click ‘Add…’ to configure one. All you need to do here is enter a name in the ‘Address’ field. This will set up your email address. Optionally, you can specify a password. Click ‘Save’ and head on to ‘Settings’ in the left tab. 
  • Expand the ‘Settings’ thread and select ‘Protocols’. Uncheck POP3 and IMAP. For more on these protocols, read our IMAP vs POP3 vs SMTP comparison blog post. Then, click ‘Save’.
  • Now, select ‘Advanced’ in the ‘Settings’ thread and enter localhost in the ‘Default domain’ field in the right tab. Click ‘Save’. To finish up with the Advanced settings, select ‘Auto-ban’ and uncheck the ‘Enabled’ box. Click ‘Save’.
  • In the end, expand the ‘Utilities’ thread and select ‘Diagnostics’. Select your domain to run tests and click ‘Start’. We’re not interested in all of the tests, just ‘Collect server details’ and ‘Test outbound port’. These ones should be marked green.

That’s it. Now you can send your test email. This can be done with PowerShell – and it’s fast and easy. For more on this, read our blog post about sending emails from PowerShell. Run the following line (don’t forget to input your data):

Send-MailMessage -To "<recipient's email address>" -From "<your email address>"  -Subject "Your email subject" -Body "Your email body text" -Credential "<your username>" -SmtpServer "127.0.0.1" -Port 25

In the pop-up window, you’ll need to enter your password and then you’ll be good to go. 

Installation using IIS

SMTP relay configuration with IIS requires any version of Windows Server. We’ll break down the general installation process. For details on your specific version, refer to the Microsoft documentation

To install the SMTP server, you’ll need to complete the following steps: 

  • Access your ‘Server Manager’ and select ‘Dashboard’ in the left navigation bar; 
  • Choose ‘Add roles and features’ to start the wizard. This will allow you to extend the functionality of the server and configure it to perform specific tasks;
  • You’ll see the ‘Before you begin’ window that includes important installation information. Press ‘Next’;
  • Choose ‘Role-based or feature-based installation’ and press ‘Next’ to continue with the setup process; 
  • Next step is to select the server you want to use for SMTP installation. At this stage, you can select the servers that you added previously. A local server is the default option here;
  • In ‘Server roles’ you can press ‘Next’ without choosing any options; 
  • ‘Features’ tab is the place where we select the SMTP server. You may be prompted to configure additional features of the server. Click ‘Next’;
  • Once that’s done, you’ll see the confirmation window. Mark ‘Restart the destination server automatically if required’ to make sure the server reboots after installation. This will prompt the installation. Press ‘Close’ when the process is complete. 

After the installation, it’s necessary to configure the SMTP Virtual Server. For that, we need IIS 6.0 Manager. 

  • The first step is to open the IIS Manager. It can be done by searching inetmgr6.exe in Start or by accessing Server Manager and choosing ‘IIS Manager’ from the Tools tab; 
  • Expand the computer name to show a tree view of the server. Right-click on the ‘SMTP Virtual Server #1’ and choose ‘Properties’; 
  • Choose the ‘Relay’ button in the Access tab to modify the relay settings of your SMTP server; 
  • The next step is to choose the IP addresses that will be allowed to relay through this SMTP server. To grant access to your computer only, press ‘Add’, type ‘127.0.0.1’, and press ‘OK’. To add other computers, you’ll have to enter their IP addresses. Mark ‘Enable logging’ to receive records of email messages for easy troubleshooting. 
  • Go to the Delivery tab, select ‘Outbound Security’, and choose between the authentication methods. You have four options: Anonymous access, basic authentication, Integrated Windows Authentication, and TLS encryption. If you’re using the server for testing purposes, it’s recommended to use anonymous access to ensure the setup doesn’t fail AUTH command;
  • In the same tab, choose ‘Outbound connections’. The default TCP or SMTP Port is 25. You should make sure it’s not blocked. If you use any other port, you should check if it’s open within your firewall; 
  • Now select ‘Advanced’ to expand the advanced options of your SMTP Virtual Server. Typically, Fully Qualified Domain Names will be listed here. You should leave the Smart Host field empty. Keep in mind that some network providers may require the Smart Host – a server that will handle the delivery; 
  • Press ‘OK’ and restart the server to apply the settings. 

Once you complete these steps, you should test the settings with Telnet. If you’re able to send emails successfully, then this means that you have a working SMTP email server. 

Is an SMTP server good for testing?

In most cases, people set up local SMTP servers for testing purposes. If you’re developing an app that will send emails, you’ll need to test this function, and a real SMTP server running on your local machine might seem to be a good option. But, is it? 

On the one hand, installing and configuring a local SMTP server is not troublesome. However, there is a much better solution.

Mailtrap is an Email Delivery Platform that covers all your email-related needs. It lets you test, send, and control your email infrastructure through a single platform. 

Instead of lengthy configurations of the local SMTP server, you can use Mailtrap Email Testing – an Email Sandbox with a safe environment to test, inspect, and debug emails. All of this can be done at staging, meaning your app can send an email, but it won’t be delivered to the actual recipient. 

Try Mailtrap Now

Instead, the test email will go to the Sandbox’s inbox. Thus, there are no risks of spamming users. 

From the inbox, it’s possible to validate your email’s HTML/CSS and quickly see spam analysis results that are verified by the Apache SpamAssassin filter. 

Mailtrap Email Testing - Spam Analysis page

The installation process is straightforward. All you have to do is update your SMTP settings with the relevant credentials: 

  • Host: sandbox.smtp.mailtrap.io
  • Port: 25 or 465 or 587 or 2525
  • Username: unique for each inbox
  • Password: unique for each inbox

Alternatively, you can copy and paste a ready-to-use code snippet that appears once the relevant language is selected from the drop-down menu.

Mailtrap Email Testing API integration

When your app goes into production, you can use Mailtrap Email Sending to reach the recipients’ inboxes. It’s an email infrastructure that offers high deliverability rates by design. 

Unlike the local SMTP server, we provide DNS records for SPF, DKIM, and DMARC authentication protocols. We also run security checks for maximum protection and support encrypted connections. 

Once the emails are sent, Email Sending will allow you to access Actionable In-Depth Analytics. 

To wrap up

Running a local mail server can sometimes be a pain. A great deal of knowledge and effort are required to achieve good email deliverability using your own SMTP server. That’s why we recommend choosing an API and SMTP service such as Mailtrap Email Sending

A local SMTP server is perhaps good for testing purposes, but Mailtrap Email Testing, or other email testing tools, will do it better. 

So, do you actually need a local SMTP server? You’re the only one who can answer that. Good luck!

Article by Zakhar Yung Technical Content Writer @Mailtrap

Comments

1 replies

Michael Cooper

Doesn’t there have to be MX records to point the email back to your server?

Comments are closed.