java Mail邮件接收工具类

下面是一个邮件接收的工具类,有点长!!!

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; 
  } 
  
  /** 
   * 获取发送邮件者信息 
   * @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; 
  } 
  
  /** 
   * 获取邮件收件人,抄送,密送的地址和信息。根据所传递的参数不同 "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"; 
          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; 
  } 
  
  /** 
   * 获取邮件发送日期 
   * @return 
   * @throws MessagingException 
   */ 
  public String getSendDate() throws MessagingException{ 
    Date sendDate = msg.getSentDate(); 
    SimpleDateFormat smd = new SimpleDateFormat(dateformate); 
    return smd.format(sendDate); 
  } 
  
  /** 
   * 获取邮件正文内容 
   * @return 
   */ 
  public String getBodyText(){ 
    return bodytext.toString(); 
  } 
  
  /** 
   * 解析邮件,将得到的邮件内容保存到一个stringBuffer对象中, 
   * 解析邮件 主要根据MimeType的不同执行不同的操作,一步一步的解析 
   * @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 
 

邮件接收与工具类的使用,有好几种邮件接收的写法!:

看了很多网上其他代码,pop3Server的值是pop.mail.163.com,但是试了试不成功,还有 user的值既可以是带有 [email protected]也可以是username。

如果收件邮箱是163邮箱,必须先登陆163邮箱中设置,开启pop3服务。至于别的邮箱暂不知道。

public static void main(String[] args) throws Exception { 
    // 连接pop3服务器的主机名、协议、用户名、密码 
    String pop3Server = "pop.163.com"; 
    String protocol = "pop3"; 
    String user = "username"; 
    String pwd = "password"; 
     
    // 创建一个有具体连接信息的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服务器 
    Store store = session.getStore(); 
    store.connect(pop3Server, user, pwd); 
     
    // 获得邮箱内的邮件夹Folder对象,以"只读"打开 
    Folder folder = store.getFolder("inbox"); 
    folder.open(Folder.READ_ONLY); 
     
    // 获得邮件夹Folder内的所有邮件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(); 
} 

你可能感兴趣的:(java Mail邮件接收工具类)