FolderClosedException occurs while accessing yahoo mail with JavaMail IMAP API - java

I am trying to access Yahoo mail with IMAP using JavaMail API. I can connect to the Yahoo mail server successfully and am able to fetch the messages using folder.getMessages() call where folder is an object of javax.mail.Folder class.
I need to iterate over all the messages returned by this call and I fetch received date of each message in this iteration. The iteration works well for small number of messages as it does not takes a long time, however if the number of returned messages is large (say around 10000) and iteration takes more than 30 minutes, then following exception occurs after 30 minutes:
javax.mail.FolderClosedException: * BYE IMAP4rev1 Server logging out
at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1234)
at com.sun.mail.imap.IMAPMessage.getReceivedDate(IMAPMessage.java:378)
at mypack.ImapUtils.getReceivedDate(ImapUtils.java:193)
...
Please note that I do not use the Folder object again during this iteration.
Could anyone please tell:
if there is a way to keep the folder open on yahoo mail server until it is explicitly closed?
if there is some property or setting which can be used to increase this "30 minutes" interval after which the folder is closed by the yahoo's IMAP server.
Thanks.

Related

spurious push and unchanged pass error from wallet

In my application, I get a lot of these errors.
Error REST из apple wallet [2019-07-19 10:51:29 +0300] Web service
error for myOwnPassTypeIdentifier (https://webServiceURL): Device
received spurious push. Request for passesUpdatedSince
'30657301263000' returned no serial numbers. (Device = ....)
And also
Error REST из apple wallet [2019-07-19 12:43:33 +0300] Web service
error for myOwnPassTypeIdentifier (https://webServiceURL): Server
ignored the 'if-modified-since' header (Fri, 19 Jul 2019 09:43:14 GMT)
and returned the full unchanged pass data for serial number
'2222000174317170'.
How can I avoid them, hot to heal ? )
For preventing that error I had to send a correct date as last modified tag with Response in /passes/{passTypeIdentifier}/{serialNumber} method with prepared Pass and also in /devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier} with corrected SerialNumbers
I got a correct date from DB and push it to Response. So, any modification,firstly updated DB, then I send push to APNs and got Request to update pass, where I had last modified from the previous Request which I compare with actual date in my DB.

Camel HTTP endpoint throws NoHttpResponseException for large File bodies

Attempting to upload files onto a Server using its REST API from a Camel endpoint.
Below is part of the camel Endpoint in consideration
<camel:setBody>
<camel:simple>${header.objectdata.getData}</camel:simple><!-- 2mb file as String -->
</camel:setBody>
<camel:setHeader headerName="CamelHttpMethod">
<camel:constant>PUT</camel:constant>
</camel:setHeader>
<camel:recipientList id="ml-rest">
<camel:simple>{URL_HERE}</camel:simple>
</camel:recipientList>
The above endpoint works fine with smaller files. But for the ~2mb file, it throws
org.apache.commons.httpclient.NoHttpResponseException: The server localhost failed to respond
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1976)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)...
I tried uploading the same file onto the server using POSTMAN(not via code,camel) and it works fine.
Tried setting the SO_TIMEOUT option, but strangely it appears capped to 30 seconds. Setting values less than 30 appears to work, but greater than 30 seconds are simply ignored. I noticed this based on the time difference between the occurences of the following log statements.
...
[t1] Request body sent
[t2] Closing the connection

Handle client-side request timeout in java

A client sends a request and catches a timeout exception. However the server is still processing the request and saving it to the database. Before that happening, the client already sent a second request which doubles the record on the database. How do I prevent that from happening? Im using java servlets and javascript.
A few suggestions:-
1) Increase the client timeout.
2) Make the server more efficient so it can respond faster.
3) Get the server to respond with an intermediate "I'm working on it" response before returning with the main response.
4) Does the server need to do all the work before it responds to the client, or can some be offloaded to a seperate process for running later?
A client sends a request and catches a timeout exception. However the server is still processing the request
Make the servlet generate some output (can be just blank spaces) and flush the stream every so often (every 15 seconds for example).
If the connection has been closed on the client side, the write will fail with a socket exception.
Before that happening, the client already sent a second request which doubles the record on the database
Use the atomicity of the database, for example, a unique key. Start the process by creating a unique record (maybe in some "unfinished" status), it will fail if the record already exists.

Is POP server providing the emails in the order of mail sent time?

I am fetching the emails from the POP server.
I am using the following logic to find the mail which is received newly.
if(currentMail.getSentTime() > lastMailFetchedTime)
{
//Processing the email
}
else
{
System.out.println("Mail sent earlier. It might be fetched already");
}
At regular time interval , Some emails are missed from the POP mail fetcher(For Google Apps account). I have analyzed the mail fetching process and identified that, POP server is giving the older emails which is not given in the previous mail fetching.
Is Gmail POP server provides the mail based on the mail sent time(I am not getting it in proper order)?
If it is not given using the mail sent time means, How can I fetch the newly created emails without using IMAP ?
Think of the POP3 server as storing messages in a sequential list where the last message in the list is always the most recently received message.
So essentially it "sorts" them in order of arrival, but this might not be the same as "Date Sent".
POP3 server is automatically sorted messages, but only up to minutes.

How do email clients handle mails from the POP server?

When writing a connection to connect to the POP3 protocol through sockets, I could use the LIST command to get the list of emails. Suppose I do a LIST command, it returns 3700 mails in the list. I read the whole response into a buffer. Now, I want to list these mails one by one on my application.
LIST
1 1472
...
3696 3224
3697 5998
3698 1970
3699 1425
3700 129345
.
I had 2 ideas in mind. First, I could parse the response line by line and get the message number of the message. And for each line I get the message number, say #3700, I do a TOP 3700 10. So this would read 10 lines of the mail headers. At first, I thought this would be good because I don't have to do a download the entire mail just for the subject, name, from address and some bit of brief content of the email; I could save the traffic. But the problem is not all emails have only 10 lines of headers. Some emails have extremely long headers while others are very short. It is difficult to determine how many lines to read. Not only that, because the email is not entirely downloaded, when the user wants to read the full mail, I have to send another RETR command to get the whole email. Then suppose if there were another connection to the POP account and deleted the message #3700. My application still recognise the email by this "non-unique" message id. When my application wants to download the email and sends a RETR 3700, it will respond with a -ERR Server Unavailable. 21
The second idea I have is then to use a RETR 3700. This would read the whole mail, just for that few information to list it in the application. I thought this is kind of silly because if the mailbox has a lot of mails, I am retrieving the entire mailbox into the application's memory!
What are the common/clever ways an email client would handle the mails from the POP server?
PS: I am not using the JavaMail because the intention is to learn more about how the POP protocol works and the implementations used to work with the protocols.
The 2nd argument of TOP command is a non-negative number of message lines to download - header lines are not included.
You can use TOP command with 0 as line count (TOP message_number 0) to download only message headers.

Categories