熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

用Javamail寫的解析MimeMessage的程序

2022-06-13   來源: JSP教程 

  package coffeecatwebmail;
  
  import javaio*;
  import javatext*;
  import javautil*;
  import javaxmail*;
  import javaxmailinternet*;
  
  public class PraseMimeMessage{
    private MimeMessage mimeMessage = null;
    private String saveAttachPath = ;     //附件下載後的存放目錄
    private StringBuffer bodytext = new StringBuffer(); //存放郵件內容的StringBuffer對象
    private String dateformat = yyMMdd HH:mm;    //默認的日前顯示格式
    
    /**
     * 構造函數初始化一個MimeMessage對象
     */
    public PraseMimeMessage(){}
    public PraseMimeMessage(MimeMessage mimeMessage){
      thismimeMessage = mimeMessage;
      Systemoutprintln(create a PraseMimeMessage object);
    }
  
    public void setMimeMessage(MimeMessage mimeMessage){
      thismimeMessage = mimeMessage;
    }
    
    /**
     * 獲得發件人的地址和姓名
     */
    public String getFrom()throws Exception{
      InternetAddress address[] = (InternetAddress[])mimeMessagegetFrom();
      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 getMailAddress(String type)throws Exception{
      String mailaddr = ;
      String addtype = typetoUpperCase();
      InternetAddress []address = null;
      if(addtypeequals(TO) || addtypeequals(CC) ||addtypeequals(BCC)){
        if(addtypeequals(TO)){
          address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeTO);
        }else if(addtypeequals(CC)){
          address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeCC);
        }else{
          address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeBCC);
        }
        if(address != null){
          for(int i=;i<addresslength;i++){
            String email=address[i]getAddress();
            if(email==null) email=;
            else{
              email=MimeUtilitydecodeText(email);
            }
            String personal=address[i]getPersonal();
            if(personal==null) personal=;
            else{
              personal=MimeUtilitydecodeText(personal);
            }
            String compositeto=personal+<+email+>;
            mailaddr+=+compositeto;
          }
          mailaddr=mailaddrsubstring();
        }
      }else{
        throw new Exception(Error emailaddr type!);
      }
      return mailaddr;
    }
    
    /**
     * 獲得郵件主題
     */
    public String getSubject()throws MessagingException{
      String subject = ;
      try{
        subject = MimeUtilitydecodeText(mimeMessagegetSubject());
        if(subject == null) subject=;
      }catch(Exception exce){
      }
      return subject;
    }
    
    /**
     * 獲得郵件發送日期
     */
    public String getSentDate()throws Exception{
      Date sentdate = mimeMessagegetSentDate();
      SimpleDateFormat format = new SimpleDateFormat(dateformat);
      return formatformat(sentdate);
    }
    
    /**
     * 獲得郵件正文內容
     */
    public String getBodyText(){
      return bodytexttoString();
    }
    
    /**
     * 解析郵件把得到的郵件內容保存到一個StringBuffer對象中解析郵件
     * 主要是根據MimeType類型的不同執行不同的操作一步一步的解析
     */
    public void getMailContent(Part part)throws Exception{
      String contenttype = partgetContentType();
      int nameindex = contenttypeindexOf(name);
      boolean conname =false;
      if(nameindex != ) conname=true;
  
      Systemoutprintln(CONTENTTYPE: +contenttype);
      if(partisMimeType(text/plain) && !conname){
        bodytextappend((String)partgetContent());
      }else if(partisMimeType(text/html) && !conname){
        bodytextappend((String)partgetContent());
      }else if(partisMimeType(multipart/*)){
        Multipart multipart = (Multipart)partgetContent();
        int counts = multipartgetCount();
        for(int i=;i<counts;i++){
          getMailContent(multipartgetBodyPart(i));
        }
      }else if(partisMimeType(message/rfc)){
        getMailContent((Part)partgetContent());
      }else{}
    }
  
    /**
     * 判斷此郵件是否需要回執如果需要回執返回true否則返回false
     */
    public boolean getReplySign()throws MessagingException{
      boolean replysign = false;
      String needreply[] = mimeMessagegetHeader(DispositionNotificationTo);
      if(needreply != null){
        replysign = true;
      }
      return replysign;
    }
    
    /**
     * 獲得此郵件的MessageID
     */
    public String getMessageId()throws MessagingException{
      return mimeMessagegetMessageID();
    }
    
    /**
     * 【判斷此郵件是否已讀如果未讀返回返回false反之返回true】
     */
    public boolean isNew()throws MessagingException{
      boolean isnew = false;
      Flags flags = ((Message)mimeMessage)getFlags();
      FlagsFlag []flag = flagsgetSystemFlags();
      Systemoutprintln(flagss length: +flaglength);
      for(int i=;i<flaglength;i++){
        if(flag[i] == FlagsFlagSEEN){
          isnew=true;
          Systemoutprintln(seen Message);
          break;
        }
      }
      return isnew;
    }
    
    /**
     * 判斷此郵件是否包含附件
     */
    public boolean isContainAttach(Part part)throws Exception{
      boolean attachflag = false;
      String contentType = partgetContentType();
      if(partisMimeType(multipart/*)){
        Multipart mp = (Multipart)partgetContent();
        for(int i=;i<mpgetCount();i++){
          BodyPart mpart = mpgetBodyPart(i);
          String disposition = mpartgetDisposition();
          if((disposition != null) &&((dispositionequals(PartATTACHMENT)) ||(dispositionequals(PartINLINE))))
            attachflag = true;
          else if(mpartisMimeType(multipart/*)){
            attachflag = isContainAttach((Part)mpart);
          }else{
            String contype = mpartgetContentType();
            if(conty
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19435.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.