java 未读邮件

编程入门 行业动态 更新时间:2024-10-09 10:19:36

java 未读<a href=https://www.elefans.com/category/jswz/34/1770920.html style=邮件"/>

java 未读邮件

我试图访问我的Gmail帐户,并从中检索所有未读电子邮件的信息。

我已经写了我的代码之后引用了很多链接。我给了几个链接供参考。

要测试我的代码,我创建了一个Gmail帐户。因此,我从Gmail收到了4封邮件。

我在检查邮件数后运行我的应用程序。这显示正确的结果。 4封未读邮件。

正在显示所有信息(例如日期,发件人,内容,主题等)

然后我登录到我的新帐户,阅读其中一封电子邮件并重新运行我的申请。

现在未读消息的计数应该是3,但它显示“未读消息数:0”

我在这里复制代码。

public class MailReader

{

Folder inbox;

// Constructor of the calss.

public MailReader() {

System.out.println("Inside MailReader()...");

final String SSL_FACTORY = "javax.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");

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", 995, "abc@gmail",

"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();

}

}

我在google上搜索,但我发现你应该使用Flags.Flag.SEEN读取未读电子邮件。

但是这在我的情况下没有显示正确的结果。

有人可以指出我可能在做一些错误吗?

如果你需要整个代码,我可以编辑我的帖子。

注意:我编辑我的问题,包括整个代码,而不是我之前发布的代码段。

更多推荐

java 未读邮件

本文发布于:2024-02-07 00:05:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮件   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!