📄 getemailinfo.java
字号:
import java.io.*;
import java.text.*;
import java.util.*;
import java.sql.*;
import java.util .Vector ;
import java.net.*;
public class GetEmailInfo
{
public String sCookie="";
int nIndex = 0;
static String key1 = "from:";
static String key2 = "date:";
static String key3 = "size:";
static String key4 = "subject:";
static String key5 = "new";
static String key6 = "old";
static String key7 = "url:";
static String key8 = "附件";
static String lineEnd = "\n";
GetEmailInfo(){
nIndex = 0;
}
//get all email by perl cgi;
public Vector getEmailInfoFromPerlCGI( String user,String pass ){
nIndex = 0;
Vector vector = new Vector (1,5);
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&JAVAMAIL1=OK");
String sEmail = getCGIReturn(sPerCGI);
if( sEmail!=null ){
String sEMFile = CommonMethods.getUserEmailFile (user);
File file = new File(sEMFile);
if(file.exists ())
file.delete ();
CommonMethods.WriteToDisk(sEmail.getBytes (),sEMFile);
//split keyword
nIndex=0;
EmailInfo EM =getEmail(sEmail);
while( EM !=null ){
vector.addElement (EM);
EM =getEmail(sEmail);
}
}
return vector;
}
public Vector getEmailInfoFromFile( String user ){
nIndex = 0;
Vector vector = new Vector (1,5);
String sEmail = CommonMethods.ReadFromDisk(CommonMethods.getUserEmailFile (user));
if(sEmail==null)
return vector;
EmailInfo EM =getEmail(sEmail);
while( EM !=null ){
vector.addElement (EM);
EM =getEmail(sEmail);
}
return vector;
}
EmailInfo getEmail(String sMailInfo ){
EmailInfo EM = new EmailInfo();
int nBegin ,nEnd ;
//get from;
nBegin = sMailInfo.indexOf (key1,nIndex) + key1.length ();
if( nBegin<key1.length () ){
return null;
}
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.setFrom ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get date;
nBegin = sMailInfo.indexOf (key2,nIndex) + key2.length ();
if( nBegin<key2.length () ){
return null;
}
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.setDate ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get size;
nBegin = sMailInfo.indexOf (key3,nIndex) + key3.length ();
if( nBegin<key3.length () ){
return null;
}
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.setSize ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get subject;
nBegin = sMailInfo.indexOf (key4,nIndex) + key4.length ();
if( nBegin<key4.length () ){
return null;
}
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.setSubject ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
nIndex += 2;
//get old or new;
nBegin = sMailInfo.indexOf(key5);
if( nBegin == 0 )
EM.setNew (true);
else
EM.setNew (false);
nIndex += key5.length ();
nBegin = sMailInfo.indexOf(key8,nIndex);
nEnd = sMailInfo.indexOf(lineEnd,nIndex);
if( nBegin==-1||nBegin>nEnd ){
EM.setAttachFlag(false);
}
//get url;
nBegin = sMailInfo.indexOf (key7,nIndex) + key7.length ();
if( nBegin<key7.length () ){
return null;
}
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.setUrl ( cutUrl(CommonMethods.GetMailHost()+sMailInfo.substring (nBegin,nEnd)) );
nIndex = nEnd;
return EM;
}
String cutUrl(String url)
{
if(url==null)
return null;
if(url==null)
return null;
int nMid = url.indexOf ("USERNAME");
int nBegin = url.indexOf ("JAVAMAIL2:");
return url.substring (0,nMid) + url.substring(nBegin,url.length ());
}
String dealWithUrl(String sMailUrl)
{
if(sMailUrl==null)
return null;
String sReturn="";
for(int i=0;i<sMailUrl.length ();i++ ){
char code = sMailUrl.charAt (i);
if( code=='#' )
sReturn += "%23";
if( code=='&' )
sReturn += "%26";
else if(code=='%')
sReturn += "%25";
else
sReturn += code;
}
return sReturn;
}
public Email getEmailContentFromPerlCGI( String user,String pass ,String sUrl ){
nIndex = 0;
//first, we get the email info;
sUrl += ("&USERNAME="+user);
sUrl += ("&PASSWORD="+pass);
String sEmail = getCGIReturn(sUrl);
if( sEmail==null )
return null;
Email EM = new Email();
EM.SetMailData ( new MailBuff ( sEmail.getBytes () ) );
EmailTranslator ET=new EmailTranslator ();
if( ET.Translate (EM) ==true )
return EM;
return null;
}
public Vector ListMailFolder( String user , String pass )
{
nIndex = 0;
Vector folders= new Vector (5,5);
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&JAVAMAIL3=ok");
sPerCGI = getCGIReturn(sPerCGI);
if( sPerCGI==null )
return folders;
//get all mail box ;
MailFolder MF= getFolder(sPerCGI);
while( MF !=null ){
folders.addElement (MF);
MF =getFolder(sPerCGI);
}
return folders;
}
public Vector ListMailsInFolder( String user , String pass , String sFolder )
{
nIndex = 0;
Vector mails= new Vector (5,5);
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&JAVAMAIL4="+sFolder);
sPerCGI = getCGIReturn(sPerCGI);
if( sPerCGI==null )
return mails;
EmailInfo EM =getEmail(sPerCGI);
while( EM !=null ){
mails.addElement (EM);
EM =getEmail(sPerCGI);
}
return mails;
}
//
public boolean DelMail( String user , String pass , String sMail )
{
String sPerCGI = CommonMethods.GetUrl365Mail();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&DELETE:"+sMail);
getCGIReturn(sPerCGI);
return true;
}
public boolean MoveMail( String user , String pass , String sMail
,String sFolder)
{
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&MOVE:"+sMail);
sPerCGI += ("&FOLDERTRANSFERLIST="+sFolder);
getCGIReturn(sPerCGI);
return true;
}
public boolean MoveMails( String user , String pass , String[] sMails
,String sFolder)
{
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
for( int i=0;i<sMails.length ;i++ )
sPerCGI += ("&MOVE:"+sMails[i]+"=TRUE");
sPerCGI += ("&FOLDERTRANSFERLIST="+sFolder);
getCGIReturn(sPerCGI);
return true;
}
public boolean CreateFolder( String user , String pass , String sFolder )
{
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&FOLDERNEWNAME="+sFolder);
sPerCGI += ("&FOLDERNEW="+"新建");
getCGIReturn(sPerCGI);
return true;
}
public boolean DelFolder( String user , String pass , String sFolder )
{
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&FOLDERDELETE="+sFolder);
getCGIReturn(sPerCGI);
return true;
}
public boolean EmptyFolder( String user , String pass , String sFolder )
{
String sPerCGI = CommonMethods.GetUrl365Mail ();
//first, we get the email info;
sPerCGI += ("?USERNAME="+user);
sPerCGI += ("&PASSWORD="+pass);
sPerCGI += ("&FOLDEREMPTY="+sFolder);
getCGIReturn(sPerCGI);
return true;
}
public String getCGIReturn( String sCGI )
{
byte[] read ;
MailBuff mb = new MailBuff (MailBuff.INCREASE_SIZE ,
MailBuff.INCREASE_SIZE);
try{
URL myurl = new URL( sCGI);
HttpURLConnection myconn=
(HttpURLConnection)myurl.openConnection();
DataInputStream myInput = new DataInputStream
( myconn.getInputStream());
read =new byte[10240];
int nSize ;
while( ( nSize= myInput.read(read) )>0 )
mb.append (read,0,nSize);
myInput.close ();
}catch(Exception e){
return null;
}
return new String(mb.getData (),0,mb.getSize ());
}
static String sF1 = "name1:";
static String sF2 = "name2:";
static String sF3 = "sum:";
static String sF4 = "new1:";
static String sF5 = "new2:";
MailFolder getFolder(String sMailInfo ){
MailFolder EM = new MailFolder();
int nBegin ,nEnd ;
//get from;
nBegin = sMailInfo.indexOf (sF1,nIndex) + sF1.length ();
if( nBegin<sF1.length () )
return null;
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.sName = ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get date;
nBegin = sMailInfo.indexOf (sF2,nIndex) + sF2.length ();
if( nBegin<sF2.length () )
return null;
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.sID = ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get size;
nBegin = sMailInfo.indexOf (sF3,nIndex) + sF3.length ();
if( nBegin<sF3.length () )
return null;
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.nMail = ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
//get subject;
nBegin = sMailInfo.indexOf (sF4,nIndex) + sF4.length ();
if( nBegin<sF4.length () )
return null;
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.nNew = ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
nBegin = sMailInfo.indexOf (sF5,nIndex) + sF5.length ();
if( nBegin<sF5.length () )
return null;
nEnd = sMailInfo.indexOf(lineEnd,nBegin);
if(nEnd == -1)
nEnd = sMailInfo.length ();
EM.nNotRead = ( sMailInfo.substring (nBegin,nEnd) );
nIndex = nEnd;
if( EM.sID.compareTo ("INBOX")==0 || EM.sID.compareTo ("SENT")==0 ||
EM.sID.compareTo ("TRASH")==0 || EM.sID.compareTo ("DRAFT")==0 )
EM.isNormal=true;
else
EM.isNormal=false;
return EM;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -