import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;public class SendMail
{
public static boolean AUTH = false;
public String phone = "";
public String host = "smtp.21cn.com";
public String to = "";
public String from = "[email protected]";
public String subject = "";
public String msg = "";
//public String baseCode = "8bit";
public String charset = "GB2312";
public String conType = "text/plain"; public SendMail( String _smtp, String _to, String _subject, String _msg )
{
host = _smtp;
to = _to;
subject = _subject;
msg = _msg; }
private String sendCommand( PrintWriter mailOut, BufferedReader mailIn, String cmd )
{
mailOut.print( cmd + "\r\n" );
mailOut.flush();
String line = new String();
try
{
line = mailIn.readLine();
}
catch( IOException e )
{
return null;
}
return line;
}

private boolean recIsCorr( String line, int iValue )
{
if( (line != null) && (line.length() >= 3) )
{
String sTemp = line.substring( 0, 3 );
try
{
int iTemp = (new Integer( sTemp )).intValue();
if( iTemp == iValue )
return true;
else
{
return false;
}
}
catch( Exception e )
{
return false;
}
}
else
{
return false;
}
}

private String sendData( PrintWriter mailOut, BufferedReader mailIn, String from, String to, String subject, String msg, String conType, String charset )
{
msg = "Date: " + (new java.util.Date()).toString() + "\r\n"
+ "Content-Type: " + conType + "; charset=" + charset + "\r\n"
+ "MIME-Version: 1.0 \r\n\r\n" + msg;
msg = "From: " + from + "\r\n" + msg;
msg = "To: " + to + "\r\n" + msg;
msg = "Subject: " + subject + "\r\n" + msg;
mailOut.print( msg );
mailOut.print( "\r\n.\r\n" );
mailOut.flush();
String line = new String();

try
{
line = mailIn.readLine();
}
catch( IOException e )
{
return null;
}
return line;
} public String send_EMail()
{
AUTH = isNeedAUTH( host );
try
{
Socket sock;
PrintWriter mailOut;
BufferedReader mailIn;
sock = new Socket( host, 25 );
mailOut = new PrintWriter( sock.getOutputStream(), true);
mailIn = new BufferedReader( new InputStreamReader( sock.getInputStream() ) );
boolean result = false;
String line = mailIn.readLine();
result = recIsCorr( line, 220 );
if( !result )
return "发送失败!";

if( host.equals( "smtp.sina.com.cn" ) )
line = sendCommand( mailOut, mailIn, "EHLO " + host );
else
line = sendCommand( mailOut, mailIn, "HELO " + host );
result = recIsCorr( line, 250 );
if( !result )
if( !recIsCorr( line, 220 ) ) return "发送失败!";

if( AUTH )
{
line = sendCommand( mailOut, mailIn, "AUTH LOGIN" );
line = sendCommand( mailOut, mailIn, get64Code("yourname") );
line = sendCommand( mailOut, mailIn, get64Code("yourpassword") );
}

line = sendCommand( mailOut, mailIn, "MAIL FROM:" + from );
result = recIsCorr( line, 250 );
if( !result )
if( !recIsCorr( line, 235 ) ) return "发送失败!";

line = sendCommand( mailOut, mailIn, "RCPT TO:" + to );
result = recIsCorr( line, 250 );
if( !result )
if ( !recIsCorr( line, 334 ) )
return "发送失败!";

line = sendCommand( mailOut, mailIn, "DATA " );
result = recIsCorr( line, 354 );
if( !result ) 
if( !recIsCorr( line, 334 ) ) 
if( !recIsCorr( line, 250 ) ) return "发送失败!";

line = sendData( mailOut, mailIn, from, to, subject, msg, conType, charset );

result = recIsCorr( line, 250 );
if( !result ) 
if( !recIsCorr( line, 235 ) ) 
if( !recIsCorr( line, 354 ) ) return "发送失败!";

line = sendCommand( mailOut, mailIn, "QUIT" );
result = recIsCorr( line, 221 );
if( !result ) 
if( !recIsCorr( line, 250 ) ) return "发送失败!";

sock.close();
}
catch( IOException e )
{
return "发送失败!";
}
return "发送成功!";
} private String get64Code( String code32 )
{
String code64 = new sun.misc.BASE64Encoder().encode(code32.getBytes()); 
return code64;
} private boolean isNeedAUTH( String host )
{
if( host.equals( "smtp.263.net" ) )
return true;
if( host.equals( "smtp.163.net" ) )
return true;
if( host.equals( "smtp.sina.com.cn" ) )
return true;
if( host.equals( "smtp.sohu.com" ) )
return true;

return false;
}
}这是我的发送原程序,你看看,希望对你有点帮助。