📄 smtpconnection.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
/**
* smtp connection class
*
* @author zyp03
*
*/
public class SMTPConnection {
/* The socket connected to server*/
private Socket connection;
/*streams for reading and writing the socket*/
private BufferedReader fromServer;
private DataOutputStream toServer;
/* BASE 64 decode*/
//private String username = "MDM2MTEwOA==";
//private String pass = "MTk4NDcyNg==";
/* static final data*/
private String server = "localhost";
private int port = 25;
private static final String CRLF = "\r\n";
/* flag to sigh whether connected */
private boolean connected = false;
/* create an SMTPConnection object.Create the socket and the associated
* streams.
* constructor
*/
public SMTPConnection (Envelope envelope)throws IOException{
/*set up the connection and in out streams */
try{
if(connected==false){
connection = new Socket (server,port);
}
fromServer = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
toServer = new DataOutputStream(connection.getOutputStream());
}
catch(IOException ex){
System.out.print(ex);
throw ex;
}
/* check the reply code*/
String reply = fromServer.readLine();
int code = parseReply(reply);
if(code != 220){
throw new IOException();
}
//String localhsot = "10.130.204.244";
sendCommand("HELO "+server,250);
//sendCommand("AUTH LOGIN "+username,334);
//sendCommand(pass,235);
connected = true;
}
/**
* send the message
* @param envelope envelope contain all the message
* @throws IOException
*/
public void send(Envelope envelope)throws IOException{
/*
* send the message using sendcommand
*/
String command = "MAIL FROM: " + envelope.sender;
sendCommand(command,250);
command = "RCPT TO: " + envelope.recipient;
sendCommand(command,250);
command = "DATA " + CRLF;
sendCommand(command,354);
command = "Subject:"+envelope.message.subject+CRLF;
command += envelope.message.body+CRLF+"."+CRLF;
sendCommand(command,250);
}
/**
* close the connection
*
*/
public void close(){
try{
sendCommand("QUIT",221);
connection.close();
connected = false;
}catch(Exception e){
System.out.println("COLOSE FAILED...");
}
}
/**
* send an smtp command to the server and checke the reply code
* @param command
* @param rc
* @throws IOException
*/
private void sendCommand(String command,int rc)throws IOException{
// format the command sent to server
command += CRLF ;
System.out.print(command);
toServer.writeBytes(command);
/*get the reply*/
String reply = fromServer.readLine();
int code = parseReply(reply);
if(code == rc){
return ;
}
else
throw new IOException(command);
/*
DATA 354
HELO 250
MAIL FROM 250
QUIT 221
RCPT TO 250
*/
}
/**
* parse the reply from the server return the reply code
* @param reply
* @return
*/
private int parseReply(String reply){
StringTokenizer tokens = new StringTokenizer(reply);
int code = 0;
try{
code = Integer.parseInt(tokens.nextToken());
}
catch(Exception e){
code = -1;
}
return code;
}
/**
* destructor.closes the connection if fail
*/
protected void finalize()throws Throwable{
close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -