|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。Oracle收购Sun后Java前途未卜。/*
*Author:tyfun
*DateTime:2003.01.10
*Package:com.westarsoft.mail
*/
packagecom.westarsoft.mail;
importjavax.mail.*;
importjavax.mail.internet.*;
importjava.util.*;
importjava.io.*;
publicclassGetMail{
publicstaticStringreceive(StringpopServer,StringpopUser,StringpopPassword){
StringmailContent="";
Storestore=null;
Folderfolder=null;
try{
Propertiesprops=System.getProperties();
Sessionsession=Session.getDefaultInstance(props,null);
store=session.getStore("pop3");
store.connect(popServer,popUser,popPassword);
folder=store.getDefaultFolder();
if(folder==null)thrownewException("Nodefaultfolder");
folder=folder.getFolder("INBOX");
if(folder==null)thrownewException("NoPOP3INBOX");
folder.open(Folder.READ_ONLY);
Message[]msgs=folder.getMessages();
for(intmsgNum=0;msgNum<msgs.length;msgNum++){
mailContent=mailContent+getMessage(msgs[msgNum])+"
";
}
}
catch(Exceptionex){
ex.printStackTrace();
}
finally{
try{
if(folder!=null)folder.close(false);
if(store!=null)store.close();
}
catch(Exceptionex2){
ex2.printStackTrace();
}
}
returnmailContent;
}
publicstaticStringgetMessage(Messagemessage){
StringmailContent=null;
try{
Stringfrom=((InternetAddress)message.getFrom()[0]).getPersonal();
if(from==null)from=((InternetAddress)message.getFrom()[0]).getAddress();
mailContent="FROM:"+from;
Stringsubject=message.getSubject();
mailContent=mailContent+"
"+"SUBJECT:"+subject;
PartmessagePart=message;
Objectcontent=messagePart.getContent();
if(contentinstanceofMultipart){
messagePart=((Multipart)content).getBodyPart(0);
mailContent=mailContent+"
"+"[MultipartMessage]";
}
mailContent=mailContent+"
"+"CONTENT:"+content.toString();
StringcontentType=messagePart.getContentType();
mailContent=mailContent+"
"+"CONTENT:"+contentType;
if(contentType.startsWith("text/plain")||contentType.startsWith("text/html")){
InputStreamis=messagePart.getInputStream();
BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
StringthisLine=reader.readLine();
while(thisLine!=null){
mailContent=mailContent+"
"+thisLine;
thisLine=reader.readLine();
}
}
}
catch(Exceptionex){
ex.printStackTrace();
}
returnmailContent;
}
}
<%
GetMailmail=newGetMail();
Stringcontent=mail.receive("pop3.server.com","user","password");
if((content.trim()==null)||(content.trim()=="")){
System.out.println("Nomail!");
}
else{
System.out.println("Yougotanewmail!");
}
%>
大型的应用一般不会用这些框架(因为性能考虑);开发人员根据需要选择用一些框架,也可以不选用框架;不用框架并不代表要自己写框架;修改框架的可能性更小。 |
|