Thursday, March 22, 2018

Sending email using netcat (nc) command

Sometimes you just have to send an email from a server you are currently working on, or you just need to test whether your email server is working, either way, you can always count on netcat to to the job for you. Below command will connect to the mail server you want, on port 25, and send email from there. Please take note, that this command is used in CentOS 7 server, using nmap-ncat-6.40-7.el7 package.

Connect to the mail.foo.bar on port 25

$ nc mail.foo.bar 25
220 mail.foo.bar ESMTP Postfix
Once connected, say hello to the email server
$ nc my.email.server 25 220 mail.foo.bar ESMTP Postfix
HELO foo.bar
250 mail.foo.bar
Specify a return address, just in case the email bounced. This is an SMTP protocol requirement.
$ nc my.email.server 25 220 mail.foo.bar ESMTP Postfix
HELO foo.bar
 
250 mail.foo.bar 
MAIL FROM: mrbean@foo.bar 
250 2.1.0 Ok
Specify recipient of the email.
$ nc my.email.server 25 220 mail.foo.bar ESMTP Postfix
HELO foo.bar

250 mail.foo.bar 
MAIL FROM: mrbean@foo.bar 
250 2.1.0 Ok 
RCPT TO: teddy@foo.bar 
250 2.1.5 Ok
Put in you message (any message will do, but below is just how normal email usually formatted). Once you are done typing in your message, put dot (.) as a signal you are done with the DATA, and server will reply with your queue number.
$ nc my.email.server 25 220 mail.foo.bar ESMTP Postfix
HELO foo.bar
250 mail.foo.bar 
MAIL FROM: mrbean@foo.bar
250 2.1.0 Ok 
RCPT TO: teddy@foo.bar
250 2.1.5 Ok 
DATA
354 End data with .
From: mrbean@foo.bar
To: teddy@foo.bar
Subject: A gift
Date: Thu, 22 Mar 2018 12:00:00 +0000
This is merely a gift for you. Enjoy.
.
250 2.0.0 Ok: queued as 7F571241267C
To exit, just type quit, and you are done.
$ nc my.email.server 25 220 mail.foo.bar ESMTP Postfix
HELO foo.bar
250 mail.foo.bar 
MAIL FROM: mrbean@foo.bar
250 2.1.0 Ok 
RCPT TO: teddy@foo.bar
250 2.1.5 Ok 
DATA
354 End data with .
From: mrbean@foo.bar
To: teddy@foo.bar
Subject: A gift
Date: Thu, 22 Mar 2018 12:00:00 +0000
This is merely a gift for you. Enjoy.
.
250 2.0.0 Ok: queued as 7F571241267C 
QUIT
221 2.0.0 Bye
That's all,  the recipient should already received the email, or you should get a return email if it is bounced.

No comments: