|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。packagedfh;
importjava.io.Serializable;
importjava.util.Vector;
publicclassMailimplementsSerializable{
/**
*界说邮件的优先级.Mailprioritydefinition.
*/
publictransientstaticfinalintLOW_PRIORITY=5;
publictransientstaticfinalintNORMAL_PRIORITY=3;
publictransientstaticfinalintHIGH_PRIORITY=1;
/**
*界说邮件的发送格局.Sendingformatdefinition.
*/
publictransientstaticfinalintTEXT=1;
publictransientstaticfinalintHTML=2;
/**
*@since2.0
*/
publicintpriority=NORMAL_PRIORITY;
publicintformat=TEXT;
/**
*TheDNSservername.
*/
publicStringserver;
publicStringfrom;
publicStringto;
publicStringsubject;
publicStringbody;
publicVectorattachments;//Attachmentfiles
publicMail(Stringserver,Stringfrom,Stringto,Stringsubject,
Stringbody,Vectorattachments,intpriority,intformat){
this.server=server;
this.from=from;
this.to=to;
this.subject=subject;
this.body=body;
this.attachments=attachments;
this.priority=priority;
this.format=format;
}
}
packagedfh;
importsun.misc.*;
importjavax.activation.MimetypesFileTypeMap;
importjavax.mail.Message;
importjava.security.MessageDigest;
importjava.io.*;
importjava.util.*;
/**
*对指定的Mail对象举行MIME编码并前往编码了局.
*/
publicclassMailEncoder{
privateMailmail;
//Themailtobeencoded
privateMessageDigestdigest;
//Usedtogenarateaboundarystring
privateBASE64Encoderencoder=newBASE64Encoder();
//EncodecontenttoBASE64character
publicMailEncoder(Mailmail){
this.mail=mail;
try{
digest=MessageDigest.getInstance("MD5");//MD5is16bitmessagedigest
//Debugtheencoder
FileWriterout=newFileWriter("out.eml");
out.write(encode());
out.close();
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicStringencode(){
Stringresult="";
byte[]data;
digest.update(((newRandom()).nextInt()+"").getBytes());
data=digest.digest();//Getaromdomintegerandgetitsdigest
StringmainBoundary="****MAIN_BOUNDARY****"+byte2hex(data);
//Mainboundary
StringsubBoundary="****SUB_BOUNDARY****"+byte2hex(data);
//Subboundary
result+="To:"+encodeText(mail.to)+"
"
+"From:"+encodeText(mail.from)+"
"
//Nowweonlysupportchinese
+"Subject:"+encodeText(mail.subject)+"
";
//Themailpriority
if(mail.priority==Mail.LOW_PRIORITY)
{
result+="X-Priority:5
";
result+="X-MSMail-Priority:Low
";
}
elseif(mail.priority==Mail.HIGH_PRIORITY)
{
result+="X-Priority:1
";
result+="X-MSMail-Priority:High
";
}
elseif(mail.priority==Mail.NORMAL_PRIORITY)
{
result+="X-Priority:3
";
result+="X-MSMail-Priority:Normal
";
}
result+="MIME-Version:1.0
"
+"Content-Type:multipart/mixed;
"
+" boundary=""+mainBoundary+""
"
+"
"
+"Thisisamulti-partmessageinMIMEformat.
"
+"
"
//Aboveismailsheader
+"--"+mainBoundary+"
"
//Themailstextbody
+"Content-Type:multipart/alternative;
"
+" boundary=""+subBoundary+""
"
+"
"
+"--"+subBoundary+"
";
//Mailformat(textorHTML)
if(mail.format==Mail.TEXT){
result+="Content-Type:text/plain;
";
}elseif(mail.format==Mail.HTML){
result+="Content-Type:text/html;
";
}
result+=" charset="gb2312"
"
+"Content-Transfer-Encoding:base64
"
+"
"
+encoder.encode(mail.body.getBytes())+"
"
+"
"
+"--"+subBoundary+"--
"//Endofsubboundary
+"
";
//Startattachfiles
if(mail.attachments!=null)
for(inti=0;i<mail.attachments.size();i++){
Attachmentattachment=(Attachment)mail.attachments.elementAt(i);
result+="--"+mainBoundary+"
"
+"Content-Type:"+getMimeType(attachment.getFileName())+";
"
+" name=""+encodeText(attachment.getFileName())+""
"
+"Content-Transfer-Encoding:base64
"
+"Content-Disposition:attachment;
"
+" filename=""+encodeText(attachment.getFileName())+""
"
+"
"
+encoder.encode(attachment.getFileData())+"
"
+"
";
}
result+="--"+mainBoundary+"--";
//Endofmainboundaryandthemail
returnresult;
}
privateStringgetMimeType(StringfileName)
{
MimetypesFileTypeMapmap=newMimetypesFileTypeMap();
returnmap.getContentType(fileName);
}
/**
*Convertthebytestohexadecimalstring.
*/
publicStringbyte2hex(byte[]b)
{
Stringhs="";
Stringstmp="";
for(intn=0;n<b.length;n++)
{
stmp=(Integer.toHexString(b[n]&0XFF));
if(stmp.length()==1)hs=hs+"0"+stmp;
elsehs=hs+stmp;
}
returnhs.toUpperCase();
}
publicStringencodeText(Stringtext){
returnencodeText(text,"gb2312");
}
publicStringencodeText(Stringtext,Stringencoding){
booleanisAllAscii=true;
for(inti=0;i<text.length();i++){
if(text.charAt(i)<=||text.charAt(i)>=~){
isAllAscii=false;
break;
}
}
if(isAllAscii)returntext;
try{
byte[]bytes=text.getBytes();
if(encoding==null||encoding.length()==0){
encoding="gb2312";
}
bytes=text.getBytes(encoding);
return"=?"+encoding+"?B?"+encoder.encode(bytes)
+"?=";
}catch(Exceptionex){
returntext;
}
}
publicstaticvoidmain(String[]args){
Mailmail=newMail("server","发件人","收件人","主题",
"注释",null,Mail.HIGH_PRIORITY,Mail.HTML);
newMailEncoder(mail);
}
}
packagedfh;
importjava.io.*;
/**
*供应附件撑持,保留一个附件的名字和数据.
*/
publicclassAttachmentimplementsSerializable{
/**
*附件文件名
*/
privateStringfileName;
/**
*附件文件数据
*/
privatebyte[]data;
/**
*给定当地文件读取文件内容并设置文件名和数据.
*/
publicAttachment(StringfileName){
try{
FileInputStreamin=newFileInputStream(fileName);
data=newbyte[in.available()];
in.read(data);
in.close();
Filefile=newFile(fileName);
fileName=file.getAbsolutePath();
}catch(Exceptionex){
System.out.println("Notavalidattachmentfile.");
ex.printStackTrace();
data=null;
}
charseparator=File.separatorChar;
if(fileName.lastIndexOf(separator)>=0)
setFileName(fileName.substring(fileName.lastIndexOf(separator)+1));
elsesetFileName(fileName);
}
/**
*依据已知附件文件名和数据机关附件.
*/
publicAttachment(StringfileName,byte[]data){
setFileName(fileName);
setFileData(data);
}
publicAttachment(){}
/**
*前往附件文件名.
*/
publicStringgetFileName(){
returnthis.fileName;
}
/**
*设置附件文件名.
*/
publicvoidsetFileName(StringfileName){
this.fileName=fileName;
}
/**
*前往附件数据.
*/
publicbyte[]getFileData(){
returnthis.data;
}
/**
*设置附件数据.
*/
publicvoidsetFileData(byte[]data){
this.data=data;
}
/**
*前往附件形貌字符串.
*/
publicStringtoString(){
returngetFileName()+"("+getFileData().length+"字节)";
}
}
你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。 |
|