System

How to send Email with netcat

netcat-nc There are many ways to send email from the Linux command line, mainly depending on user preference and the programs installed. When testing a system, however, entering the commands and message details each time can make every test time-consuming. This article introduces a quick way to send test mail with nc, allowing a message to be sent with one command. To send mail with nc, follow these steps. Run date as shown below to generate a date string for the Date: header.

# date '+%a, %d %b %Y %H:%M:%S %z'

Output:

Thu, 12 Mar 2015 12:00:35 +0700

Create a file containing the mail commands. This example names it /tmp/test_message.

HELO host1.ezylinux.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [User1] <[email protected]>
To: <[email protected]>
Date: Thu, 12 Mar 2015 12:00:35 +0700
Subject: Test sending message

Have a good day!
.
QUIT

Run nc to send the email:

# nc smtp.ezylinux.com 25 < /tmp/test_message
220 mail.ezylinux.com ESMTP Postfix
250 mail.ezylinux.com
250 2.1.0 Ok
250 2.1.5 Ok
354 End data with <CR><LF>.<CR><LF>
250 2.0.0 Ok: queued as 9434DA802F7
221 2.0.0 Bye

After running the command, check for the email.

EOF.← Back to articles