2013-04-09 67 views
2

我有一個包含GUI的應用程序,它使用Javamail。當我打開這個Jframe我必須看到郵件發送到我的郵件jTextArea使用javamail從我的Gmail中讀取所有新郵件

問題是,當我寫我的代碼它只顯示最後發送的消息。

如何在收件箱中顯示所有新消息?

這是我的代碼:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

Properties props = new Properties(); 

props.put("mail.pop3.host", "pop.gmail.com"); 

props.put("mail.pop3.user", "[email protected]"); 

props.put("mail.pop3.socketFactory", 995); 

props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

props.put("mail.pop3.port", 995); 

Session session = Session.getDefaultInstance(props, new Authenticator() { 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("[email protected]", "mypassword"); 

    } 
}); 

try { 
    Store store = session.getStore("pop3"); 

    store.connect("pop.gmail.com", "[email protected]", "mypaswword"); 

    Folder fldr = store.getFolder("INBOX"); 

    fldr.open(Folder.READ_ONLY); 

    Message[] msg = fldr.getMessages(); 

    Address[] address; 


    for (int i = 0; i < msg.length; i++) { 

     jTextArea1.setText("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString()); 

    } 

    fldr.close(true); 

    store.close(); 

} catch (Exception e) { 
    System.out.println(e); 
} 

回答

0

你反覆設置jTextArea1爲相同內容的文字在你的循環中的郵件這裏:

for (int i = 0; i < msg.length; i++) { 

    jTextArea1.setText("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString()); 

} 

你應該建立一個StringBuilder與所有的消息然後設置內容jTextArea1

final StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < msg.length; i++) { 

    sb.append("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString()); 

} 
jTextArea1.setText(sb.toString()); 

然後,您可以通過使用增強的for循環並使用StringBuilderappend方法使其更清晰易讀。

final StringBuilder sb = new StringBuilder(); 
for (Message message : msg) { 

    sb.append("SentDate : "). 
      append(message.getSentDate()). 
      append("\n"). 
      append("From : "). 
      append(message.getFrom()[0]). 
      append("\n").append("Subject : "). 
      append(message.getSubject()). 
      append("\n"). 
      append("Message : "). 
      append("\n"). 
      append(message.getContent().toString()); 

} 
jTextArea1.setText(sb.toString()); 
+0

非常感謝你對你的反應, 所以用你的代碼,它顯示日期和郵件的主題,但只顯示一行部份內容: javax.mail。 internet.MimeMultipart @ – wael 2013-04-09 16:26:27

+0

您正在'message.getContent'方法中調用'toString',但根據[Javadoc](http://docs.oracle.com/javaee/6/api/javax/mail/Part。 html#getContent())如果消息是多段,這不是'String'。閱讀[this](http://stackoverflow.com/a/13013066/2071828)SO帖子,瞭解如何顯示多部分消息。 – 2013-04-09 16:40:41

+0

好吧再次感謝:) – wael 2013-04-09 17:00:48

0
final StringBuilder sb = new StringBuilder(); 
for (Message message : msg) { 

    sb.append("SentDate : "). 
     append(message.getSentDate()). 
     append("\n"). 
     append("From : "). 
     append(message.getFrom()[0]). 
     append("\n").append("Subject : "). 
     append(message.getSubject()). 
     append("\n"). 
     append("Message : "). 
     append("\n"). 
     append(message.getContent().toString()); 

} 
jTextArea1.setText(sb.toString()); 
相關問題