Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mark email as read

 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am writting an application to read email and display emails using James server and POP3. I would like to know how to move already read email to another folder or how mark these emails as read so that everytime I run the program I will only read new emails. I understand that POP3 makes it a bit complicated because Flags don't really work with POP3 so I'm looking for another alternative to make this work. Please, this is very urgent and adding some code snippet to your answer will help a lot.
Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please EaseUp. There is no such thing as "urgent" around here.
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Problem. I will be patient...
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
POP3 doesn't support folders. You've already found out that it doesn't support flags much. Why don't you just use IMAP if you need those features?
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I can get all the message with the follwong Message[] msg = folder.getMessages(); I thought it would be possible to copy and paste these emails (files and attachments) which I believe are stored in the "inbox" folder to another pre defined folder that I create let's say C:\emails and then I can delete the content of the "inbox" folder.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can of course store emails in some folder on your local hard disk. But that is entirely outside of what JavaMail handles. As Paul said, POP3 doesn't support the concept of folders.
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how can I achieve this? i am stuck when it comes to being able to copy the emails to another folder on my local disk. can anybody help with the code?
Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe "copying" isn't the best term for it - you would read the message, and create a text file with its content on your hard disk.
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the term "Copy" because I actually want to cpoy the files from the "inbox" folder to another folder on my local disk and then delete the files in the inbox folder. almost like a cut and paste...
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "another" folder on your local disk like that, it really sounds like you think that the Inbox folder is also a folder on your local disk. Which it isn't, it's a folder in your mail server. So it would be less confusing if you said you wanted to copy a mail message (please don't call it a "file", that's confusing too) to a file on your local disk.

Let's use that as a description of what you want to do, then. You want to take a message from your mail server (you don't even need to mention the Inbox because POP3 doesn't support folders anyway). And you want to write the message to a file on your disk. (And then you want to delete the message from the mail server.)

If you get the mail message into your code as a MimeMessage then you'll see from the API documentation that there's a writeTo(OutputStream) on that class. You could pass a FileOutputStream to that method. Don't forget to close the output stream after you finish writing to it.
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since moving email after reading is a bit challenging I thought about another alternative. Since after I read an email I store it to a Database I want always want to whether the record exist or not. I believe the combination of Sender and time will always be unique. so I want to check in my table whether this combination exist if yes do not insert but if it exist, insert... But I don't really know how write this logic. can somebody assist whit the code please. Thank you
 
Abdoul Kader Soumahoro
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out with the following code:



sentdate is a String here...
Just in case somebody is stuck on this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic