让多个 Java pop3 客户端与 GMail 一起使用
我用 Java 编写了一个很好的程序,它连接到 gmail 帐户并下载发送给它的附件。 下载附件后,它会被标记为已读,并且不会再次下载。 该程序必须在多个实例中运行,每个程序都下载唯一的附件,这样单个附件就不会被下载两次。 问题是,目前如果附件大小合适,一个程序仍在下载它,而另一个实例连接并在附件被标记为已读之前也开始下载附件。
我尝试过检查和设置各种标志并检查文件夹是否打开,但似乎没有任何效果。 有什么解决办法吗?
更新:感谢您的快速解答,遗憾的是由于其他原因,IMAP 不是一个选项。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
考虑使用 IMAP - 它是为客户端-服务器交互而设计的。
Consider using IMAP instead - it is designed for client-server interaction.
来自 RFC1939(邮局协议 - 版本 3):
From RFC1939 (Post Office Protocol - Version 3):
我不认为 POP3 是为多个同时访问而设计的。
问问自己:我真的需要多个进程访问同一个邮箱吗?
如果这样做,您将必须找到一种方法让这些进程相互通信。
使用通用数据库或服务器进程来协调操作。
IMAP 确实有更多选项,但我不确定您是否可以“锁定”单个邮件以将其标记为正在处理。
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.
正如其他人所提到的,POP3 并不是真正适合这种情况。
如果您绝对必须使用 POP3,我建议将所有电子邮件下载到中间服务器,该服务器对邮件进行排序并使它们可供其他每个客户端使用。
听起来您只是想分散电子邮件的处理。 如果是这种情况,您只需让每个客户端连接到中间服务器即可检索下一条可用消息。
我不确定您的限制是什么,但您甚至可能想考虑通过电子邮件以外的其他方式接收附件。 如果人们上传文件,您可以设置一个 Web 表单,自动将每个文件发送到应用程序的下一个可用实例进行处理。
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.
如果您需要保持 POP3 连接,您可以保留先前下载的消息 ID 的本地数据库。 然后新实例可以在再次下载之前进行检查。 不过,最好的解决方案是使用 IMAP,因为 IMAP 能够在下载之前设置已读/未读标志。
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.
您可以在开始下载之前将邮件标记为已读,然后开始下载。
You could mark the mail as read before starting the download, and then start downloading it.