服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

开放源码-smtp发信客户端 for java


  //------------------------------------------------------------------
// ///|///
// // -^- //
// ( @ @ )
// +----------------------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();

// base64 encode begain
byte[] buser = user.getbytes();
byte[] bpasswd= passwd.getbytes();

base64.startencode();
base64.encode(buser, buser.length);
base64.endencode();
this.encryptuser = new string(base64.getencodedresult());

base64.startencode();
base64.encode(bpasswd, bpasswd.length);
base64.endencode();
this.encryptpasswd = new string(base64.getencodedresult());
}
}

/***
* 获取处理的错误信息
* <p>
* @return 错误信息
***/
public string geterror() {
return errorstring;
}

/***
* 当出错时抛出错误信息
* <p>
* @param e 错误异常
***/
private void onerror(exception e)
{
this.errorstring = e.getmessage();
//log.error("onerror() " + this.errorstring);
//if( log.isdebugenabled() ) {
// log.debug("onerror()", e);
//}
}

/***
* 检查smtp协议通讯收到的信息是否成功,即以指定返回代号开头,smtp协议标准。
* <p>
* @param rcvmsg smtp协议通讯收到的信息
* @param code smtp协议通讯返回代号
* @exception ioexception 失败时抛出异常
***/
private void check(string rcvmsg, string code)
throws ioexception
{

扫描关注微信公众号