📄 mailitem.java
字号:
import java.io.File;
import java.io.FileInputStream;
public class MailItem
{
//content type;
static byte CONTENT_TYPE_PLAIN = 0;
static byte CONTENT_TYPE_ATTCH = 1;
//encode type;
static byte ENCODE_TYPE_NORMAL = 0;
static byte ENCODE_TYPE_BASE64 = 1;
static byte ENCODE_TYPE_PRINT = 2;
//charset
static byte CHARSET_TYPE_NORMAL = 0;
static byte CHARSET_TYPE_JIS = 1;
//members;
String sContentName;
byte nContentType;
byte nEncodeType;
byte nCharSet;
byte[] sContentIn;
byte[] sContentOut;
int nDataLength;
int nOutCodeLength;
String sFileName;
//initial
public MailItem(){
}
public MailItem( String sName,byte contentType,
byte encode,String sIn,String sFile){
SetData( sName, contentType,
encode, sIn, sFile);
}
//retrive data func.
public String GetContentName(){
return sContentName;
}
public void SetContentName(String name){
sContentName =name;
}
public byte GetContentType(){
return nContentType;
}
public void SetContentType(byte Type){
nContentType =Type;
}
public byte GetEncodeType(){
return nEncodeType;
}
public void SetEncodeType(byte Type){
nEncodeType =Type;
}
public byte GetCharset(){
return nCharSet;
}
public void SetCharset(byte Type){
nCharSet =Type;
}
public void SetContentIn(byte[] data){
sContentIn = data;
nDataLength = sContentIn.length ;
}
public byte[] GetContentIn(){
return sContentIn;
}
public void SetContentOut(byte[] data){
sContentOut = data;
}
public byte[] GetContentOut(){
return sContentOut;
}
public int GetContentInLength(){
return nDataLength;
}
public void SetContentOutLength(int nOut){
nOutCodeLength = nOut;
}
public int GetContentOutLength(){
return nOutCodeLength;
}
public String GetAttachFileName(){
return sFileName;
}
public void SetAttachFileName(String sFile){
sFileName =sFile;
}
public void SetData( String sName,byte contentType,
byte encode,String sIn,String sFile){
sContentName = sName;
nContentType = contentType;
nEncodeType = encode;
sFileName = sFile;
if( sIn!=null )
sContentIn= sIn.getBytes();
nDataLength =0;
if(sContentIn!=null)
nDataLength = sContentIn.length ;
else
nDataLength = 0;
}
//encode;
public boolean EncodeByIn(){
if( nContentType==CONTENT_TYPE_PLAIN )
return EncodeByInBuf();
else
return EncodeByAttach();
}
private boolean EncodeByInBuf(){
if( sContentIn==null || nDataLength==0 )
return true;
Encodeing Encode;
if( nEncodeType==ENCODE_TYPE_BASE64 ){
Base_64Coding b64 = new Base_64Coding();
Encode = b64;
}
else if( nEncodeType==ENCODE_TYPE_PRINT ){
Ascii_7Coding A7 = new Ascii_7Coding();
Encode = A7;
}
else{
NormalCoding Nc= new NormalCoding();
Encode = Nc;
}
nOutCodeLength = Encode.CalcEncodeLength (sContentIn,nDataLength);
if( nOutCodeLength <= 0 )
return true;
sContentOut = new byte[nOutCodeLength];
//encode now;
nOutCodeLength = 0;
nOutCodeLength += Encode.Encode ( sContentIn,sContentOut,
nDataLength ,0,0);
sContentIn = null;
return true;
}
private boolean EncodeByAttach(){
if( sFileName==null||sFileName.length () == 0 )
return false;
File tempFile=new File(sFileName);
if( tempFile.exists ()==false || tempFile.canRead ()==false )
return false;
nDataLength =(int)tempFile.length ();
try{
sContentIn = new byte[nDataLength];
FileInputStream tempFS = new FileInputStream (sFileName);
nDataLength = tempFS.read (sContentIn);
tempFS.close ();
}
catch( Exception e){
return false;
}
return EncodeByInBuf();
}
//decode;
public boolean DecodeByInBuf(){
if( sContentIn==null || nDataLength==0 )
return true;
Encodeing Encode;
if( nEncodeType==ENCODE_TYPE_BASE64 ){
Base_64Coding b64 = new Base_64Coding();
Encode = b64;
}
else if( nEncodeType==ENCODE_TYPE_PRINT ){
Ascii_7Coding A7 = new Ascii_7Coding();
Encode = A7;
}
else{
NormalCoding Nc= new NormalCoding();
Encode = Nc;
}
nOutCodeLength = nDataLength;
sContentOut = new byte[nOutCodeLength];
//encode now;
nOutCodeLength = 0;
nOutCodeLength += Encode.Decode ( sContentIn,sContentOut,
nDataLength ,0,0);
sContentIn = null;
return true;
}
//create send item header;
public String CreateHeaderForSend(){
StringBuffer sReturn=new StringBuffer();
//content type;
sReturn.append("Content-Type: ");
if( nContentType==CONTENT_TYPE_PLAIN )
sReturn.append("text/plain");
else{
sReturn.append("application/octet-stream");
if( sContentName!=null && sContentName.length ()!=0 ){
sReturn.append(";name=");
sReturn.append(sContentName);
}
}
sReturn.append("\r\n");
//code type;
sReturn.append("Content-Transfer-Encoding: ");
if( nEncodeType==ENCODE_TYPE_NORMAL )
sReturn.append("7Bit");
else if(nEncodeType==ENCODE_TYPE_BASE64)
sReturn.append("base64");
else if(nEncodeType==ENCODE_TYPE_PRINT )
sReturn.append("quoted-printable");
sReturn.append("\r\n");
//attach
if( nContentType!=CONTENT_TYPE_PLAIN ){
int nIndex = sFileName.lastIndexOf ('/');
int nIndex1 = sFileName.lastIndexOf (File.separatorChar );
if( nIndex < nIndex1 )
nIndex = nIndex1;
sReturn.append("Content-Disposition: attachment; filename=");
if( nIndex != -1 )
sReturn.append(sFileName.substring (nIndex+1,sFileName.length ()));
else
sReturn.append(sFileName);
sReturn.append("\r\n");
}
sReturn.append("\r\n");
return sReturn.toString() ;
}
public byte[] CreateBodyCodeForSend(){
if( EncodeByIn()==false)
return null;
if(sContentOut!=null)
return sContentOut;
else
return null;
}
public boolean HaveContent(){
if(nOutCodeLength<=0||sContentOut==null)
return false;
boolean haveCode=false;
for(int i=0;i<nOutCodeLength;i++){
if( sContentOut[i]!=0xa && sContentOut[i]!=0xd ){
haveCode = true;
break;
}
}
return haveCode;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -