当前位置: 技术问答>java相关
几个有关邮件收发难题,请教各位高手!
来源: 互联网 发布时间:2015-05-10
本文导语: 最近学习网络协议(smtp/pop3)做了一个邮件收发的小软件(没有使用javamail) 但是遇到一些难以解决的问题如下: 1,用pop3收到的中文为乱码 这是第 1 封邮件 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DATE : Wed, 17 Apr 20...
最近学习网络协议(smtp/pop3)做了一个邮件收发的小软件(没有使用javamail)
但是遇到一些难以解决的问题如下:
1,用pop3收到的中文为乱码
这是第 1 封邮件 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DATE : Wed, 17 Apr 2002 18:52:6 +0800
To : gamin119@163.net
From: lucky_i@263.net
Message-Id:
Subject: =?GB2312?Q?=C4=E3=BA=C3?= ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Z2FtaW4xMTmjrMT6usOjoQ0KDQqhoaGhICAgICAgICAgILeiy8224Le9yPe1xLilyPi3qCDL+bXD
t6LJorXYt70NCg0KoaGhoaGhoaGhoaGhoaGhodbCDQrA8aOhDQogCQkJCQ0KoaGhoaGhoaGhoaGh
oaGhoaGhoaGhoaGhoaGhoWx1Y2t5X2kNCqGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaFsdWNr
eV9pQDI2My5uZXQNCqGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaEyMDAyLTA0LTE3
DQo=
2,smtp怎样解决身份验证,有什么命令吗?
比如我是这样通信的:
S:220 163.net system SMTP(Anti Spam+) Server ready
C:helo abc
S:250 bjapp11.163.net
C:mail from:abc@abc.net
S:250 Ok
C:rcpt to:gamin119@21cn.com
S:550 : Invalid User
C:quit S:221 Bye
3,做了一个类似于telnet的程序
想维护一个输入过的命令队列
比如就像DOS方式,用上下键可以找到输入过得命令
开始想用一个下来列表,发现没办法像TextField那样接受输入
后来就用一个TextField 做成像DOS的命令行方式,可不知道怎么接受上下键
请指点一下!
但是遇到一些难以解决的问题如下:
1,用pop3收到的中文为乱码
这是第 1 封邮件 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DATE : Wed, 17 Apr 2002 18:52:6 +0800
To : gamin119@163.net
From: lucky_i@263.net
Message-Id:
Subject: =?GB2312?Q?=C4=E3=BA=C3?= ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Z2FtaW4xMTmjrMT6usOjoQ0KDQqhoaGhICAgICAgICAgILeiy8224Le9yPe1xLilyPi3qCDL+bXD
t6LJorXYt70NCg0KoaGhoaGhoaGhoaGhoaGhodbCDQrA8aOhDQogCQkJCQ0KoaGhoaGhoaGhoaGh
oaGhoaGhoaGhoaGhoaGhoWx1Y2t5X2kNCqGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaFsdWNr
eV9pQDI2My5uZXQNCqGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaEyMDAyLTA0LTE3
DQo=
2,smtp怎样解决身份验证,有什么命令吗?
比如我是这样通信的:
S:220 163.net system SMTP(Anti Spam+) Server ready
C:helo abc
S:250 bjapp11.163.net
C:mail from:abc@abc.net
S:250 Ok
C:rcpt to:gamin119@21cn.com
S:550 : Invalid User
C:quit S:221 Bye
3,做了一个类似于telnet的程序
想维护一个输入过的命令队列
比如就像DOS方式,用上下键可以找到输入过得命令
开始想用一个下来列表,发现没办法像TextField那样接受输入
后来就用一个TextField 做成像DOS的命令行方式,可不知道怎么接受上下键
请指点一下!
|
sun.misc.BASE64Decoder
一个例子:
import sun.misc.*;
import java.awt.*;
import java.io.*;
public class Base64Decode {
public static void main(String[] args) {
if (args.length >= 2) {
try {
InputStream is = new FileInputStream(args[0]);
OutputStream os = new FileOutputStream(args[1]);
BASE64Decoder b64dc = new BASE64Decoder();
b64dc.decodeBuffer(is, os);
}
catch (IOException e) {
System.err.println(e);
}
} // end if
} // end main
输入的是一个encode的文件,输出就是解码后的文件了。
一个例子:
import sun.misc.*;
import java.awt.*;
import java.io.*;
public class Base64Decode {
public static void main(String[] args) {
if (args.length >= 2) {
try {
InputStream is = new FileInputStream(args[0]);
OutputStream os = new FileOutputStream(args[1]);
BASE64Decoder b64dc = new BASE64Decoder();
b64dc.decodeBuffer(is, os);
}
catch (IOException e) {
System.err.println(e);
}
} // end if
} // end main
输入的是一个encode的文件,输出就是解码后的文件了。
|
验证的问题我已经解决了,可是不能发信加上验证后,哈哈!代码如下:
send( in, out, "AUTH LOGIN" );
send( in, out, get64Code("user_ID") );
send( in, out, get64Code("password") );
转换方法:
public String get64Code( String code32 ){
//把字符串转换成BASE64格式
String code64 = new sun.misc.BASE64Encoder().encode(code32.getBytes());
return code64;
}
共同提高。
send( in, out, "AUTH LOGIN" );
send( in, out, get64Code("user_ID") );
send( in, out, get64Code("password") );
转换方法:
public String get64Code( String code32 ){
//把字符串转换成BASE64格式
String code64 = new sun.misc.BASE64Encoder().encode(code32.getBytes());
return code64;
}
共同提高。
|
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 = "meijin_sz@21cn.com";
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 + "rn" );
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() + "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;
}
}
这是我的发送原程序,你看看
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 = "meijin_sz@21cn.com";
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 + "rn" );
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() + "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;
}
}
这是我的发送原程序,你看看