javamail接收邮件的bean,可以正确的解析中文(转)(二)

//接上一文章 /**
* 获取日期显示格式
*/
public String getDateFormat(String format) throws Exception {
return this.dateformat;
}

/**
* 获得发件人的地址和姓名
*
* @throws Exception
*/
public String getFrom() throws Exception {
return getFrom(this.message);
}

public String getFrom(Message mimeMessage) throws Exception {
InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom();
String from = address[0].getAddress();
if (from == null)
from = "";
String personal = address[0].getPersonal();
if (personal == null)
personal = "";
String fromaddr = personal + "<" + from + ">";
return fromaddr;
}

/**
* 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同 * "to"----收件人 "cc"---抄送人地址
* "bcc"---密送人地址
*/
public String getTOAddress() throws Exception {
return getMailAddress("TO", this.message);
}

public String getCCAddress() throws Exception {
return getMailAddress("CC", this.message);
}

public String getBCCAddress() throws Exception {
return getMailAddress("BCC", this.message);
}

public String getTOAddress(Message mimeMessage) throws Exception {
return getMailAddress("TO", mimeMessage);
}

public String getCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("CC", mimeMessage);
}

public String getBCCAddress(Message mimeMessage) throws Exception {
return getMailAddress("BCC", mimeMessage);
}

public String getMailAddress(String type) throws Exception {
return getMailAddress(type, this.message);
}

public String getMailAddress(String type, Message mimeMessage)
throws Exception {
String mailaddr = "";
String addtype = type.toUpperCase();
InternetAddress[] address = null;
if (addtype.equals("TO") || addtype.equals("CC")
|| addtype.equals("BCC")) {
if (addtype.equals("TO")) {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.TO);
} else if (addtype.equals("CC")) {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.CC);
} else {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.BCC);
}
if (address != null) {
for (int i = 0; i < address.length; i++) {
String email = address[i].getAddress();
if (email == null)
email = "";
else {
email = MimeUtility.decodeText(email);
}
String personal = address[i].getPersonal();
if (personal == null)
personal = "";
else {
personal = MimeUtility.decodeText(personal);
}
String compositeto = personal + "<" + email + ">";
mailaddr += "," + compositeto;
}
mailaddr = mailaddr.substring(1);
}
} else {
throw new Exception("Error emailaddr type!");
}
return mailaddr;
}

/**
* 获得邮件主题
*/
public String getSubject() throws MessagingException {
return getSubject(this.message);
}

public String getSubject(Message mimeMessage) throws MessagingException {
String subject = "";
try {
subject = MimeUtility.decodeText(mimeMessage.getSubject());
if (subject == null)
subject = "";
} catch (Exception exce) {
}
return subject;
}

/**
* 获得邮件发送日期
*/
public String getSentDate() throws Exception {
return getSentDate(this.message);
}

public String getSentDate(Message mimeMessage) throws Exception {
Date sentdate = mimeMessage.getSentDate();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format.format(sentdate);
}

/**
* 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*/
public boolean getReplySign() throws MessagingException {
return getReplySign(this.message);
}

public boolean getReplySign(Message mimeMessage) throws MessagingException {
boolean replysign = false;
String needreply[] = mimeMessage
.getHeader("Disposition-Notification-To");
if (needreply != null) {
replysign = true;
}
return replysign;
}

/**
* 获得此邮件的Message-ID
*/
public String getMessageId() throws MessagingException {
return getMessageId(this.message);
}

public String getMessageId(Message mimeMessage) throws MessagingException {
return ((MimeMessage) mimeMessage).getMessageID();
}

/**
* 初始化出错邮件数组
*
*/
private void setRecordFailure() {
this.recordFailure = new boolean[getMessageCount()];
}

/**
* 返回出错数组
*
* @return
*/
public boolean[] getRecordFailure() {
return this.recordFailure;
}

/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew() throws MessagingException {
return isNew(this.message);
}

/**
* 判断此邮件是否已读,如果未读返回返回false,反之返回true
*/
public boolean isNew(Message mimeMessage) throws MessagingException {
boolean isnew = false;
Flags flags = mimeMessage.getFlags();
Flags.Flag [] flag = flags.getSystemFlags();
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Flags.Flag.SEEN) {
isnew = true;
break;
}
}
return isnew;
}

/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach() throws Exception {
return isContainAttach(this.part);
}

/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception {
boolean attachflag = false;
String contentType = part.getContentType();
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
attachflag = true;
else if (mpart.isMimeType("multipart/*")) {
attachflag = isContainAttach((Part) mpart);
} else {
String contype = mpart.getContentType();
if (contype.toLowerCase().indexOf("application") != -1)
attachflag = true;
if (contype.toLowerCase().indexOf("name") != -1)
attachflag = true;
}
}
} else if (part.isMimeType("message/rfc822")) {
attachflag = isContainAttach((Part) part.getContent());
}
return attachflag;
}

/**
* 连到server,创建folder对象,创建message对象
*/
public void getConn() {
try {
this.getStoreFromServer();
this.getFolderFromStore();
} catch (Exception e) {
otherError = true;
mailDownErrorCounter++;
}
}

/**
* 建立Store连接
*/
private Store getStoreFromServer() throws Exception {
//创建session
Session session = Session.getDefaultInstance(System.getProperties(),
null);
//session.setDebug(true);

//创建store,建立连接
Store store = session.getStore(protocol);
System.out.println("connecting");
store.connect(mailHost, userName, password);
System.out.println("connected successfully");
setStore(store);
return store;
}

/**
* 打开INBox文件夹
*/
private Folder getFolderFromStore() {
//打开邮件相应文件夹
Folder getFolder;
try {
getFolder = store.getFolder("INBOX");
getFolder.open(Folder.READ_ONLY);
setFolder(getFolder);
return getFolder;
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.err.println("获取Folder失败!");
e.printStackTrace();
}
return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值