使用 javamail 从我的 gmail 中读取所有新消息

2022-01-24 00:00:00 email java jakarta-mail jtextarea jframe

我有一个包含 GUI 的应用程序,它使用 Javamail.当我打开这个 Jframe 时,我必须在 jTextArea 上看到发送到我的邮件的消息.

I have an application which contains a GUI, it is using Javamail. When i open this Jframe I have to see messages that are sent to my mail on a jTextArea.

问题是当我编写代码时,它只显示发送的最后一条消息.

The problem is when i wrote my code it only shows just the last message sent.

如何在收件箱中显示所有新邮件?

How do I display all new messages in my inbox?

这是我的代码:

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", "mymail@gmail.com");

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("mymail@gmail.com", "mypassword");

    }
});

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

    store.connect("pop.gmail.com", "mymail@gmail.com", "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() + "
" + "From : " + msg[i].getFrom()[0] + "
" + "Subject : " + msg[i].getSubject() + "
" + "Message : " + "
" + msg[i].getContent().toString());

    }

    fldr.close(true);

    store.close();

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

推荐答案

您在此处的消息循环中反复将 jTextArea1 的文本设置为相同的内容:

You repeatedly set the text of the jTextArea1 to the same contents in your loop over messages here:

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

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

}

您应该使用所有消息构建一个 StringBuilder,然后设置 jTextArea1

You should build a StringBuilder with all the messages and then set the contents of the jTextArea1

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

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

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

然后,您可以通过使用增强的 for 循环并使用 StringBuilderappend 方法来使其更清晰.

You can then make this a lot more legible by using an enhanced for loop and using the append method of the StringBuilder.

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

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

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

相关文章