2008-11-26 13:18:32 +00:00
|
|
|
package zutil.network;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A simple class that connects and logs in to a SMTP
|
|
|
|
|
* server and then send emails.
|
|
|
|
|
* INFO: http://cr.yp.to/smtp/client.html
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class SMTPClient {
|
2008-12-05 15:13:26 +00:00
|
|
|
public static boolean DEBUG = false;
|
2008-11-26 13:18:32 +00:00
|
|
|
|
|
|
|
|
private BufferedReader in;
|
|
|
|
|
private PrintStream out;
|
|
|
|
|
private Socket socket;
|
|
|
|
|
private String url;
|
|
|
|
|
private int port;
|
|
|
|
|
|
|
|
|
|
public SMTPClient(String url){
|
|
|
|
|
this(url, 25);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SMTPClient(String url, int port){
|
|
|
|
|
this.url = url;
|
|
|
|
|
this.port = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connects to the server.
|
|
|
|
|
* Sends the message.
|
|
|
|
|
* Closes the connection
|
|
|
|
|
*
|
|
|
|
|
* @param from The destination email address
|
|
|
|
|
* @param to The recipients email address
|
|
|
|
|
* @param subj The subject of the message
|
|
|
|
|
* @param msg The message
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void send(String from, String to, String subj, String msg) throws IOException{
|
|
|
|
|
try{
|
|
|
|
|
connect();
|
|
|
|
|
// FROM and TO
|
|
|
|
|
sendCommand("MAIL FROM:"+from);
|
|
|
|
|
sendCommand("RCPT TO:"+to);
|
|
|
|
|
sendCommand("DATA");
|
|
|
|
|
// The Message
|
2008-12-05 15:13:26 +00:00
|
|
|
sendNoReplyCommand("Date: "+(new Date()), DEBUG);
|
|
|
|
|
sendNoReplyCommand("From: "+from, DEBUG);
|
|
|
|
|
sendNoReplyCommand("To: "+to, DEBUG);
|
|
|
|
|
sendNoReplyCommand("Subject: "+subj, DEBUG);
|
|
|
|
|
sendNoReplyCommand(" ", DEBUG);
|
|
|
|
|
sendNoReplyCommand(msg, DEBUG);
|
|
|
|
|
sendCommand(".", DEBUG);
|
2008-11-26 13:18:32 +00:00
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
close();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connects to the server
|
|
|
|
|
*
|
|
|
|
|
* @throws UnknownHostException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public void connect() throws UnknownHostException, IOException{
|
|
|
|
|
socket = new Socket(url, port);
|
|
|
|
|
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
|
|
|
|
out = new PrintStream(socket.getOutputStream());
|
|
|
|
|
|
2008-12-05 15:13:26 +00:00
|
|
|
readCommand(DEBUG);
|
2008-11-26 13:18:32 +00:00
|
|
|
sendCommand("HELO "+url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends the given line to the server and returns a status integer
|
|
|
|
|
*
|
|
|
|
|
* @param cmd The command to send
|
|
|
|
|
* @return The return code from the server
|
|
|
|
|
* @throws IOException if the cmd fails
|
|
|
|
|
*/
|
|
|
|
|
private int sendCommand(String cmd) throws IOException{
|
2008-12-05 15:13:26 +00:00
|
|
|
return parseReturnCode(sendCommand(cmd, DEBUG));
|
2008-11-26 13:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends the given line to the server and returns the last line
|
|
|
|
|
*
|
|
|
|
|
* @param cmd The command to send
|
|
|
|
|
* @param print To print out the received lines
|
|
|
|
|
* @return Last String line from the server
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private String sendCommand(String cmd, boolean print) throws IOException{
|
|
|
|
|
sendNoReplyCommand(cmd, print);
|
|
|
|
|
return readCommand(print);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a given command and don't cares about the reply
|
|
|
|
|
*
|
|
|
|
|
* @param cmd The command
|
|
|
|
|
* @param print If it should print to System.out
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private void sendNoReplyCommand(String cmd, boolean print) throws IOException{
|
|
|
|
|
out.println(cmd);
|
|
|
|
|
if(print)System.out.println(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads on line from the command channel
|
|
|
|
|
*
|
|
|
|
|
* @param print If the method should print the input line
|
|
|
|
|
* @return The input line
|
|
|
|
|
* @throws IOException if the server returns a error code
|
|
|
|
|
*/
|
|
|
|
|
private String readCommand(boolean print) throws IOException{
|
|
|
|
|
String tmp = in.readLine();
|
|
|
|
|
if(print)System.out.println(tmp);
|
|
|
|
|
if(parseReturnCode(tmp) >= 400 ) throw new IOException(tmp);
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses the return line from the server and returns the status code
|
|
|
|
|
*
|
|
|
|
|
* @param msg The message from the server
|
|
|
|
|
* @return The status code
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private synchronized int parseReturnCode(String msg){
|
|
|
|
|
return Integer.parseInt(msg.substring(0, 3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() throws IOException{
|
2008-12-05 15:13:26 +00:00
|
|
|
sendCommand("QUIT", DEBUG);
|
2008-11-26 13:18:32 +00:00
|
|
|
in.close();
|
|
|
|
|
out.close();
|
|
|
|
|
socket.close();
|
|
|
|
|
}
|
|
|
|
|
}
|