Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 收POP EMAIL 的程序
David_Zhuo





发贴: 16
积分: 0
于 2002-12-19 14:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Beef 于 2002-10-22 19:34:31 加贴:

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

/**
* Title:
* Description:
* Copyright: Copyright Coffee 2002
* Company:
* @author
* @version 1.0
*/

public class GetPopEmail {

private String protocol = "pop3";
private String host = "";
private String user = "";
private String password = "";
private String attachFileSavePath = "";

private boolean isDeleteEmailAfterGetFlag = false;
// public GetPopEmail() {
// }

public GetPopEmail(String host, String user,String password,
String attachFileSavePath)
{
this.host = host;
this.user = user;
this.password = password;
this.attachFileSavePath = attachFileSavePath;
}

public void setDeleteEmailAfterGetFlag(boolean isDeleteEmailAfterGetFlag)
{
this.isDeleteEmailAfterGetFlag = isDeleteEmailAfterGetFlag;
}

public Collection getAllEmail()
{
System.out.println("getPopEmail: start");

ArrayList arlstEmailModels = new ArrayList();
EmailModel emailModel = null;

String url = null;
String root = null;
boolean debug = false;

Store store = null;
javax.mail.Folder rf = null;
javax.mail.Folder rootFolder = null;
javax.mail.Folder inbox = null;

try
{

// Get a Properties object
Properties props = System.getProperties();

// Get a Session object
// javax.mail.Session session = Session.getDefaultInstance(new Properties(), null);
javax.mail.Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);

// Get a Store object
store = session.getStore(protocol);
// Logger.log.log(Logger.APP1,this,"getAllEmail","host=" + host + "<<<<");
// Logger.log.log(Logger.APP1,this,"getAllEmail","user=" + user + "<<<<");
// Logger.log.log(Logger.APP1,this,"getAllEmail","password=" + password + "<<<<");
store.connect(host, 110, user, password);

//
// フォルダをひらく
//
rootFolder = store.getDefaultFolder();
inbox = rootFolder.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
System.out.println("メール:" + inbox.getMessageCount() + "枚");
javax.mail.Message[] allMsg = inbox.getMessages(); // 全部取得

for (int i = 0; i < allMsg.length; i++ )
{
System.out.println("----");
System.out.println("No." + Integer.toString(i + 1));
System.out.println("----");
System.out.println("Subject : " + allMsg[i].getSubject());
System.out.println("Date : " + allMsg[i].getSentDate());
emailModel = new EmailModel();

String strSubject = allMsg[i].getSubject();
java.util.Date sentDate = allMsg[i].getSentDate();
java.util.Date recieveDate = allMsg[i].getReceivedDate();

emailModel.setSubject(strSubject);
if(sentDate != null)
{
emailModel.setSentDate(new java.sql.Timestamp(sentDate.getTime()));
}
if(recieveDate != null)
{
emailModel.setRecieveDate(new java.sql.Timestamp(recieveDate.getTime()));
}

Address[] from = allMsg[i].getFrom();
Address[] to = null;
try
{
to = allMsg[i].getRecipients(javax.mail.Message.RecipientType.TO);
}
catch(javax.mail.internet.AddressException addressE)
{
addressE.printStackTrace();
}

String[] strFromAddress = new String[from.length];
String[] strToAddress = null;
for(int i1=0;i1<from.length;i1++)
{
strFromAddress[i1] = from[i1].toString();
}
if(to != null)
{
strToAddress = new String[to.length];
for(int i2=0;i2<to.length;i2++)
{
strToAddress[i2] = to[i2].toString();
}
}

emailModel.setAttachSaveFilePath(this.attachFileSavePath);
emailModel.setFromAddress(strFromAddress);
emailModel.setToAddress(strToAddress);

if (from != null)
{
for (int n = 0; n < from.length; n++)
{
System.out.println("From : " + from[n]);
}
}
if (to != null)
{
for (int n = 0; n < to.length; n++)
{
System.out.println("To : " + to[n]);
}
}
System.out.println();
Object body = null;
System.out.println("allMsg[i].getContentType()=" + allMsg[i].getContentType());
System.out.println("allMsg[i].getDisposition()=" + allMsg[i].getDisposition());

///////////// get content ////////////////////////////
body = allMsg[i].getContent();
ArrayList arlstAttachFileName = new ArrayList();
if(body instanceof String)
{
System.out.println("This is a normal email that has no attach file");
System.out.println(body.toString());
emailModel.setContent((String)body);
}
else if (body instanceof Multipart)
{
System.out.println("This is a multipart email.body.getClass().getName()=" + body.getClass().getName());
Multipart mp = (Multipart)body;
int count3 = mp.getCount();
System.out.println("It has " + count3 + " BodyParts in it**");

for (int j = 0; j < count3; j++) {
// Part are numbered starting at 0
BodyPart b = mp.getBodyPart(j);
String mimeType = b.getContentType();
System.out.println( "BodyPart " + (j + 1) +
" is of MimeType " + mimeType);
Object o2 = b.getContent();
if (o2 instanceof String) {
System.out.println("**This is a String BodyPart**");
System.out.println((String)o2);
emailModel.setContent((String)o2);
}
else if (o2 instanceof Multipart) {
System.out.print(
"**This BodyPart is a nested Multipart. ");
Multipart mp2 = (Multipart)o2;
int count2 = mp2.getCount();
System.out.println("It has " + count2 +
"further BodyParts in it**");
}
else if (o2 instanceof InputStream)
{
System.out.println("b.getFileName()=" + allMsg[i].getFileName());
System.out.println(
"**This is an InputStream BodyPart**");
String strFileName = saveAttachFile((InputStream)o2,b.getFileName());
if(o2 != null)
{
try
{
((InputStream)o2).close();
}
catch(java.io.IOException ioe1){}
}
arlstAttachFileName.add(strFileName);
}
} //End of for
}
else if (body instanceof InputStream)
{
System.out.println("content is Inputsteam");
System.out.println("b.getFileName()=" + allMsg[i].getFileName());
System.out.println(
"**This is an InputStream BodyPart**");
String strFileName = saveAttachFile((InputStream)body,allMsg[i].getFileName());
if(body != null)
{
try
{
((InputStream)body).close();
}
catch(java.io.IOException ioe1){}
}
arlstAttachFileName.add(strFileName);
}
//////////////////////////////

String[] strAttachFileName = new String[arlstAttachFileName.size()];
for(int i3=0;i3<arlstAttachFileName.size();i3++)
{
strAttachFileName[i3] = (String)arlstAttachFileName.get(i3);
}
emailModel.setAttachSaveFileNames(strAttachFileName);
arlstEmailModels.add(emailModel);
if(this.isDeleteEmailAfterGetFlag)
{
allMsg[i].setFlag(Flags.Flag.SEEN,true);
}
else
{
allMsg[i].setFlag(Flags.Flag.SEEN,true);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
inbox.close(true);
store.close();
}
catch(javax.mail.MessagingException messgingE)
{}
}
System.out.println("getPopEmail: end");
return arlstEmailModels;
}

private String saveAttachFile(InputStream is,String strOriginFileName)
{
System.out.println("saveAttach:start");
String strExtFileName = ".gif";
int index = 0;
if(strOriginFileName != null)
{
index = strOriginFileName.lastIndexOf(".");
strExtFileName = strOriginFileName.substring(index);
}
GregorianCalendar greCalendTmp = new GregorianCalendar();
String strFileName = Integer.toString(greCalendTmp.get(GregorianCalendar.YEAR))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.MONTH))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.DAY_OF_MONTH))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.HOUR_OF_DAY))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.MINUTE))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.SECOND))
+ Integer.toString(greCalendTmp.get(GregorianCalendar.MILLISECOND))
+ strExtFileName;
FileOutputStream fosAttach = null;
InputStream isAttach = null;
byte[] byTmp = new byte[1024];
int iReaded = 0;
try
{
File fileAttach = new File(this.attachFileSavePath,strFileName);
if(!fileAttach.exists())
{
fileAttach.createNewFile();
}
fosAttach = new FileOutputStream(fileAttach);
while(true)
{
iReaded = is.read(byTmp,0,1024);
if(iReaded == -1)
{
break;
}
fosAttach.write(byTmp,0,iReaded);
}
}
catch(java.io.IOException ioE)
{
ioE.printStackTrace();
}
finally
{
if(fosAttach != null)
{
try
{
fosAttach.close();
}
catch(java.io.IOException ioe1){}
}
}
return strFileName;
}

