關於javamail

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章