有些邮件由于某些原因可能会没有dete属性,因此用getSentDate();跟getReceivedDate();方法是不到时间的,
在这种情况下就只能通过邮件的头信息来获取时间信息。如图:
public String getHeader(){ Enumeration temp; String ReceivedDate=""; Date date=null; String[] lines = new String[]{"Received"};//获取Received的信息 try { temp=mimeMessage.getMatchingHeaderLines(lines); String Received=(String) temp.nextElement(); if(Received.split(";").length>1){ SimpleDateFormat format = new SimpleDateFormat(dateformat); SimpleDateFormat sdf = new SimpleDateFormat(" EEE, dd MMM yyyy HH:mm:ss Z",Locale.US);//根据Received里提取的时间信息格式决定(ps:必须保持一致) ReceivedDate=Received.split(";")[1]; try { date=sdf.parse(ReceivedDate);//将提取的时间转成date } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } ReceivedDate=format.format(date);//转成自己想要的时间格式 } } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ReceivedDate; }
方法如下: