views:

304

answers:

6

Hello, I have written a nice program in Java that connects to a gmail account and download atachments sent to it. Once an attachment has been downloaded, it is marked as read and is not downloaded ever again. This program will have to run in multiple instances with each program downloading unique attachments so that a single attachment is never downloaded twice. The problem is that at the moment if the attachment is of a decent size, one program is still downloading it, when another instance connects and also starts to download the attachment before it has been marked as read.

I have tried checking and setting various flags and checking whether the folder is open, nothing seems to work. Any solutions?

Update: Thank you for the quick answers, sadly IMAP is not an option due to other reasons.

+2  A: 

Consider using IMAP instead - it is designed for client-server interaction.

Thorbjørn Ravn Andersen
+1  A: 

From RFC1939 (Post Office Protocol - Version 3):

POP3 is not intended to provide extensive manipulation operations of mail on the server; normally, mail is downloaded and then deleted. A more advanced (and complex) protocol, IMAP4, is discussed in RFC1730.

McDowell
+1  A: 

I don't think POP3 is made for multiple simultaneous access.

Ask yourself this: do i really need multiple processes accessing the same mailbox?

If you do, you'll have to find a way to have these processes communicate to each other. Use a common database or server process to coordinate actions.

IMAP does have more options, but i'm not sure if you can "lock" a single mail to mark it as being processed.

Stroboskop
He could set flags on the email with IMAP, though I agree that this isn't a 100% reliable approach either.
jsight
Does IMAP set the flags immediately or at the end of the transaction?
Stroboskop
He most likely just fires off downloading clients through cron or similar without them knowing of each other.
Thorbjørn Ravn Andersen
A: 

If you need to stay with a POP3 connection, you could keep a local database of previously downloaded message ids. Then new instances could check against that before downloading again. The best solution is just to use IMAP, though, as IMAP is able to set the read/unread flags before downloading.

jsight
A: 

As the others have mentioned, POP3 isn't really intended for this kind of scenario.

If you absolutely have to use POP3, I'd suggest downloading all the e-mail to an intermediate server which sorts the messages and makes them available for each of the other clients.

It sounds like you're just trying to distribute the processing of the e-mails. If that's the case, you can just have each client connect to your intermediate server to retrieve the next available message.

I'm not sure what your constraints are, but you may even want to consider receiving the attachments some other way besides e-mail. If people are uploading files, you could set up a web form that automatically sends each file to the next available instance of your application for processing.

rob
Thank you, I might look into getting rid of using mail altogether, though downloading to another server would definately be an overkill.
A: 

You could mark the mail as read before starting the download, and then start downloading it.

aperkins
pop does not support this
Ah, I had forgotten that part.
aperkins