Last active
March 27, 2016 01:03
-
-
Save thorntonrose/f974f4de8820a468f126 to your computer and use it in GitHub Desktop.
Send simple message via SMTP as "ping"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
/** | |
* MailPing is used to test communication with a mail server. | |
* | |
* @author Thornton Rose | |
* @version 1.1 | |
*/ | |
public class MailPing | |
{ | |
private static final int SMTP_PORT = 25; | |
private static final char SMTP_ERROR_CODE1 = '4'; | |
private static final char SMTP_ERROR_CODE2 = '5'; | |
public static void main(String[] args) { | |
String host = null; | |
String recipient = null; | |
String sender = null; | |
// Check args. | |
if (args.length < 1) { | |
System.out.println("MailPing"); | |
System.out.println(""); | |
System.out.println("Version 1.1"); | |
System.out.println("by Thornton Rose"); | |
System.out.println(""); | |
System.out.println("This program is used to test communication with a mail server using SMTP. If"); | |
System.out.println("only host is specified, only the socket connection is tested. If recipient is"); | |
System.out.println("specified, a test message is sent to recipient. If sender is not specified,"); | |
System.out.println("sender = recipient."); | |
System.out.println(""); | |
System.out.println(""); | |
System.out.println("Usage:"); | |
System.out.println(" MailPing host [recipient] [sender]"); | |
System.out.println(""); | |
System.out.println("Parameters:"); | |
System.out.println(" host = Name or IP address of mail host."); | |
System.out.println(" recipient = E-mail address of recipient."); | |
System.out.println(" sender = E-mail address of sender."); | |
} | |
else { | |
// Get host. | |
host = args[0]; | |
// Get recipient and sender. | |
if (args.length >= 2) { | |
recipient = args[1]; | |
} | |
if (args.length >= 3) { | |
sender = args[2]; | |
} | |
if (sender == null) { | |
sender = recipient; | |
} | |
// Ping that server! | |
(new MailPing()).ping(host, recipient, sender); | |
} | |
System.exit(0); | |
} | |
/** ping() attempts to communicate with the specified host. All | |
* I/O for the session is printed to stdout. If recipient is not | |
* <i>null</i>, a test message is sent to the recipient. | |
* | |
* @param host The name or IP of the mail host (server). | |
* @param recipient The e-mail address of the recipient. | |
* @param sender The e-mail address of the sender. | |
*/ | |
public void ping(String host, String recipient, String sender) { | |
Socket mailSocket; | |
BufferedReader socketInput; | |
DataOutputStream socketOutput; | |
String mailData; | |
String domain; | |
try { | |
// Open the connection to the host. | |
mailSocket = new Socket(host, SMTP_PORT); | |
try { | |
// Get references to the input and output streams. | |
socketInput = new BufferedReader( | |
new InputStreamReader(mailSocket.getInputStream()) ); | |
socketOutput = new DataOutputStream(mailSocket.getOutputStream()); | |
// Get the initial reply from the server. | |
readReply(socketInput); | |
// If a recipient is specified, send a test message. | |
if (recipient != null) { | |
domain = "unknown.com"; | |
mailData = | |
"Date: " + (new Date()).toString() + "\r\n" + | |
"From: " + sender + "\r\n" + | |
"To: " + recipient + "\r\n" + | |
"Subject: Test message\r\n" + | |
"\r\n" + | |
"This is a test message from MailPing.\r\n"; | |
// Greet the server. | |
sendCommand(socketOutput, "HELO " + domain); | |
readReply(socketInput); | |
// Send the sender's address. | |
sendCommand(socketOutput, "MAIL FROM: " + sender); | |
readReply(socketInput); | |
// Send the recipient's address. | |
sendCommand(socketOutput, "RCPT TO: " + recipient); | |
readReply(socketInput); | |
// Start the data section. | |
sendCommand(socketOutput, "DATA"); | |
readReply(socketInput); | |
// Send the mail message. | |
sendCommand(socketOutput, mailData + "."); | |
readReply(socketInput); | |
// End the session. | |
sendCommand(socketOutput, "QUIT"); | |
readReply(socketInput); | |
} | |
} finally { | |
mailSocket.close(); | |
} | |
} | |
catch(Exception theException) { | |
System.out.println(theException); | |
} | |
} | |
/** | |
* sendCommand() sends an SMTP command to the SMTP server. An | |
* SMTP command is a string that looks like: | |
* <pre> | |
* [key words] [data][CR][LF] | |
* </pre> | |
* | |
* <p>Example: <pre>HELO xyz.com</pre> | |
* | |
* @param out The output stream to which to write the command. | |
* @param command The command to write. | |
* | |
* @exception IOException No special handling is done; the exception is | |
* passed back up the call chain. | |
*/ | |
private void sendCommand(DataOutputStream out, String command) | |
throws IOException { | |
out.writeBytes(command + "\r\n"); | |
System.out.println(command); | |
} | |
/** | |
* readReply() reads the reply from the SMTP server. | |
* | |
* @param reader The input reader from which to read the reply. | |
* | |
*/ | |
/* @exception IOException If an IOException occurs because of an error | |
* with socket IO, no special handling is done, and the exception is | |
* passed back up the call chain. If the status code in the reply from | |
* the SMTP server is equal to SMTP_ERROR_CODE1 or SMTP_ERROR_CODE2, an | |
* IOException containing "SMTP: " + the reply is thrown. | |
*/ | |
private void readReply(BufferedReader reader) | |
throws IOException { | |
String reply; | |
char statusCode; | |
reply = reader.readLine(); | |
statusCode = reply.charAt(0); | |
System.out.println(reply); | |
/* | |
if ( (statusCode == SMTP_ERROR_CODE1) | | |
(statusCode == SMTP_ERROR_CODE2) ) { | |
throw (new IOException("SMTP: " + reply)); | |
} | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment