最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java Mail郵件接收工具類

 更新時(shí)間:2015年02月11日 08:52:45   投稿:junjie  
這篇文章主要介紹了java Mail郵件接收工具類,本文直接給出類實(shí)現(xiàn)代碼和使用示例,需要的朋友可以參考下

下面是一個(gè)郵件接收的工具類,有點(diǎn)長?。?!

public class ReciveMail { 
 private MimeMessage msg = null; 
  private String saveAttchPath = ""; 
  private StringBuffer bodytext = new StringBuffer(); 
  private String dateformate = "yy-MM-dd HH:mm"; 
  
  public ReciveMail(MimeMessage msg){ 
    this.msg = msg; 
    } 
  public void setMsg(MimeMessage msg) { 
    this.msg = msg; 
  } 
  
  /** 
   * 獲取發(fā)送郵件者信息 
   * @return 
   * @throws MessagingException 
   */ 
  public String getFrom() throws MessagingException{ 
    InternetAddress[] address = (InternetAddress[]) msg.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; 
  } 
  
  /** 
   * 獲取郵件收件人,抄送,密送的地址和信息。根據(jù)所傳遞的參數(shù)不同 "to"-->收件人,"cc"-->抄送人地址,"bcc"-->密送地址 
   * @param type 
   * @return 
   * @throws MessagingException 
   * @throws UnsupportedEncodingException 
   */ 
  public String getMailAddress(String type) throws MessagingException, UnsupportedEncodingException{ 
    String mailaddr = ""; 
    String addrType = type.toUpperCase(); 
    InternetAddress[] address = null; 
    
    if(addrType.equals("TO")||addrType.equals("CC")||addrType.equals("BCC")){ 
      if(addrType.equals("TO")){ 
        address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.TO); 
      } 
      if(addrType.equals("CC")){ 
        address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.CC); 
      } 
      if(addrType.equals("BCC")){ 
        address = (InternetAddress[]) msg.getRecipients(Message.RecipientType.BCC); 
      }    
      if(address != null){ 
        for(int i=0;i<address.length;i++){ 
          String mail = address[i].getAddress(); 
          if(mail == null){ 
            mail = ""; 
          }else{ 
            mail = MimeUtility.decodeText(mail); 
          } 
          String personal = address[i].getPersonal(); 
          if(personal == null){ 
            personal = ""; 
          }else{ 
            personal = MimeUtility.decodeText(personal); 
          } 
          String compositeto = personal +"<"+mail+">"; 
          mailaddr += ","+compositeto; 
        } 
        mailaddr = mailaddr.substring(1); 
      } 
    }else{ 
      throw new RuntimeException("Error email Type!"); 
    } 
    return mailaddr; 
  } 
  
  /** 
   * 獲取郵件主題 
   * @return 
   * @throws UnsupportedEncodingException 
   * @throws MessagingException 
   */ 
  public String getSubject() throws UnsupportedEncodingException, MessagingException{ 
    String subject = ""; 
    subject = MimeUtility.decodeText(msg.getSubject()); 
    if(subject == null){ 
      subject = ""; 
    } 
    return subject; 
  } 
  
  /** 
   * 獲取郵件發(fā)送日期 
   * @return 
   * @throws MessagingException 
   */ 
  public String getSendDate() throws MessagingException{ 
    Date sendDate = msg.getSentDate(); 
    SimpleDateFormat smd = new SimpleDateFormat(dateformate); 
    return smd.format(sendDate); 
  } 
  
  /** 
   * 獲取郵件正文內(nèi)容 
   * @return 
   */ 
  public String getBodyText(){ 
    return bodytext.toString(); 
  } 
  
  /** 
   * 解析郵件,將得到的郵件內(nèi)容保存到一個(gè)stringBuffer對象中, 
   * 解析郵件 主要根據(jù)MimeType的不同執(zhí)行不同的操作,一步一步的解析 
   * @param part 
   * @throws MessagingException 
   * @throws IOException 
   */ 
  public void getMailContent(Part part) throws MessagingException, IOException{ 
    
    String contentType = part.getContentType(); 
    int nameindex = contentType.indexOf("name"); 
    boolean conname = false; 
    if(nameindex != -1){ 
      conname = true; 
    } 
    System.out.println("CONTENTTYPE:"+contentType); 
    if(part.isMimeType("text/plain")&&!conname){ 
      bodytext.append((String)part.getContent()); 
    }else if(part.isMimeType("text/html")&&!conname){ 
      bodytext.append((String)part.getContent()); 
    }else if(part.isMimeType("multipart/*")){ 
      Multipart multipart = (Multipart) part.getContent(); 
      int count = multipart.getCount(); 
      for(int i=0;i<count;i++){ 
        getMailContent(multipart.getBodyPart(i)); 
      } 
    }else if(part.isMimeType("message/rfc822")){ 
      getMailContent((Part) part.getContent()); 
    } 
    
  } 
  
  /** 
   * 判斷郵件是否需要回執(zhí),如需回執(zhí)返回true,否則返回false 
   * @return 
   * @throws MessagingException 
   */ 
  public boolean getReplySign() throws MessagingException{ 
    boolean replySign = false; 
    String needreply[] = msg.getHeader("Disposition-Notification-TO"); 
    if(needreply != null){ 
      replySign = true; 
    } 
    return replySign; 
  } 
  
  /** 
   * 獲取此郵件的message-id 
   * @return 
   * @throws MessagingException 
   */ 
  public String getMessageId() throws MessagingException{ 
    return msg.getMessageID(); 
  } 
  
  /** 
   * 判斷此郵件是否已讀,如果未讀則返回false,已讀返回true 
   * @return 
   * @throws MessagingException 
   */ 
  public boolean isNew() throws MessagingException{ 
    boolean isnew = false; 
    Flags flags = ((Message)msg).getFlags(); 
    Flags.Flag[] flag = flags.getSystemFlags(); 
    System.out.println("flags's length:"+flag.length); 
    for(int i=0;i<flag.length;i++){ 
      if(flag[i]==Flags.Flag.SEEN){ 
        isnew = true; 
        System.out.println("seen message ......."); 
        break; 
      } 
    } 
    return isnew; 
  } 
  
  /** 
   * 判斷是是否包含附件 
   * @param part 
   * @return 
   * @throws MessagingException 
   * @throws IOException 
   */ 
  public boolean isContainAttch(Part part) throws MessagingException, IOException{ 
    boolean flag = false; 
    
    //String contentType = part.getContentType(); 
    if(part.isMimeType("multipart/*")){ 
      Multipart multipart = (Multipart) part.getContent(); 
      int count = multipart.getCount(); 
      for(int i=0;i<count;i++){ 
        BodyPart bodypart = multipart.getBodyPart(i); 
        String dispostion = bodypart.getDisposition(); 
        if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){ 
          flag = true; 
        }else if(bodypart.isMimeType("multipart/*")){ 
          flag = isContainAttch(bodypart); 
        }else{ 
          String conType = bodypart.getContentType(); 
          if(conType.toLowerCase().indexOf("appliaction")!=-1){ 
            flag = true; 
          } 
          if(conType.toLowerCase().indexOf("name")!=-1){ 
            flag = true; 
          } 
        } 
      } 
    }else if(part.isMimeType("message/rfc822")){ 
      flag = isContainAttch((Part) part.getContent()); 
    } 
    
    return flag; 
  } 
  
  /** 
   * 保存附件 
   * @param part 
   * @throws MessagingException 
   * @throws IOException 
   */ 
  public void saveAttchMent(Part part) throws MessagingException, IOException{ 
    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 dispostion = mpart.getDisposition(); 
        if((dispostion != null)&&(dispostion.equals(Part.ATTACHMENT)||dispostion.equals(Part.INLINE))){ 
          filename = mpart.getFileName(); 
          if(filename.toLowerCase().indexOf("gb2312")!=-1){ 
            filename = MimeUtility.decodeText(filename); 
          } 
          saveFile(filename,mpart.getInputStream()); 
        }else if(mpart.isMimeType("multipart/*")){ 
          saveAttchMent(mpart); 
        }else{ 
          filename = mpart.getFileName(); 
          if(filename != null&&(filename.toLowerCase().indexOf("gb2312")!=-1)){ 
            filename = MimeUtility.decodeText(filename); 
          } 
          saveFile(filename,mpart.getInputStream()); 
        } 
      } 
      
    }else if(part.isMimeType("message/rfc822")){ 
      saveAttchMent((Part) part.getContent()); 
    } 
  } 
  /** 
   * 獲得保存附件的地址 
   * @return 
   */ 
  public String getSaveAttchPath() { 
    return saveAttchPath; 
  } 
  /** 
   * 設(shè)置保存附件地址 
   * @param saveAttchPath 
   */ 
  public void setSaveAttchPath(String saveAttchPath) { 
    this.saveAttchPath = saveAttchPath; 
  } 
  /** 
   * 設(shè)置日期格式 
   * @param dateformate 
   */ 
  public void setDateformate(String dateformate) { 
    this.dateformate = dateformate; 
  } 
  /** 
   * 保存文件內(nèi)容 
   * @param filename 
   * @param inputStream 
   * @throws IOException 
   */ 
  private void saveFile(String filename, InputStream inputStream) throws IOException { 
    String osname = System.getProperty("os.name"); 
    String storedir = getSaveAttchPath(); 
    String sepatror = ""; 
    if(osname == null){ 
      osname = ""; 
    } 
    
    if(osname.toLowerCase().indexOf("win")!=-1){ 
      sepatror = "http://"; 
      if(storedir==null||"".equals(storedir)){ 
        storedir = "d://temp"; 
      } 
    }else{ 
      sepatror = "/"; 
      storedir = "/temp"; 
    } 
    
    File storefile = new File(storedir+sepatror+filename); 
    System.out.println("storefile's path:"+storefile.toString()); 
    
    BufferedOutputStream bos = null; 
    BufferedInputStream bis = null; 
    
    try { 
      bos = new BufferedOutputStream(new FileOutputStream(storefile)); 
      bis = new BufferedInputStream(inputStream); 
      int c; 
      while((c= bis.read())!=-1){ 
        bos.write(c); 
        bos.flush(); 
      } 
    } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    }finally{ 
      bos.close(); 
      bis.close(); 
    } 
    
  } 
  
  public void recive(Part part,int i) throws MessagingException, IOException{ 
    System.out.println("------------------START-----------------------"); 
    System.out.println("Message"+i+" subject:" + getSubject()); 
    System.out.println("Message"+i+" from:" + getFrom()); 
    System.out.println("Message"+i+" isNew:" + isNew()); 
    boolean flag = isContainAttch(part); 
    System.out.println("Message"+i+" isContainAttch:" +flag); 
    System.out.println("Message"+i+" replySign:" + getReplySign()); 
    getMailContent(part); 
    System.out.println("Message"+i+" content:" + getBodyText()); 
    setSaveAttchPath("c://temp//"+i); 
    if(flag){ 
      saveAttchMent(part); 
    } 
    System.out.println("------------------END-----------------------"); 
  }

}

