java 接收、解析邮件实例(三)

阅读更多
package com.prase.email.four;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;

import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

public class PraseMimeMessage{
	
 private MimeMessage mimeMessage=null;
 private String saveAttachPath="";//附件下载后的存放目录
 private  StringBuffer bodytext=new StringBuffer();
 //存放邮件内容的StringBuffer对象
 private String dateformat="yy-MM-dd HH:mm";//默认的日前显示格式
  /**
  * 构造函数,初始化一个MimeMessage对象
  */
 public PraseMimeMessage() {
 }
 public PraseMimeMessage(MimeMessage mimeMessage) {
  this.mimeMessage=mimeMessage;
 }
 public void setMimeMessage(MimeMessage mimeMessage){
  this.mimeMessage=mimeMessage; 
 }
 /**
  * 获得发件人的地址和姓名
  */
 public String getFrom1()throws Exception{
  InternetAddress address[]=(InternetAddress[])mimeMessage.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"---密送人地址  
  * @throws Exception */
 public String getMailAddress(String type){
  String mailaddr="";
  try {  
   String addtype=type.toUpperCase();
   InternetAddress []address=null;
   if(addtype.equals("TO")||addtype.equals("CC")||addtype.equals("BBC")){
    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[i].getAddress();
      if(email==null)email="";
      else{
       email=MimeUtility.decodeText(email);
      }
      String personal=address[i].getPersonal();
      if(personal==null)personal="";
      else{
       personal=MimeUtility.decodeText(personal);
      }
      String compositeto=personal+"<"+email+">";
      mailaddr+=","+compositeto;
     }
     mailaddr=mailaddr.substring(1);
    }
   }
   else{
   }
  } catch (Exception e) {
   // TODO: handle exception
  } 
  return mailaddr;
 }
 /**
   * 获得邮件主题   
  */
 public String getSubject()
 {
  String subject="";
  try {
   subject=MimeUtility.decodeText(mimeMessage.getSubject());
   if(subject==null)subject="";
  } catch (Exception e) {
   // TODO: handle exception
  }
  return subject;
 }
 /**
   * 获得邮件发送日期
   */
 public String getSendDate()throws Exception{
  Date senddate=mimeMessage.getSentDate();
  SimpleDateFormat format=new SimpleDateFormat(dateformat);
  return format.format(senddate);
 }
 /**
   * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件
   * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
   */
 public void getMailContent(Part part)throws Exception{
  String contenttype=part.getContentType();
  int nameindex=contenttype.indexOf("name");
  boolean conname=false;
  if(nameindex!=-1)conname=true;
  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 counts=multipart.getCount();
   for(int i=0;i 
 
  • activation.jar (53.9 KB)
  • 下载次数: 7
  • mail.jar (451.3 KB)
  • 下载次数: 6

你可能感兴趣的:(mail.jar,java,email,javax,activation.jar)