Reading New Emails from Java Applications
Sometimes we only want to check new emails received, we commonly see this functionality in email client where in we open the email client in morning and we see few emails in black bold text denoting new emails received. In this articles we will learn on how we can identify new emails received in our mailbox using java.
Email Exchange Servers can be POP3 and IMAP, by default we use POP3. Every email have certain flags set by which we can identify the new emails received but these functionality is not available in POP3 servers; you will have to use IMAP servers. But there are alternatives to read new emails received in POP3 servers as well but that would required some processing and storing the email ids received at local end.
Various Flags Used in Emails are SEEN, RECENT, DELETED, ANSWERED, DRAFT, FLAGGED, USER; but now we are going to concentrate on SEEN and ANSWERED flag as that will be used for checking new emails.
Check Unread Unread Emails in Java:
import javax.mail.internet.MimeMultipart
import javax.mail.AuthenticationFailedException
import javax.mail.Folder
import javax.mail.FolderClosedException
import javax.mail.FolderNotFoundException
import javax.mail.Message
import javax.mail.Multipart
import javax.mail.NoSuchProviderException
import javax.mail.Part
import javax.mail.ReadOnlyFolderException
import javax.mail.Session
import javax.mail.Store
import java.io.InputStream
import java.io.File
import java.io.BufferedOutputStream
import java.io.BufferedInputStream
import java.io.FileOutputStream
import javax.mail.Flags
import javax.mail.StoreClosedException
import javax.mail.internet.InternetAddress
class ReadEmail {
private Session sessioned = null;
private Store store = null;
private Folder folder = null;
private Message message = null;
private Message[] messages = null;
private Object messagecontentObject = null;
private String sender = null;
private String subject = null;
private Multipart multipart = null;
private Part part = null;
private String contentType = null;
ReadEmail() {
//Default Constructor
}
void processEmail() {
sessioned = Session.getDefaultInstance(System.getProperties(),null);
store = sessioned.getStore("imap");
store.connect([HOSTNAME], [USERNAME],[PASSWORD]);
// Get a handle on the default folder
folder = store.getDefaultFolder();
// Retrieve the "Inbox"
folder = folder.getFolder("inbox");
//Reading the Email Index in Read / Write Mode
folder.open(Folder.READ_WRITE);
// Retrieve the messages
messages = folder.getMessages();
if (messages.length == 0) {
System.out.println("No Message to Read");
} else {
System.out.println("Total Messages Found:" + messages.length + "");
}
// Loop over all of the messages
for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
// Retrieve the next message to be read
message = messages[messageNumber];
Flags flags = message.getFlags();
if (!message.isSet(Flags.Flag.SEEN) && !message.isSet(Flags.Flag.ANSWERED)) {
//Here you will read all the email content which are unread
}
}
}
public static void main(String args[]) {
ReadEmail re = new ReadEmail();
re.processEmail();
}
}
Popular Articles:
- Date Manipulation in JAVA
- Reading Excel Sheet Documents in Java
- Factory Design Pattern in Java
- HTTP POST File Content in JAVA
- JSP – Create Custom Tags
- Java Plugin detection using JavaScript
- Reading Microsoft Word Document in JAVA
- Singleton Design Pattern in Java
- HTTP Form POST Request using AJAX and Servlet
- JSON in JAVA



































Ohw great tutorial I will give it a try.. Thanks!
why to you call
Flags flags = message.getFlags();
since you don’t use the flags variable?
Also you open the folder read_write but don’t set any flags etc.
thanks but someone may plz tell how to get or download only those mails on my PC which are unread?here in this code we are retrieving all messages.i don’t want that.