SMTP for Node.js — application mail that arrives
Node.js has no built-in mail function; almost every application uses Nodemailer for that. The library makes sending easy, but says nothing about deliverability: that depends entirely on the SMTP server you send through.
Nodemailer works with a transport. You create an SMTP transport with your relay's details — host, port, encryption and login — and the rest of your code stays untouched. What leaves through a local server or a test account today goes through 2mail tomorrow.
Why it goes wrong without a relay
A Node.js application typically sends mail at the moment it matters: a verification code, a password reset, a booking confirmation. Those are messages expected within seconds. If they leave from a server without reputation, some of them arrive late or not at all — and the user clicks "resend" until they give up.
Setting up SMTP in Node.js
Collect your details
Log in to your 2mail account and retrieve your SMTP server, username and password there.
Fill them in inside Node.js
Create an SMTP transport in Nodemailer. Set the host to 2mail's SMTP server and the port to 587, and switch the secure option off so the connection is upgraded via STARTTLS. Under auth, fill in the username and password of your 2mail login. Read those details from environment variables, not from the code. Nodemailer has a verify method to check the connection before you send the first real mail — use it.
Authenticate your domain
Make sure SPF, DKIM and DMARC are set correctly for your domain, so recipients can verify that the mail comes from you.
Test
Send a test message to an address at a major provider and check that it arrives in the inbox and not in the spam folder.
The fields Node.js asks for
Every platform names them slightly differently, but it is always the same five. The actual values are tied to your account and can be found in your 2mail account — take them from there, so you are sure to use the right ones.
| Field | What you enter |
|---|---|
| SMTP server (host) | The server name from your 2mail account. |
| Port | The port that matches your chosen encryption; your account shows which one. |
| Encryption | TLS. Never send unencrypted — your credentials would otherwise travel in plain text. |
| Username | The SMTP login of your 2mail account. |
| Password | The matching password. Never use the password of an employee's mailbox. |
Which emails Node.js sends
As soon as the relay is set up, all of these run through 2mail — you do not have to configure them one by one.
Pitfalls with Node.js
secure and port have to match
The secure option does not mean "safe or unsafe" but "TLS from the first byte". On port 587 secure should be off — STARTTLS follows afterwards. Switch it on anyway and the connection hangs or is dropped immediately, without a clear error.
One transport, reused
Create the transport once at start-up and reuse it, rather than setting up a new one per mail. During a spike — a reminder run, a campaign — you otherwise open hundreds of connections at once, which a relay may read as abuse. Nodemailer can pool connections; enable that for larger volumes.
Wait for the result and log it
Sending is asynchronous. If you do not wait for the server's response or do not log it, you never see that a message was refused. Log at least the message id and any rejection, and preferably hook up 2mail's webhooks for delivery and bounce, so you also see what goes wrong after handover.
A different platform?
SMTP is a standard protocol: any system that speaks SMTP — a CRM, an ERP, a custom application or a server — can send through 2mail using the same five fields.