Reading all new messages from my gmail using javamail(使用 javamail 从我的 gmail 中读取所有新消息)
问题描述
我有一个包含 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 循环并使用 StringBuilder
的 append
方法来使其更清晰.
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());
这篇关于使用 javamail 从我的 gmail 中读取所有新消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 javamail 从我的 gmail 中读取所有新消息
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01