使用带有POP3协议的javamail检索所有未读电子邮件
我正在尝试访问我的gmail帐户,并从中检索所有未读电子邮件的信息 我在参考了许多链接后编写了代码。我给了一些参考链接 为了测试我的代码,我创建了一个Gmail帐户。所以我收到了4条来自Gmail的信息。 我在检查邮件数量后运行应用程序。这显示了正确的结果。4封未读邮件。 正在显示所有信息(例如日期、发件人、内容、主题等) 然后我登录到我的新帐户,阅读其中一封电子邮件,然后重新运行我的应用程序。 现在,未读邮件的计数应为3,但它显示“未读邮件数:0” 我在这里复制代码使用带有POP3协议的javamail检索所有未读电子邮件,java,jakarta-mail,pop3,Java,Jakarta Mail,Pop3,我正在尝试访问我的gmail帐户,并从中检索所有未读电子邮件的信息 我在参考了许多链接后编写了代码。我给了一些参考链接 为了测试我的代码,我创建了一个Gmail帐户。所以我收到了4条来自Gmail的信息。 我在检查邮件数量后运行应用程序。这显示了正确的结果。4封未读邮件。 正在显示所有信息(例如日期、发件人、内容、主题等) 然后我登录到我的新帐户,阅读其中一封电子邮件,然后重新运行我的应用程序。 现在,未读邮件的计数应为3,但它显示“未读邮件数:0” 我在这里复制代码 public clas
public class MailReader
{
Folder inbox;
// Constructor of the calss.
public MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, "abc@gmail.com",
"paasword");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
public static void main(String args[]) {
new MailReader();
}
}
公共类邮件阅读器
{
文件夹收件箱;
//calss的构造函数。
公共邮件阅读器(){
System.out.println(“内部邮件阅读器()…”;
最终字符串SSL_FACTORY=“javax.net.SSL.SSLSocketFactory”;
/*设置邮件属性*/
Properties props=System.getProperties();
//设置手动属性
setProperty(“mail.pop3.socketFactory.class”,SSL_工厂);
props.setProperty(“mail.pop3.socketFactory.fallback”、“false”);
props.setProperty(“mail.pop3.port”、“995”);
props.setProperty(“mail.pop3.socketFactory.port”、“995”);
props.put(“mail.pop3.host”、“pop.gmail.com”);
尝试
{
/*创建会话并获取用于读取邮件的存储*/
Session Session=Session.getDefaultInstance(
System.getProperties(),null);
Store Store=session.getStore(“pop3”);
store.connect(“pop.gmail.com”,995,”abc@gmail.com",
“paasword”);
/*提及要读取的文件夹名称*/
//inbox=store.getDefaultFolder();
//inbox=inbox.getFolder(“收件箱”);
收件箱=store.getFolder(“收件箱”);
/*使用应用商店打开收件箱*/
收件箱。打开(文件夹。只读);
/*获取收件箱中未读的邮件*/
消息消息[]=收件箱。搜索(新标志词(新标志(
Flags.Flag.SEEN),false);
System.out.println(“未读邮件数量:“+邮件长度”);
/*使用合适的配置文件*/
FetchProfile fp=新的FetchProfile();
fp.add(FetchProfile.Item.信封);
fp.add(FetchProfile.Item.CONTENT\u INFO);
inbox.fetch(消息,fp);
尝试
{
打印所有消息(消息);
收件箱。关闭(true);
store.close();
}
捕获(例外情况除外)
{
System.out.println(“读取邮件时出现异常”);
例如printStackTrace();
}
}
捕获(消息异常e)
{
System.out.println(“连接到服务器时出现异常:”
+e.getLocalizedMessage());
e、 printStackTrace();
系统出口(2);
}
}
public void printalmessages(Message[]msgs)引发异常
{
对于(int i=0;i
我在谷歌上搜索过,但我发现你应该使用Flags.Flag.SEEN来阅读未读的电子邮件。
但在我的案例中,这并没有显示出正确的结果
有人能指出我可能犯了什么错误吗
如果你需要完整的代码,我可以编辑我的帖子
注意:我编辑了我的问题以包括所有
Flags.Flag.RECENT
Flags seen = new Flags(Flags.Flag.RECENT);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
messages = inbox.search(unseenFlagTerm);
public Message[] fetchMessages(String host, String user, String password, boolean read) {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore();
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
// use READ_ONLY if you don't wish the messages
// to be marked as read after retrieving its content
emailFolder.open(Folder.READ_WRITE);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, read);
return emailFolder.search(unseenFlagTerm);
}