郵件接收與工具類的使用,有好幾種郵件接收的寫法?。?/p>

看了很多網(wǎng)上其他代碼,pop3Server的值是pop.mail.163.com,但是試了試不成功,還有 user的值既可以是帶有 username@...com也可以是username。

如果收件郵箱是163郵箱,必須先登陸163郵箱中設(shè)置,開啟pop3服務(wù)。至于別的郵箱暫不知道。

public static void main(String[] args) throws Exception { 
    // 連接pop3服務(wù)器的主機(jī)名、協(xié)議、用戶名、密碼 
    String pop3Server = "pop.163.com"; 
    String protocol = "pop3"; 
    String user = "username"; 
    String pwd = "password"; 
     
    // 創(chuàng)建一個(gè)有具體連接信息的Properties對象 
    Properties props = new Properties(); 
    props.setProperty("mail.store.protocol", protocol); 
    props.setProperty("mail.pop3.host", pop3Server); 
     
    // 使用Properties對象獲得Session對象 
    Session session = Session.getInstance(props); 
    session.setDebug(true); 
     
    // 利用Session對象獲得Store對象,并連接pop3服務(wù)器 
    Store store = session.getStore(); 
    store.connect(pop3Server, user, pwd); 
     
    // 獲得郵箱內(nèi)的郵件夾Folder對象,以"只讀"打開 
    Folder folder = store.getFolder("inbox"); 
    folder.open(Folder.READ_ONLY); 
     
    // 獲得郵件夾Folder內(nèi)的所有郵件Message對象 
    Message [] messages = folder.getMessages();  
    ReciveMail rm = null; 
    for(int i=0;i< messages.size() ;i++){ 
      rm = new ReciveMail((MimeMessage) messages[i]); 
      rm.recive(messages[i],i);; 
    } 
     folder.close(false); 
    store.close(); 
} 