public class EmailModel implements java.io.Serializable
{
private String[] strFromAddress = null;
private String[] strToAddress = null;
private String strSubject = "";
private String strContent = "";
private String[] strAttachSaveFileNames = null;
private String strAttachSaveFilePath = "";
private java.sql.Timestamp recieveDate = null;
private java.sql.Timestamp sentDate = null;

public EmailModel()
{
}

public void setFromAddress(String[] strFromAddress)
{
this.strFromAddress = strFromAddress;
}
public void setToAddress(String[] strToAddress)
{
this.strToAddress = strToAddress;
}
public void setContent(String strContent)
{
this.strContent = strContent;
}
public void setAttachSaveFileNames(String[] strAttachSaveFileNames)
{
this.strAttachSaveFileNames = strAttachSaveFileNames;
}
public void setAttachSaveFilePath(String strAttachSaveFilePath)
{
this.strAttachSaveFilePath = strAttachSaveFilePath;
}
public void setSubject(String strSubject)
{
this.strSubject = strSubject;
}
public void setSentDate(java.sql.Timestamp sentDate)
{
this.sentDate = sentDate;
}
public void setRecieveDate(java.sql.Timestamp recieveDate)
{
this.recieveDate = recieveDate;
}

public String[] getFromAddress()
{
return this.strFromAddress;
}
public String[] getToAddress()
{
return this.strToAddress;
}
public String getContent()
{
return this.strContent;
}
public String[] getAttachSaveFileNames()
{
return this.strAttachSaveFileNames;
}
public String getAttachSaveFilePath()
{
return this.strAttachSaveFilePath;
}
public String getSubject()
{
return this.strSubject;
}
public java.sql.Timestamp getSentDate()
{
return this.sentDate;
}
public java.sql.Timestamp getRecieveDate()
{
return this.recieveDate;
}

}
}




为什么这个Applet不能在浏览器显示啊?

话题树型展开
人气 标题 作者 字数 发贴时间
4525 收POP EMAIL 的程序 David_Zhuo 12999 2002-12-19 14:01

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923