ITeye博客

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

ShellyLi · 2009-09-02 16:34 · javamail · Bean · Markdown

//接上一文章 /** * 获取日期显示格式 */ 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.getAddress();
     if (from == null)
         from = "";
     String personal = address.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*.getAddress();
                 if (email == null)
                     email = "";
                 else {
                     email = MimeUtility.decodeText(email);
                 }
                 String personal = address*.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;
 }

 /**
  * 返回出错数组
  * 
  * @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* == 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)

暂无评论。

评论功能维护中,当前仅展示历史评论。