Create an SMTP server with NodeJS

What you're going to need.

  • Your own domain name with the ability to edit DNS records.
  • NodeJS
  • A fixed IP Address
  • If you're on a home network you will need to be able to set up port forward rules on your Internet Router.

DNS setup

In order for a domain to receive emails, it needs to know about the mail server that will handle incoming email.

Step 1

Delete any existing MX records on your domain.

Step 2

Add a new A record that points to your internet router IP Address.

Hostname: mail
Type: A
Desination IP Address: <Your routers Public IP Address>

Step 3

Create a new MX record that points to the fully qualified hostname

Hostname: @
Type: MX
Priority: 10
Destination MX:

Port Forwarding

You only need to do this if the server, computer, laptop that you will be running the server on, is behind a network router. How you configure this will be different for every router brand. At a high level, you need to tell the router to forward all traffic arriving on port 25 to the internal IP address of the computer running the SMTP server.

Forward all traffic arriving on port: 25TOIP Address: <Your internal IP Address>
port: 25

Firewall configuration

If you have a firewall set up on your network or computer, you will need to make sure that traffic on port 25 is allowed through. Consult the docs for the firewall your using.

Writing the code

Create a new folder called node_smtp_server

npm init -y
touch app.js
npm install smtp-server mailparser --save

Testing it out

I am going to send an email from my Gmail Account to When the message is received a few seconds later, the following is outputted to the terminal.


Writing a mail server in NodeJS is really simple and can provide some really powerful features for an application.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store