//------------------------------------------------------------------
// \\\|///
// \\ -^- //
// ( @ @ )
// +----------------------oOOo-(_)-oOOo---------------------+
//
// FREE SOFTWARE WRITEN BY NAVY, COPYLEFT (C) 2002
// SmtpClient Class 1.0
// Use smtp server with user authorization
// All rights reserved.
//
// Oooo
// +---------------------- oooO---( )---------------------+
// ( ) ) /
// \ ( (_/
// \_)
//------------------------------------------------------------------
package encrypt;
import java.io.*;
import java.net.*;
import java.util.Vector;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import encrypt.Base64;
/***
* 标准SMTP发信类
* <p>
* 标准的纯JAVA的SMTP发信客户端
程序,支持用户认证。
* <p>
* <p>
* @author Naven
* @see SmtpClient
***/
public class SmtpClient
{
//protected static final Log log = LogFactory.getLog(SmtpClient.class);
private static final String CMD_HELO = "HELO ";
private static final String CMD_AUTH_LOGIN = "AUTH LOGIN ";
private static final String CMD_MAIL_FROM = "MAIL FROM: ";
private static final String CMD_RCPT_TO = "RCPT TO: ";
private static final String CMD_DATA = "DATA";
private static final String CMD_HELP = "HELP";
private static final String CMD_RSET = "RSET";
private static final String CMD_NOOP = "NOOP";
private static final String CMD_QUIT = "QUIT";
private static final String END_OF_MAIL = "\r\n.\r\n";
private static final String RCV_SERVOK = "220"; // 220 服务就绪
private static final String RCV_HELO = "250"; // 250 要求的邮件操作完成
private static final String RCV_AUTH_LOGIN = "334";
private static final String RCV_AUTH_USER = "334";
private static final String RCV_AUTH_PASSWD = "334";
private static final String RCV_AUTH_OK = "235";
private static final String RCV_MAIL_FROM = "250";
private static final String RCV_RCPT_TO = "250";
private static final String RCV_DATA = "354";
private static final String RCV_SEND_END = "250";
private static final String RCV_RSET = "250";
private static final String RCV_NOOP = "250";
private static final String RCV_QUIT = "221"; // 221 服务关闭传输信道
private static final int SEND_BLOCK_SIZE = 1024; // 每次发送信件内容的块的大小
/**
* BASE64加密对象
*/
//private Base64 base64 = new Base64();
private static final int _NOTHING_SPECIAL_STATE = 0;
private static final int _LAST_WAS_CR_STATE = 1;
private static final int _LAST_WAS_NL_STATE = 2;
/**
* 记录处理邮件正文数据发送的状态
*/
private int _state = 0;
/**
* 用于处理邮件正文数据发送同步处理的锁定
*/
private Integer lock = new Integer(0);
/**
* client socket
*/
private Socket socketSmtp = null;
/**
* socket out printwriter
*/
private PrintWriter sout = null;
/**
* socket int reader
*/
private BufferedReader sin = null;
/**
* smtp email server address
*/
private String smtpServer = null;
/**
* email from user for smtp server
*/
private String user = null;
/**
* user password
*/
private String passwd = null;
/**
* sender's email address
*/
private String sender = null;
/**
* email from user for smtp server, base64 encode
*/
private String encryptUser = null;
/**
* user password, base64 encode
*/
private String encryptPasswd = null;
/**
* client localhost
*/
private String localHost = null;
/**
* error message
*/
private String errorString = "NO ERROR";
/***
* 初始化发信类
* <p>
* @param server SMTP服务器地址
* @param sender SMTP发信人邮件地址
***/
public SmtpClient(String server, String sender)
{
this(server, null, null, sender);
}
/***
* 初始化发信类
* <p>
* @param server SMTP服务器地址
* @param user SMTP发信人认证用户名
* @param passwd SMTP发信人认证密码
* @param sender SMTP发信人邮件地址
***/
public SmtpClient(String server, String user, String passwd, String sender)
{
this.smtpServer = server;
this.user = user;
this.passwd = passwd;
this.sender = sender;
if( this.user != null && this.passwd != null )
{
Base64 base64 = new Base64()