How do email clients handle mails from the POP server? - java

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.

Related

New to Email Parser in Java, facing issue when reading the subject

How do I read the original subject of the kick back email received from the mail delivery system.
I am doing it this way: messageSubject = message.getSubject(); which actually returns the wrong one i.e ”Delivery status notification” (this one I can see under subject of view message details of kick back email).
It is normal for a "bounce" email that you get in response to an email that cannot be delivered to have a different subject to the email that you sent. The original email may be included as an attachment to the notification.
The responses that you get in this scenario are not standardized and vary from one mail server (MTA) to another. If you are going to process them automatically, you will need to understand the structure of the responses you are getting from your MTA and potentially the remote MTA that are bouncing the emails.
You should probably start by looking at the bounce emails in your email to get an idea of their structure.

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.

GMAIL: Send one email with multiple addresses or sending multiple emails. Which one is better?

My web app using JMS to send mail via GMAIL as my SMTP server. I need to send out email to every people in my group (20 - 50 people), does GMAIL allow to send this many email in short amount of time? And if so, can I get around with either sending one email with multiple addesses or sending multiple email with one address.
Think about this:
If you send one email, the mail server does the work sending a copy to each recipient.
If you send 20 emails, your code does all the work, sending 20 copies over the network to the email server.
You may want to use bcc: for the recipients if you don't want every recipient to see the address of every other.
The only reason you would want to send multiple messages is if you want to customize the message, such as
Hello <username>
blah blah blah

Search email inbox using javax.mail

I was trying to see if there was a way to search an email inbox from javax.mail. Say I wanted to send a query and have the it return emails to us. Can we parse returned HTML and extract data. Also, if the above is possible how would I "translate" those messages returned by that server to POP3 messages? E.g. we have extracted:
Subject: Foo
Body: Bar
but to open same message using POP3 I need to know it's POP3 uid, or number. I don't think we'll be able to get UID but perhaps we can figure out the number.
I guess the question is:
Can I send a query to email server (such as Hotmail or Yahoo) and get returned emails?
Unfortunately, the POP3 protocol doesn't support that. It is not like SQL or so. You need to mirror the complete mailbox yourself in some kind of a datastore (SQL database?) and execute the search on that. You can eventually keep/cache the data so that you don't need to retrieve the whole inbox everytime, but only the unread items.

How to track an email in Java?

How I can track an email?
I' m using java on the server side for sending emails. I want to track whether it is delivered , opened, etc... How I can do that ?
This is not a Java specific issue.
You can create an HTML email, and embed an invisible gif which will report back to your server. Some software like Outlook and some web mail programs will block this for untrusted emails.
You can request a return receipt. Many mail programs ignore this entirely, and the ones which don't usually ask the user if they want to send it.
Example:
email.AddHeaderField("Disposition-Notification-To","<g.revolution#stackoverflow.com>")
There is no way to ensure that you always get the delivery or open-message notification.
Mailservers may accept the mail and drop it afterwards.
users may read the mail but dismiss the notification.
"Webbugs" (aka images in the html source of the mail that include a special token that allows the mail to be recognized) don't work in most email programs.
As a matter of fact it's very unlikely that you can see that someone got the message.
What you could do is to keep the message on your server and only send a link. If the user clicks that you can be pretty sure that he got the message. But thankfully many users would not click on such links because it's used in fraud and spam.
I suppose you're sending it through SMTP. Whenever your mail is sent to your SMTP server, your java program has no control of it:
1) If you want to know if your mail has been delivered, you should contact your SMTP server somehow (if the SMTP server is outside your control then forget that) and see if your mail has been sent.
2) You can't know if a mail has been opened by its receiver. The maximum you can do is set a flag that the mail requires acknowledgement, but that depends if the user explicitly wants to send that acknowledgment. Other possibility is set some link to your site within the mail that should be clicked to see the content. You will be able to track if the user clicked that link.
what you can do is -
you have to embed an invisible image in the body in which the src will call the page in server and log that event, you can only do it in HTML encoding.
example -
<img src="ImageServer.aspx?imageID=track.jpg& custID=134ghxx34343ai& campID=32434"/>
and then in ImageServer.aspx there will a handling code which will log that event or save it to database.
example -
private void Page_Load(object sender, System.EventArgs e)
{
// content type should be resolved programmatically
Response.ContentType = "image/jpeg";
if (!IsPostBack) Track();
Response.WriteFile(GetImageFileByID());
}
In case you send HTML email, you could add a 1x1 pixel image with some tracking parameters, that calls back to your webserver. It's not 100% reliable since some email clients (and users) block images in emails.
You can add something like
<span style="display:none">Tracking no: ${TRACK_NO}</span>
to the body of the email. This should always be in the response unless the email client strips it out.

Categories