当前位置: 技术问答>java相关
SMTP 发信通过验证了,可没有发出信怎么回事?附程序 谢谢!!!
来源: 互联网 发布时间:2015-05-10
本文导语: ////////////////////////////////////// ////program: ////////////////////////////////////// public void sendMail() { try { displayKey(); Socket s = new Socket(mailServer, 25); BufferedReader...
//////////////////////////////////////
////program:
//////////////////////////////////////
public void sendMail() {
try {
displayKey();
Socket s = new Socket(mailServer, 25);
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
send( in, out, "AUTH LOGIN" );
send( in, out, get64Code("w.hg") );
send( in, out, get64Code("3213211") );
send(in, out, "MAIL FROM: " + adresser);
send(in, out, "RCPT TO: " + recipient);
send(in, out, "DATA");
send(out, "Subject: " + subject);
send(out, "From: " + adresser);
send (out, "n");
for(int h=0;h = 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() + "rn"
+ "Content-Type: " + conType + "; charset=" + charset + "rn"
+ "MIME-Version: 1.0 rnrn" + msg;
msg = "From: " + from + "rn" + msg;
msg = "To: " + to + "rn" + msg;
msg = "Subject: " + subject + "rn" + msg;
mailOut.print( msg );
mailOut.print( "rn.rn" );
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;
}
}
这是我的发送原程序,你看看,希望对你有点帮助。
////program:
//////////////////////////////////////
public void sendMail() {
try {
displayKey();
Socket s = new Socket(mailServer, 25);
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
send( in, out, "AUTH LOGIN" );
send( in, out, get64Code("w.hg") );
send( in, out, get64Code("3213211") );
send(in, out, "MAIL FROM: " + adresser);
send(in, out, "RCPT TO: " + recipient);
send(in, out, "DATA");
send(out, "Subject: " + subject);
send(out, "From: " + adresser);
send (out, "n");
for(int h=0;h = 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() + "rn"
+ "Content-Type: " + conType + "; charset=" + charset + "rn"
+ "MIME-Version: 1.0 rnrn" + msg;
msg = "From: " + from + "rn" + msg;
msg = "To: " + to + "rn" + msg;
msg = "Subject: " + subject + "rn" + msg;
mailOut.print( msg );
mailOut.print( "rn.rn" );
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;
}
}
这是我的发送原程序,你看看,希望对你有点帮助。