Java 接收未读邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21174357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java get unread emails
提问by user1700594
I'm writing a code to read email from gmail. So the first time I launch, it reads new emails. That's fine. But I want when I launch it a second time, it doesn't get same emails it got before.
我正在编写一个代码来从 gmail 读取电子邮件。所以我第一次启动时,它会读取新电子邮件。没关系。但是我希望当我第二次启动它时,它不会收到与以前相同的电子邮件。
For example if there are 3 unread emails, when I launch for the first time, it gets the 3. But When I launch again, it gets nothing (because it already got 3). And if there is a new email and I launch it again, it should get only the last one and not the 3 firsts.
例如,如果有 3 封未读电子邮件,当我第一次启动时,它会得到 3。但是当我再次启动时,它什么也得不到(因为它已经得到了 3)。如果有一封新电子邮件,我再次启动它,它应该只收到最后一封而不是 3 个第一封邮件。
Hope I'm clear enough.
希望我足够清楚。
I use the code from http://alvinalexander.com/java/javamail-search-unseen-unread-messages-mailbox-pop3
我使用来自http://alvinalexander.com/java/javamail-search-unseen-unread-messages-mailbox-pop3的代码
package javamailtests;
import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
public class JavaMailSearchInbox {
public static void main(String args[]) throws Exception {
// mail server info
String host = "pop.gmail.com";
String user = "USER";
String password = "PASS";
// connect to my pop3 inbox in read-only mode
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");
store.connect(host, user, password);
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message messages[] = inbox.search(unseenFlagTerm);
if (messages.length == 0) System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
// stop after listing ten messages
if (i > 10) {
System.exit(0);
inbox.close(true);
store.close();
}
System.out.println("Message " + (i + 1));
System.out.println("From : " + messages[i].getFrom()[0]);
System.out.println("Subject : " + messages[i].getSubject());
System.out.println("Sent Date : " + messages[i].getSentDate());
System.out.println();
}
inbox.close(true);
store.close();
}
}
回答by Steve
May I suggest returning the old messages that were read to "Mark as Unread" ?
我可以建议将已读的旧邮件退回到“标记为未读”吗?
回答by IslamHarb
import java.util.*;
import javax.mail.*;
public class ReadingEmail {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "[email protected]", "Password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
} catch (Exception mex)
mex.printStackTrace();
}
}
}
回答by Davex
I've just made this ugly example. And it worked, it retrieves all messages including those that have been seen.
我刚刚做了这个丑陋的例子。它起作用了,它检索了所有消息,包括那些已经看到的消息。
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
try {
Store store = App.session.getStore("imaps");
store.connect("imap-mail.outlook.com", "email", "password");
Folder folder = App.store.getFolder("Inbox");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
for (Message msg : msgs) {
System.out.println(msg.getSubject());
}
}catch(MessagingException e) {
System.out.println(e);
}
Of course you will have to properly retrieve just the mails you need, otherwise if you have an old email account this code will start a very heavy process.
当然,您必须正确检索您需要的邮件,否则如果您有一个旧的电子邮件帐户,此代码将启动一个非常繁重的过程。
I hope this could be helpfull.
我希望这会有所帮助。