package com.wgh.dao;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.UIDFolder;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.search.AndTerm;
import javax.mail.search.MessageIDTerm;
import javax.mail.search.NotTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import sun.io.ByteToCharUTF8;
import com.sun.mail.pop3.POP3Folder;
import com.wgh.model.ReceiveEmailForm;
import com.wgh.tools.ConnDB;
import com.wgh.tools.StringUtils;
public class ReceiveEmailDAO {
private ConnDB conn = new ConnDB();
private String contentMail = "";
// 建立与邮件接收服务器的连接
public Store connectStore(String host, String username, String password) {
String protocol = "pop3";
Properties prop = new Properties(); //实例化Properties类
prop.setProperty("mail.store.protocol", "pop3"); //指定采用POP3协议接收邮件
prop.setProperty("mail.pop3.host", host); //指定POP3服务器
Session mailSession = Session.getDefaultInstance(prop, null); //创建Session
mailSession.setDebug(false); //设置调试标志为false,表示不调试
Store store = null;
try {
store = mailSession.getStore(protocol); //获取store对象
store.connect(host, username, password); //建立与邮件服务器的连接
// System.out.println("已经成功建立连接");
} catch (Exception e) {
e.printStackTrace();
if(null!=store){
if(store.isConnected()){ //当store已经连接
try {
store.close(); //关闭与邮件服务器的连接
} catch (MessagingException e1) {
e1.printStackTrace();
}
}
store=null;
}
}
return store;
}
// 查询已读邮件的方法
public String queryIsRead(String addressee) {
String messageId = "";
String sql = "SELECT * FROM tb_isRead WHERE addressee='" + addressee
+ "'";
ResultSet rs = conn.executeQuery(sql);
try {
while (rs.next()) {
messageId = messageId + rs.getString(2) + ",";
}
} catch (SQLException e) {
e.printStackTrace();
}
conn.close();
return messageId;
}
// 获取邮件内容
public List<ReceiveEmailForm> showEmail(String host, String username,
String password, String flag) {
List<ReceiveEmailForm> list = new ArrayList<ReceiveEmailForm>();
try {
Store store = connectStore(host, username, password);// 建立与邮件接收服务器的连接
POP3Folder folder = (POP3Folder) store.getFolder("inbox");
folder.open(Folder.READ_WRITE); //打开邮件夹
// 查询发件人为wgh@sina.com的邮件
// SearchTerm st=new FromStringTerm("wgh@wgh.com");
// Message[] message=folder.search(st);
// 查询未读邮件
// Flags flagss=new Flags(Flags.Flag.SEEN);
// FlagTerm st=new FlagTerm(flagss,flag);
//
// Message[] message=folder.search(st);
// *************************************************
FetchProfile profile = new FetchProfile();
// profile.add(UIDFolder.FetchProfileItem.UID);
// profile.add(FetchProfile.Item.ENVELOPE);
Message[] message = null;
String str_messageId = queryIsRead(username); // 已经阅读的邮件ID组成的字符串
if (!"".equals(str_messageId)) {
String[] isRead = str_messageId.split(",");
if ("0".equals(flag)) { // 未读邮件
// 生成搜索条件
NotTerm[] arrIsRead = new NotTerm[isRead.length];
for (int i = 0; i < isRead.length; i++) {
arrIsRead[i] =new NotTerm(new MessageIDTerm(isRead[i]));
}
SearchTerm st = new AndTerm(arrIsRead);
message = folder.search(st);
folder.fetch(message, profile);
} else { // 已读邮件
// 生成搜索条件
MessageIDTerm[] arrIsRead = new MessageIDTerm[isRead.length];
for (int i = 0; i < isRead.length; i++) {
arrIsRead[i] = new MessageIDTerm(isRead[i]);
}
SearchTerm st = new OrTerm(arrIsRead);
message = folder.search(st);
folder.fetch(message, profile);
}
} else {
if ("0".equals(flag)) { // 未读邮件
message = folder.getMessages(); // 获取全部邮件
folder.fetch(message, profile);
}
}
String mail_content = "";
String mail_attach = "";
Message messageI = null;
/******************************循环读取邮件信息******************************/
if(message!=null){
for (int i = message.length - 1; i >= 0; i--) {
MimeMessage m = (MimeMessage) message[i];
ReceiveEmailForm f = new ReceiveEmailForm();
messageI = message[i];
f.setMessageId(m.getMessageID()); // 邮件ID
f.setAddresser(messageI.getFrom()[0].toString()); // 发件人
/*************获取邮件主题**********************************************/
MimeMessage part=(MimeMessage) message[i];
String head=part.getHeader("SUBJECT")[0]; //获取邮件的头
if(head.toLowerCase().startsWith("=?gb")){ //获取GBK或GB2312编码的邮件主题
f.setTitle(messageI.getSubject());
}else{
f.setTitle(new String(messageI.getSubject().getBytes("ISO-8859-1"),"GBK")); // 邮件主题
}
/********************************************************************/
f.setSendTime(messageI.getSentDate().toLocaleString()); // 发送时间
String[] obj = getMailAttach(part, i); //调用获取邮件附件的方法
message[i].getFlags().getSystemFlags().toString();
mail_content = obj[0]; //邮件内容
mail_attach = obj[1]; //邮件附件
if (mail_attach == null || mail_attach.equals("")) {
mail_attach = "0";
} else {
mail_attach = "1";
}
f.setAdjunct(mail_attach); // 附件
list.add(f); // 将邮件信息保存到List集合中
}
}
/***********************************************************************/
folder.close(false); //关闭邮件夹
store.close(); //关闭与邮件服务器的连接
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//插入已读标记
public void inIsRead(String messageId,String addressee){
//将该邮件的MessageID插入到tb_isRead数据表中,表示该邮件已读
String sql="INSERT INTO tb_isRead (messageId,addressee) VALUES('"+messageId+"','"+addressee+"')";
System.out.println("插入已读邮件:"+sql);
conn.executeUpdate(sql);
}
// 获取邮件的附件信息
public String[] getMailAttach(Part part, int emailv) throws Exception {
String contenttype = part.getContentType();
int nameindex = contenttype.indexOf("name");
String fileName1 = "";
String fileName = "";
boolean conname = false;
if (nameindex != -1)
conname = true;
if(part.isMimeType("multipart/report")){ //获取UTF-7编码的邮件内容
contentMail=decodeStream(part.getInputStream(),"UTF-7");
}else{
if ((part.isMimeType("text/plain") && !conname) || (part.isMimeType("text/html") && !conname)) {
if(part.isMimeType("text/plain; CHARSET=US-A")){ //获取ISO-8859-1编码的邮件内容
contentMail =new String(part.getContent().toString().getBytes("ISO-8859-1"),"GBK");
}else if (part.getContent().toString().length() > 0) {
contentMail =part.getContent().toString();
}
}else if (part.isMimeType("multipart/*") ) {
Multipart multipart = (Multipart) part.getContent();
// 获取附件名称(可能包含多个附件)
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart mpart = multipart.getBodyPart(j);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE)))) {
fileName = mpart.getFileName();
StringUtils su=new StringU
没有合适的资源?快使用搜索试试~ 我知道了~
基于java开发的邮箱系统
共94个文件
gif:23个
jsp:19个
java:15个
5星 · 超过95%的资源 需积分: 50 39 下载量 55 浏览量
2011-05-26
14:41:06
上传
评论
收藏 1.56MB ZIP 举报
温馨提示
基于Java的邮箱系统的建立,同时含有收发件等各种功能的实现。所运用的数据库是sql server2000,主要是structs的技能,同时含有jax的技术,同是附带有文档说明和数据库说明。
资源推荐
资源详情
资源评论
收起资源包目录
17.zip (94个子文件)
17
sl
01
WEB-INF
web.xml 2KB
lib
classes
com
connDB.properties 422B
wgh
dao
ReceiveEmailDAO.class 12KB
AddressListDAO.class 4KB
SendMailDAO.class 7KB
action
SendMailServlet.class 6KB
AddressListServlet.class 4KB
ReceiveEmailServlet.class 9KB
DealAttach.class 3KB
model
IsReadForm.class 977B
TypeForm.class 761B
SendMailForm.class 2KB
AddressListForm.class 2KB
ReceiveEmailForm.class 2KB
tools
ConnDB.class 4KB
StringUtils.class 1KB
MyPagination.class 3KB
index.jsp 2KB
upFile_deal.jsp 829B
JS
AjaxRequest.js 1KB
function.js 2KB
emailList.jsp 5KB
Database
db_email_Log.LDF 1024KB
db_email_Data.MDF 1024KB
addressList_list.jsp 4KB
top.jsp 793B
writeMail.jsp 12KB
error.jsp 1KB
safe.jsp 309B
src
com
connDB.properties 422B
wgh
dao
SendMailDAO.java 6KB
ReceiveEmailDAO.java 12KB
AddressListDAO.java 3KB
action
ReceiveEmailServlet.java 10KB
DealAttach.java 1KB
AddressListServlet.java 4KB
SendMailServlet.java 6KB
model
ReceiveEmailForm.java 1KB
TypeForm.java 353B
SendMailForm.java 2KB
IsReadForm.java 541B
AddressListForm.java 1KB
tools
StringUtils.java 1KB
MyPagination.java 2KB
ConnDB.java 3KB
left.jsp 4KB
main.jsp 1KB
deal.jsp 159B
showEmailNumber.jsp 130B
Thumbs.db 6KB
sendLog.jsp 4KB
blank.htm 438B
Css
style.css 2KB
images
login_btn_bg1.gif 746B
UBB
left.gif 69B
center.gif 70B
right.gif 70B
U.gif 92B
I.gif 86B
Thumbs.db 8KB
B.gif 90B
error_b.gif 2KB
login_center_bg.gif 22KB
upFile_bg.gif 20KB
addressList_title.gif 4KB
bottom_bg.jpg 829B
left_bg.GIF 354B
error.jpg 27KB
login_btn_bg.gif 911B
jian.gif 746B
btn_bg.jpg 14KB
login_top.jpg 80KB
bottom_line.jpg 17KB
center_b.gif 10KB
adjunct.gif 154B
Thumbs.db 40KB
left_title.GIF 3KB
bottom.JPG 100KB
jian_null.gif 1KB
left_subTitle.JPG 3KB
b_top3.gif 72KB
main_bg.gif 2KB
jia.gif 1KB
top.jpg 80KB
main_title.gif 3KB
folder.gif 533B
main_center.jpg 51KB
linkman_add.jsp 3KB
copyright.jsp 1KB
color.jsp 2KB
sendMail_ok.jsp 364B
logout.jsp 380B
upFile.jsp 1KB
程序使用说明.doc 915KB
共 94 条
- 1
资源评论
- 丹之枫叶2012-12-11很好,很强大,从中获益了。
- zhengbingqun2013-12-03很不错,学习了很多
- hhhhhh12232013-07-26很强大,能有mysql数据库就更好了
- greentoma172012-08-14很强大,功能简单但思路很清晰,可惜导入到eclipse中用不了 有些细节不太明白,个人原因,资源还是很好的
abczhenyizhi
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
- 粒子群算法优化BP神经网络PSO-BP的MATLAB代码(数值预测)
- 基于Springboot的一起看书平台.zip
- 无头单向非循环链表的实现(SList.h)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功