我实现的接收邮件主要是基于pop3协议的,所以javamail提供的很多api实际上是没有用处的。
因此我实现web端邮箱大概的思路是:从邮箱服务器取新邮件,然后把新邮件解析成我要的类,并序列化到本地。展示邮件的时候,就将本地邮件反序列化出来。
从邮件服务器检查是否有新邮件;实现方法是遍历邮件服务器端的邮件,获取他们的uid(获取uid不需要下载邮件头,速度比获取messageId快的多)并与本地序列化的邮件判断(我将序列化的文件名就取成uid,但不同邮箱uid可能重复,因此文件路径最好是"/邮箱服务器名/邮箱号/uid文件"这种格式)。
如果是新邮件就获取,并解析它;邮件是由邮件头和邮件体组成,在邮件头中主要包含了收件人、发件人、主题等等基础信息。而邮件体中就包括了邮件的正文和附件等内容信息。下图就是pop3协议下,邮件的大致内容。
解析邮件头基本上使用javamail的api很快就能搞定。但解析邮件体的时候,遇到了比较多的问题。因为邮件体是由Multipart对象嵌套组合成的,Multipart又是由Bodypart组合成的。在解析邮件体的时候,就是递归解析这些对象。通过分析这些对象的类型(getContentType)来判断如何获取内容或者继续递归。如果获取到内容类型是 image/ 、text/、application/ 这些分别表示的是图片、文本或超文本、附件等内容,针对不同内容类型采取不同方式获取。
很多人可能遇到跟我一样的问题,邮件的正文是html格式的,但正文中的图片展示不出来,这里我们需要先观察一下html文本和上述邮件协议下的完整内容。如下图,所以我们只要将图片下载到工程目录下,然后把content-id 替换成新路径就可以了。
javamail的环境配置就不讲了,直接跳至关键的代码部分。一些文件读写,保存,数据库存储的代码就不展示了。主要是对邮件解析,以及web端邮箱的一些思路。包括邮件的已读,删除,等等标记这些实现。
import java.io.File; import java.io.IOException; import java.util.List; import javax.mail.Message; import javax.mail.MessagingException; /** * Pop3协议接受邮件服务层 * @author tao_j * */ public interface Pop3ReceiveMailService { /** * 根据邮箱号、邮箱密码、接受邮件的端口号,采用pop3协议获取邮件消息。 * @param email * @param password * @param port * @return * @throws Exception */ void receiveMessagesAccordingPop3(String email,String password, String path,int port)throws Exception; /** * 发送外网邮件 * @param message * @param personal * @param email * @param password * @throws Exception */ void sendExtranetMail(MimeMessageVo messageVo, String personal,String email,String password,String emailPath) throws Exception; /** * 获取默认域名 * @param email * @return */ String getUserHostName(String email)throws Exception; /** * 判断是否是新邮件 * @param emailPath * @param uid * @return */ boolean isNewEmail(String emailPath,String uid); /** * 序列化新邮件 * @param message * @param emailPath * @param uid * @throws IOException */ void serializeEmailObj(MimeMessageVo message,String emailPath,String uid) throws IOException; /** * 反序列化 * @param file * @return */ MimeMessageVo deserializeEmailObj(File file); /** * 根据uId获取邮件信息 * @param emailPath * @param UId * @return */ MimeMessageVo getMimeMessageVoByUId(String emailPath,String uId); /** * 根据删除标志、关键字获取用户收件箱下的邮件 * @param emailPath * @return */ List<MimeMessageVo> getMimeMessageVos(String emailPath,String keyContext); /** * 解析MimeMessage邮件信息 * @param message * @return * @throws Exception */ MimeMessageVo parserMimeMessage(Message message) throws Exception; /** * 设置邮件已读 * @param message * @param emailPath * @param uid */ void setEmailHasRead(MimeMessageVo message, String emailPath,String uid); /** * 从服务器彻底删除邮件 * @param uid * @param email * @param password * @throws MessagingException * @throws Exception */ void deleteEmail(String uids,String email,String password) throws Exception; }
import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.UUID; import javax.annotation.Resource; import javax.mail.BodyPart; 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.URLName; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import com.fpi.common.file.entity.po.FpiFile; import com.fpi.common.file.service.FpiFileService; import com.fpi.common.file.service.InnerFpiFileService; import com.fpi.epoch.common.lang.entity.vo.PageData; import com.fpi.epoch.database.service.CommonService; import com.fpi.prd.oa.im.entity.vo.MimeMessageVo; import com.fpi.prd.oa.im.service.Pop3ReceiveMailService; import com.sun.mail.pop3.POP3Folder; import edu.emory.mathcs.backport.java.util.Collections; @Service public class Pop3ReceiveMailServiceImpl implements Pop3ReceiveMailService { @Autowired private CommonService commonService; @Autowired(required = false) private FpiFileService fileService; @Autowired(required = false) private InnerFpiFileService innerService; @Resource(name = "mailSender") private JavaMailSenderImpl mailSender; /** 日志. */ static final Logger LOGGER = Logger.getLogger(Pop3ReceiveMailServiceImpl.class); /** * 根据邮箱号、邮箱密码、接受邮件的端口号,采用pop3协议获取邮件消息。 * @param email * @param password * @param port * @return * @throws Exception */ @Override public void receiveMessagesAccordingPop3(String email, String password,String path, int port) throws Exception { String hostName = getUserHostName(email); Properties props = System.getProperties(); //邮件发送服务器 String host="smtp."+hostName+".com"; props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); //获取配置信息的集合-Session对象 Session session = Session.getDefaultInstance(props, null); //邮件接受服务器 String reciveServerHost="pop."+hostName+".com"; URLName url = new URLName("pop3",reciveServerHost, port, null, email,password); Store store = session.getStore(url); store.connect(); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); POP3Folder inbox = (POP3Folder) folder; //设置序列化邮件的存储地址 String temp="email"+File.separator+getUserHostName(email) +File.separator+email.substring(0,email.indexOf("@")); String emailPath =path+File.separator+temp+File.separator+"receive"; String deletePath =path+File.separator+temp+File.separator+"delete"; Message[] messages = folder.getMessages(); for(Message message : messages){ String uid = inbox.getUID((MimeMessage) message); //接受新邮件 if(isNewEmail(emailPath, uid)&&isNewEmail(deletePath, uid)){ MimeMessageVo newEmail=parserMimeMessage(message); newEmail.setOwnerEmail(email); newEmail.setuId(uid); Date sendDate = message.getSentDate(); newEmail.setSendDate(sendDate); newEmail.setState("receive"); serializeEmailObj(newEmail, emailPath, uid); } } folder.close(false); store.close(); } /** * 发送外网邮件 * @param messageVo * @param personal * @param email * @param password * @throws Exception */ @Override public void sendExtranetMail(MimeMessageVo messageVo, String personal, String email, String password ,String emailPath) throws Exception { if(StringUtils.isNotEmpty(emailPath)){ String uid = UUID.randomUUID().toString().replaceAll("-", ""); messageVo.setuId(uid); messageVo.setFromAddress(personal+"<"+email+">"); messageVo.setSendDate(new Date()); messageVo.setState("send"); serializeEmailObj(messageVo, emailPath, uid); } String hostName = getUserHostName(email); String host="smtp."+hostName+".com"; mailSender.setHost(host); mailSender.setUsername(email); mailSender.setPassword(password); String[] toAddresss = messageVo.getToAddress().split(";"); for(String toAddress:toAddresss){ // 设置邮件信息 MimeMessage mimeMessge = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessge, true, "UTF-8"); //收件人 int begin =toAddress.indexOf("<"); int end =toAddress.indexOf(">",begin); if(begin!=-1&&end!=-1) toAddress = toAddress.substring(begin+1,end); mimeMessageHelper.setTo(toAddress); // 发送人 mimeMessageHelper.setFrom(email,personal); // 主题 mimeMessageHelper.setSubject(messageVo.getSubject()); // 优先级 mimeMessageHelper.setPriority(messageVo.getXpriority()); // 邮件内容 mimeMessageHelper.setText(messageVo.getContent(), true); // 设置发送时间 mimeMessageHelper.setSentDate(messageVo.getSendDate()); // 附件 String attachments = messageVo.getAttachId(); if (StringUtils.isNotBlank(attachments)) { for (String fileId : attachments.split(",")) { FpiFile fpiFile = commonService.get(FpiFile.class, fileId); File file = innerService.getFileOnDisk(fileId); mimeMessageHelper.addAttachment(MimeUtility.encodeWord(fpiFile.getFileName()), file); } } // 发送邮件 try { mailSender.send(mimeMessge); } catch (MailException e) { e.printStackTrace(); LOGGER.error(e); throw new Exception("邮箱账户设置错误,请检查邮箱或密码设置!"); } } } /** * 获取用户host * @param email * @return * @throws Exception */ @Override public String getUserHostName(String email) throws Exception { String hostName = ""; if (StringUtils.isNotBlank(email)) { int startIndex = email.indexOf("@") + 1; int endIndex = -1; if (startIndex != -1) { endIndex = email.indexOf(".com", startIndex); } if (startIndex == -1 || endIndex == -1) { throw new Exception("邮箱格式错误,请正确填写!"); } else { hostName = email.substring(startIndex, endIndex); } } else { throw new Exception("请设置账户邮箱!"); } return hostName; } /** * 判断是否是新邮件 * @param emailPath * @param uid * @return */ @Override public boolean isNewEmail(String emailPath,String uid) { String fileName = emailPath+File.separator+uid+".obj"; File file = new File(fileName); if(file.exists()){ return false; }else{ return true; } } /** * 序列化新邮件 * @param message * @param emailPath * @param uid * @throws IOException */ @Override public void serializeEmailObj(MimeMessageVo message,String emailPath,String uid) throws IOException { File dir = new File(emailPath); if (!dir.exists()) { dir.mkdirs(); } String fileName = emailPath+File.separator+uid+".obj"; File file = new File(fileName); if(!file.exists()){ file.createNewFile(); } FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(message); } catch (IOException e) { throw new IOException("序列化出错!"); } finally{ try { if(oos!=null){ oos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 反序列化 * @param file * @return */ @Override public MimeMessageVo deserializeEmailObj(File file) { MimeMessageVo vo = new MimeMessageVo(); FileInputStream fis=null; ObjectInputStream ois=null; try { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); vo = (MimeMessageVo) ois.readObject(); return vo; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally{ try { if(ois!=null){ ois.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 获取用户邮箱下的所有邮件 * @param emailPath * @return */ @Override public List<MimeMessageVo> getMimeMessageVos(String emailPath,String keyContext) { List<MimeMessageVo> list = new ArrayList<MimeMessageVo>(); File file = new File(emailPath); if(file.exists()&&file.isDirectory()){ File[] messages=file.listFiles(); for(File message : messages){ MimeMessageVo vo=deserializeEmailObj(message); if(vo!=null){ String fromAddress=vo.getFromAddress(); String toAddress = vo.getToAddress(); if(fromAddress==null){ fromAddress=""; } if(toAddress==null){ toAddress=""; } vo.setFromAddress(fromAddress.replaceAll("<", "<").replaceAll(">", ">")); vo.setToAddress(toAddress.replaceAll("<", "<").replaceAll(">", ">")); if(StringUtils.isNotEmpty(keyContext)){ keyContext=keyContext.trim(); if(vo.getSubject().contains(keyContext)||toAddress.contains(keyContext)||fromAddress.contains(keyContext)){ list.add(vo); } continue; }else{ list.add(vo); } } } } if(list.size()>0){ Collections.sort(list); } return list; } /** * 解析MimeMessage邮件信息 * @param message * @return * @throws Exception */ @Override public MimeMessageVo parserMimeMessage(Message message) throws Exception { MimeMessageVo vo = new MimeMessageVo(); vo.setSeenFlag(0); vo.setAttachFlag(0); vo.setFromAddress(getFrom((MimeMessage)message)); vo.setToAddress(getMailAddress((MimeMessage)message,"to")); vo.setCcAddress(getMailAddress((MimeMessage)message,"cc")); vo.setBccAddress(getMailAddress((MimeMessage)message,"bcc")); vo.setSubject(getSubject((MimeMessage)message)); vo.setPriority(getPriority((MimeMessage)message)); String ids=saveAttachment((Part) message); if(StringUtils.isNotEmpty(ids)){ vo.setAttachFlag(1); vo.setAttachId(ids.substring(1)); } StringBuffer sb = new StringBuffer(); Map<String,String> imageMap = new HashMap<String,String>(); getMailContent((Part)message,sb,imageMap); String htmlContent = sb.toString(); Set<Entry<String, String>> set =imageMap.entrySet(); Iterator<Entry<String, String>> it = set.iterator(); while(it.hasNext()){ Entry<String,String>entry = it.next(); String cid=entry.getKey(); String src=entry.getValue(); htmlContent=htmlContent.replace(cid, src); } vo.setContent(htmlContent); return vo; } /** * 获取发件人信息 * @param mimeMessage * @return * @throws Exception */ public String getFrom(MimeMessage mimeMessage) throws Exception { InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom(); String from = address[0].getAddress(); String personal = address[0].getPersonal(); if (from == null) from = ""; if (personal == null) personal = ""; String addr = personal + "<" + from + ">"; return addr; } /** * 获取收件人、抄送人、密件抄送人信息 * @param mimeMessage * @param type * @return * @throws Exception */ public String getMailAddress(MimeMessage mimeMessage, String type) throws Exception { String mailaddr = ""; type = type.toUpperCase(); InternetAddress[] address = null; if (type.equals("TO") || type.equals("CC")|| type.equals("BCC")) { if (type.equals("TO")) { address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.TO); } else if (type.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("获取地址出错!"); } return mailaddr; } /** * 获取主题 * @param mimeMessage * @return * @throws Exception */ public String getSubject(MimeMessage mimeMessage) throws Exception { String subject = ""; subject = MimeUtility.decodeText(mimeMessage.getSubject()); if (subject == null) subject = ""; return subject; } /** * 获取优先级 * @param mimeMessage * @return * @throws Exception */ public String getPriority(MimeMessage mimeMessage) throws Exception{ String priority = "普通"; String[] headers = mimeMessage.getHeader("X-Priority"); if (headers != null) { String headerPriority = headers[0]; if (headerPriority.indexOf("1") != -1 || headerPriority.indexOf("High") != -1) priority = "紧急"; else if (headerPriority.indexOf("5") != -1 || headerPriority.indexOf("Low") != -1) priority = "低"; else priority = "普通"; } return priority; } /** * 保存附件 * @param part * @return * @throws Exception */ public String saveAttachment(Part part) throws Exception{ String ids=""; String fileName = ""; 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)))) { fileName = mpart.getFileName(); if (fileName.toLowerCase().indexOf("gb2312") != -1||fileName.toLowerCase().indexOf("utf") != -1){ fileName = MimeUtility.decodeText(fileName); } ids+=","+saveFile(fileName, mpart.getInputStream()); } else if (mpart.isMimeType("multipart/*")) { saveAttachment(mpart); } } } else if (part.isMimeType("message/rfc822")) { saveAttachment((Part) part.getContent()); } return ids; } /** * 写到本地 * @param fileName * @param is * @return * @throws IOException */ public String saveFile(String fileName , InputStream is) throws IOException{ String id=""; id=fileService.saveFile(input2byte(is), fileName, "", "", ""); return id; } /** * 获取邮件正文 * @param part * @param sb * @throws Exception */ public void getMailContent(Part part,StringBuffer htmlContent,Map<String,String> imageMap) throws Exception { String contentType = part.getContentType(); int nameIndex = contentType.indexOf("name"); if (part.isMimeType("text/html") && nameIndex==-1) { htmlContent.append((String)part.getContent()); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int counts = multipart.getCount(); for (int i = 0; i < counts; i++) { getMailContent(multipart.getBodyPart(i),htmlContent,imageMap); } } else if (part.isMimeType("message/rfc822")) { getMailContent((Part) part.getContent(),htmlContent,imageMap); } else if (contentType.contains("image/")) { String id="cid:"+part.getHeader("Content-ID")[0].replace("<", "").replace(">", ""); String root ="src/main/webapp/mailImages"; String src ="/image"+new Date().getTime() + ".jpg"; imageMap.put(id,"/oa/mailImages"+src); src=root+src; File file = new File(root); if(!file.exists()){ file.mkdirs(); } File image = new File(src); if(!image.exists()){ image.createNewFile(); } DataOutputStream output=null; try{ output= new DataOutputStream(new BufferedOutputStream(new FileOutputStream(image))); com.sun.mail.util.BASE64DecoderStream test = (com.sun.mail.util.BASE64DecoderStream) part.getContent(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = test.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } }catch(Exception e){ e.printStackTrace(); }finally{ if(output!=null){ try{ output.close(); }catch(Exception e){ e.printStackTrace(); } } } } else {} } /** * 将inputstream转换成byte[] * @param is * @return * @throws IOException */ private byte[] input2byte(InputStream is) throws IOException { ByteArrayOutputStream bs = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int length = 0; while ((length = is.read(buff, 0, 100)) > 0) { bs.write(buff, 0, length); } byte[] in2b = bs.toByteArray(); return in2b; } /** * 设置邮件已读 * @param message * @param emailPath * @param uid */ @Override public void setEmailHasRead(MimeMessageVo message, String emailPath,String uid) { try { message.setSeenFlag(1); serializeEmailObj(message, emailPath, uid); } catch (IOException e) { e.printStackTrace(); } } /** * 根据uId获取邮件信息 * @param emailPath * @param UId * @return */ @Override public MimeMessageVo getMimeMessageVoByUId(String emailPath, String uId) { String filePath =emailPath+File.separator+uId+".obj"; MimeMessageVo vo=deserializeEmailObj(new File(filePath)); return vo; } /** * 获取对应邮箱下的邮件分页数据 * @param pageData * @param emailPath * @param keyContext * @param delFlag * @return */ @Override public PageData<MimeMessageVo> getMimeMessageVoPageData( PageData<MimeMessageVo> pageData, String emailPath, String keyContext) { try { List<MimeMessageVo> list=getMimeMessageVos(emailPath,keyContext); int startIndex = pageData.getBeginIndex(); int endIndex = pageData.getEndIndex(); int total = list.size(); pageData.setTotal(total); if(endIndex+1>total){ endIndex=total; } pageData.setRows(list.subList(startIndex, endIndex)); } catch (Exception e) { pageData.setRows(null); pageData.setTotal(0); e.printStackTrace(); } return pageData; } /** * 从服务器彻底删除邮件 * @param uid * @param email * @param password * @throws Exception */ @Override public void deleteEmail(String uids, String email, String password) throws Exception { if(StringUtils.isEmpty(uids)){ throw new Exception("未选中需彻底删除的邮件!"); } String hostName = getUserHostName(email); Properties props = System.getProperties(); //邮件发送服务器 String host="smtp."+hostName+".com"; props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); //获取配置信息的集合-Session对象 Session session = Session.getDefaultInstance(props, null); //邮件接受服务器 String reciveServerHost="pop."+hostName+".com"; URLName url = new URLName("pop3",reciveServerHost, 110, null, email,password); Store store = session.getStore(url); store.connect(); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); POP3Folder inbox = (POP3Folder) folder; Message[] messages = folder.getMessages(); for(Message message : messages){ String tempUid = inbox.getUID((MimeMessage) message); if(uids.contains(tempUid)){ message.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); } }
import java.io.Serializable; import java.util.Date; public class MimeMessageVo implements Serializable,Comparable<MimeMessageVo>{ private static final long serialVersionUID = -2341790681671987361L; private String uId; private String ownerEmail; private String messageId; private String subject; private String fromAddress; private String toAddress; private String ccAddress; private String bccAddress; /** * 邮件发送时间 */ private Date sendDate; /** * 逻辑删除的时间 */ private Date deleteDate; /** * 草稿保存的时间 */ private Date saveDate; private String priority; /** * 优先级:1 紧急;5 低;3 普通 */ private Integer xpriority; /** * 状态:receive 收件箱;send 发件箱;draft 草稿箱;delete 垃圾箱 */ private String state; /** * 已读标志:0未读;1已读 */ private Integer seenFlag; /** * 附件标志:0没有;1有 */ private Integer attachFlag; /** * 附件id,已逗号隔开 */ private String attachId; /** * 邮件正文 */ private String content; private Long size; public String getuId() { return uId; } public void setuId(String uId) { this.uId = uId; } public String getOwnerEmail() { return ownerEmail; } public void setOwnerEmail(String ownerEmail) { this.ownerEmail = ownerEmail; } public String getMessageId() { return messageId; } public void setMessageId(String messageId) { this.messageId = messageId; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getCcAddress() { return ccAddress; } public void setCcAddress(String ccAddress) { this.ccAddress = ccAddress; } public String getBccAddress() { return bccAddress; } public void setBccAddress(String bccAddress) { this.bccAddress = bccAddress; } public Date getSendDate() { return sendDate; } public void setSendDate(Date sendDate) { this.sendDate = sendDate; } public String getPriority() { if(this.priority==null&&this.xpriority!=null){ if (this.xpriority==1) priority = "紧急"; else if (this.xpriority==5) priority = "低"; else priority = "普通"; } return priority; } public void setPriority(String priority) { this.priority = priority; } public Integer getSeenFlag() { return seenFlag; } public void setSeenFlag(Integer seenFlag) { this.seenFlag = seenFlag; } public Integer getAttachFlag() { return attachFlag; } public void setAttachFlag(Integer attachFlag) { this.attachFlag = attachFlag; } public String getAttachId() { return attachId; } public void setAttachId(String attachId) { this.attachId = attachId; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Long getSize() { return size; } public void setSize(Long size) { this.size = size; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Integer getXpriority() { return xpriority; } public void setXpriority(Integer xpriority) { this.xpriority = xpriority; } public Date getDeleteDate() { return deleteDate; } public void setDeleteDate(Date deleteDate) { this.deleteDate = deleteDate; } public Date getSaveDate() { return saveDate; } public void setSaveDate(Date saveDate) { this.saveDate = saveDate; } @Override public int compareTo(MimeMessageVo o) { if(state.equals("delete")&&o.state.equals("delete")){ if(this.deleteDate.before(o.deleteDate)){ return 1; }else if (this.deleteDate.equals(o.deleteDate)){ return 0; }else{ return -1; } }else if(state.equals("draft")&&o.state.equals("draft")){ if(this.saveDate.before(o.saveDate)){ return 1; }else if (this.saveDate.equals(o.saveDate)){ return 0; }else{ return -1; } }else{ if(this.sendDate.before(o.sendDate)){ return 1; }else if (this.sendDate.equals(o.sendDate)){ return 0; }else{ return -1; } } } }