2013-05-30 79 views
0

我正在使用Java郵件1.4.6來讀取和解析Gmail郵箱的郵件。但問題是,當我綁解析emaill主題(例如主題字符串「建設失敗詹金斯:appanalyti X»appanalyti X#51" )的聲明使用java郵件在java中解析電子郵件主題的錯誤

String subjectStr=(String) mimeMessage.getSubject();

我越來越

subjectStr as「Jenkins的構建失敗:appanaltix»appanal」而不是 「Jenkins的構建失敗:appanalyti x»appanalyti x#51」。

我可以知道我哪裏出錯了嗎?需要它來解碼主題串

代碼:

public boolean parseEmailSubject(String host,String userName,String password,String configurationStringToCheck,String saveDirectory){ 
.... 
Session session = Session.getDefaultInstance(properties); 
     try { 
      IMAPSSLStore store = (IMAPSSLStore)session.getStore(IMAP); 
      store.connect(host, userName, password) ; 
      Folder folderInbox =store.getFolder(IMAP_FOLDER); 
      folderInbox.open(Folder.READ_ONLY); 
      Message[] arrayMessages = folderInbox.getMessages(); 
      for (int i = 0; i < arrayMessages.length; i++) { 
       MimeMessage message = (MimeMessage) arrayMessages[i]; 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       message.writeTo(bos); 
       bos.close(); 
       SharedByteArrayInputStream bis =new SharedByteArrayInputStream(bos.toByteArray()); 
       MimeMessage cmsg = new MimeMessage(session, bis); 
       bis.close(); 

       String megSubject = cmsg.getSubject(); 
       if(megSubject.contains(configurationStringToCheck)){ 
        String contentType = cmsg.getContentType(); 

        if (contentType.contains(IMAP_CONTENT_TYPE)) { 
         Multipart multiPart = (Multipart) cmsg.getContent(); 
         int numberOfParts = multiPart.getCount(); 
         for (int partCount = 0; partCount < numberOfParts; partCount++) { 
          MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount); 
          if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { 
           String fileName = part.getFileName(); 
           part.saveFile(saveDirectory + File.separator + fileName); 
          } 
         } 
        } 
        return true; 

       } 

      } 
      folderInbox.close(false); 
      store.close(); 

     }catch (Exception e) { 
     } 
} 

電子郵件樣本 (以下主題行) 構建失敗詹金斯:appanalyti X»appanalyti X#51

(下面身體部分) 變更:


< === [JENKINS遠程處理CAPAC ITY] ===>頻道開始 log4j:WARN記錄器(org.apache.commons.beanutils.converters.BooleanConverter)找不到appender。 log4j:WARN請正確初始化log4j系統。

+0

我們可以看到您的代碼和電子郵件示例(沒有任何私人數據)嗎? – Djon

+0

我編輯了上面的代碼並添加了郵件樣本 – user2215139

回答

0

的JavaMail你的主題進行解碼。當然,如果主題編碼不正確,它不會正確解碼。

Subject標題中的原始文本是什麼?

什麼是JavaMail調試輸出顯示?

+0

「在Jenkins中構建失敗:appanalyti x»appanalyti x#51」。這是我的主題包含和輸出我得到的是「建設失敗詹金斯:appanalytix»appanal」,當我運行junit測試用例我得到currect主題內容。 – user2215139

+0

是你從[msg.getHeader(「Subject」,null)](https://javamail.java.net/nonav/docs/api/javax/mail/internet/MimeMessage.html#getHeader(java) .lang.String,%20java.lang.String))?再一次,調試輸出顯示了什麼? –

0

,如果你使用Gmail服務器,那麼這是最好的代碼只需輸入您的用戶名和密碼,並運行它

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMail { 

    public static void main(String[] args) { 

     final String username = "username"; 
     final String password = "fghdf"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
      }); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("username")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("to email is")); 
      message.setSubject("Testing Subject"); 
      message.setText("Dear user ," 
       + "\n\n your username is xxx and pasword is yyy"); 

      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
}