相關(guān)文章

  • SpringBoot請求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹

    SpringBoot請求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹

    這篇文章主要介紹了SpringBoot請求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Protostuff序列化和反序列化的使用說明

    Protostuff序列化和反序列化的使用說明

    今天小編就為大家分享一篇關(guān)于Protostuff序列化和反序列化的使用說明,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • spring注解 @Valid 的作用說明

    spring注解 @Valid 的作用說明

    這篇文章主要介紹了spring注解 @Valid 的作用說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 淺析Spring中的循環(huán)依賴問題

    淺析Spring中的循環(huán)依賴問題

    這篇文章主要介紹了淺析Spring中的循環(huán)依賴問題,Spring 是利用了 三級緩存 來解決循環(huán)依賴的,其實(shí)現(xiàn)本質(zhì)是通過提前暴露已經(jīng)實(shí)例化但尚未初始化的 bean 來完成的,需要的朋友可以參考下
    2023-11-11
  • java如何循環(huán)增加序號

    java如何循環(huán)增加序號

    這篇文章主要介紹了java如何循環(huán)增加序號問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解

    Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解

    GUI圖形界面設(shè)計(jì)是用戶和程序交互的工具,用戶通過圖形界面控制程序事件的發(fā)生。首先介紹Swing的基本體系結(jié)構(gòu),這是底層
    2021-09-09
  • jmeter壓力測試工具簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    jmeter壓力測試工具簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了jmeter壓力測試工具相關(guān)介紹資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java圖形界面之布局設(shè)計(jì)

    java圖形界面之布局設(shè)計(jì)

    這篇文章主要介紹了java圖形界面之布局設(shè)計(jì)的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • Mybatis plus枚舉處理器的具體使用

    Mybatis plus枚舉處理器的具體使用

    在開發(fā)中,數(shù)據(jù)庫表中的字段很常見會使用枚舉類型來表示一些固定的取值范圍,本文主要介紹了Mybatis plus枚舉處理器的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • 關(guān)于spring web-mvc衍生注解

    關(guān)于spring web-mvc衍生注解

    這篇文章主要介紹了關(guān)于spring web-mvc衍生注解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論

鄂尔多斯市| 东乡| 长子县| 灯塔市| 长海县| 津南区| 开封市| 尉犁县| 济南市| 长宁区| 盐津县| 云南省| 江门市| 乌什县| 邢台县| 鸡泽县| 越西县| 延津县| 曲周县| 广汉市| 浪卡子县| 贡觉县| 沐川县| 滨州市| 孝感市| 台前县| 泰宁县| 双流县| 独山县| 成都市| 乐山市| 灵台县| 容城县| 家居| 尖扎县| 嘉定区| 酒泉市| 东平县| 黄骅市| 河西区| 台州市|