关于javamail

来源:互联网 发布:jsp网上商城系统 源码 编辑:程序博客网 时间:2024/06/07 02:18

1.  这几天在学习javamail时碰到一个问题,怎么样去标记已读邮件,让Quartz读取邮件时不再重复读取。刚开始查到了用flag标记seen,但是使用过程中发现,会报错,原因好像是pop3中,flag只能执行delete操作。

    这个问题可以这么解决:javamail读取邮件时,可以返回一个每个邮件特有的ID。这样便可以存储邮件的ID,在下一次读取邮件时,只需读取ID值不存在表中的邮件即可。

 

2.在获取邮件正文时,出现了获取到的内容重复的情况。

   原因可能是出在获取正文,解析正文的方法上。原方法如下:

   /**
  * 获得邮件正文内容
  */
 public String getBodyText() {
  return bodytext.toString();
 }

 /**
  * 解析邮件,把得到的邮件内容保存到一个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;
  System.out.println("CONTENTTYPE: " + contenttype);
  if (part.isMimeType("text/html") && !conname) {
   bodytext.append((String) part.getContent());
  } else if (part.isMimeType("text/plain") && !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 < counts; i++) {
    getMailContent(multipart.getBodyPart(i));
   }
 
 } else if (part.isMimeType("message/rfc822")) {
   getMailContent((Part) part.getContent());
  } else {
  }
 }

猜想也许是因为text/html和text/plain都存在,导致重复获取正文内容(前者支持HTML语法 后者是纯文本) 这样,屏蔽掉含有text/plain的判断语句,再去接收,就不重复了,或者屏蔽text/html,也有同样效果,只是此时获取的只是一些纯文本了。不过也不知道是否是这个原因O(∩_∩)O~

 

3.javamail接收邮件后,将附件保存进数据库,将附件从数据库读取出并可保存。

(1)保存进数据库:保存附件时,把放置二进制字段设置为Blob类型,根据文件的大小选择合适的Blob类型,以下是各个Blob类型所能容纳二进制文件的大小

          MySQL的四种BLOB类型:

     类型 大小(单位:字节)

          TinyBlob 最大 255

          Blob 最大 65K

          MediumBlob 最大 16M

          LongBlob 最大 4G

         本例选择了Blob。保存的过程比较简单,就像保存其他数据类型一样,在service层写了方法,进行保存。

(2)从数据库中读取附件并保存到本地磁盘,SSH:

         Dao层方法:

 /**
  * 返回附件名称<将附件名称存入数据库,以供下载附件时用>
  * @throws Exception
  * @throws IOException
  * @throws MessagingException
  * @throws UnsupportedEncodingException
  */
 public String attachName(Part part) throws UnsupportedEncodingException, MessagingException, IOException, Exception {
  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 disposition = mpart.getDisposition();
    if ((disposition != null)
      && ((disposition.equals(Part.ATTACHMENT)) || (disposition
        .equals(Part.INLINE)))) {
     fileName = mpart.getFileName();
     if (fileName.toLowerCase().indexOf("gb2312") != -1) {
      fileName = MimeUtility.decodeText(fileName);
     }
     
    } else if (mpart.isMimeType("multipart/*")) {
     saveAttachMent(mpart);
    } else {
     fileName = mpart.getFileName();
     if ((fileName != null)
       && (fileName.toLowerCase().indexOf("GB2312") != -1)) {
      fileName = MimeUtility.decodeText(fileName);
      
     }
    }
   }
  }
  return fileName;
 }

 /**
  * 将附件保存到数据库
  */
 public Blob mysqlInsert(Part part)throws Exception{
  String fileName="";
  Blob attachment = null;
  if (part.isMimeType("multipart/*")) {
   Multipart mp = (Multipart) part.getContent();
   for (int i = 0; i < mp.getCount(); i++) {
    BodyPart mpart = mp.getBodyPart(i);
    //BodyPart mpart = mp.getBodyPart(0);
    String disposition = mpart.getDisposition();
    if ((disposition != null)
      && ((disposition.equals(Part.ATTACHMENT)) || (disposition
        .equals(Part.INLINE)))) {
     fileName = mpart.getFileName();
     if (fileName.toLowerCase().indexOf("gb2312") != -1) {
      fileName = MimeUtility.decodeText(fileName);
     }
     attachment=Hibernate.createBlob(mpart.getInputStream());
    } else if (mpart.isMimeType("multipart/*")) {
     mysqlInsert(mpart);
    } else {
     fileName = mpart.getFileName();
     if ((fileName != null)
       && (fileName.toLowerCase().indexOf("GB2312") != -1)) {
      fileName = MimeUtility.decodeText(fileName);
      attachment=Hibernate.createBlob(mpart.getInputStream());
     }
    }
   }
  } else if (part.isMimeType("message/rfc822")) {
   mysqlInsert((Part) part.getContent());
  }
  return attachment;  
 }
 

 

/**
  * 从数据库中读取附件供下载
  */
 public String mysqlOutput(Integer taskId) throws Exception {
  Session session = (Session) getSession();
  String hql = "from Task t where t.taskId=?";
  Query query = session.createQuery(hql);
  query.setParameter(0, taskId);
  List list = query.list();
  Iterator it = list.iterator();
  while (it.hasNext()) {
   Task task = (Task) it.next();
   Blob blob = task.getAddition();
   String addName = task.getAddName();
   InputStream ins = blob.getBinaryStream();
   HttpServletResponse response = ServletActionContext.getResponse();
   response.setContentType("application/unknown");
   response.addHeader("Content-Disposition", "attachment; filename="
     + addName);
   OutputStream outStream = response.getOutputStream();
   byte[] bytes = new byte[1024];
   int len = 0;
   while ((len = ins.read(bytes)) != -1) {
    outStream.write(bytes, 0, len);
   }
   ins.close();
   outStream.close();
   outStream = null;
  }
  return "success";
 }

 

Service层方法:

/**
  * 从数据库中读取附件供下载
  */
 public String mysqlOutput(Integer taskId) throws Exception {
  
  return taskDao.mysqlOutput(taskId);
 }

action层:

/**
  * 从数据库中读取附件供下载
  */
 public String additionDown()throws Exception{
  String flag=taskService.mysqlOutput(taskId);
  return flag;
 }

JSP:

<td width="50">
         <s:if test="#list.addition!=null">
          <a
           href="down.action?taskId=<s:property value="#list.taskId"/>"><s:property value="#list.addName"/></a>
         </s:if>
        </td>

 

原创粉丝点击