Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Offline Gmail Desktop App Tut [Part 1]

4.50/5 (2 votes)
18 Aug 2011CPOL3 min read 28.9K   1.7K  
Helps you to synchronise your gmails and lets you watch your mail even in offline states.

Introduction

This series of articles will help you to make a Gmail app with offline support. It will let you search your mail, view it, give alarms on new messages etc etc... This part covers the basics.

Using the Code

We start the tutorial step by step:

  1. First of all you need to add three jars which are:
    1. mail.jar
    2. mailapi.jar
    3. smtp.jar

    You can do this by adding them in the build path in your IDE

  2. Now we import the neccessary classes:
  3. Java
    import java.util.Properties;
    import javax.activation.DataHandler;
    import javax.mail.BodyPart;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Store;
  4. For checking mails we make use of IMAP protocol, so we set it by below code:
  5. Properties props = System.getProperties();
    	props.setProperty("mail.store.protocol", "imaps");

    Explaination:

    For accesing mails we have two protocols which are POP3 and IMAP. In these two protocols IMAP gives much more functionality than pop protocol. Here we are setting the system property to make use of IMAP protocol.

  6. Now we make the session and the store object which connect tothe corresponding gmail IMAP server:
  7. Session session = Session.getDefaultInstance(props, null);
    	Store store = session.getStore("imaps");
    	store.connect("imap.gmail.com", "Your Username here","Your password here");

    Explaination:

    Here store is an abstract class that models a message store and its access protocol (in this case IMAP), for storing and retrieving messages. We have set the session to use the property we made above

  8. Now when we are connected we move to the inbox folder which contains all our mails:
  9. C#
    Folder inbox = store.getFolder("Inbox");
    	inbox.open(Folder.READ_WRITE);

    Explaination:

    All messages are stored in the folder object. Here we are retreiving a folder by the name Inbox. To access the folder we need to open it which is being done through inbox. Open(Folder.READ_WRITE). Now inbox contains all the mails we got and which are editable.

  10. You may check the total number of messages by using the below command:
    System.out.println("Total number of messages-->"+inbox.getMessageCount());
  11. Now for checking your recent messages (For eg your top 10 messgaes) do the following:
  12. Message[] messages1 = inbox.getMessages(inbox.getMessageCount()-9,inbox.getMessageCount());
    	for(Message message:messages1)
    	{
    		System.out.println("From:"+message.getFrom()[0]+"\nSubject:"+message.getSubject()+"\nDate Receving:"+message.getReceivedDate()+"\nMessage Number:"+message.getMessageNumber());
    		System.out.println("--------------------------------------------------------------");
    	}

    Explaination:

    Here inbox.getMessageCount() gives us the total number of messages. We make use of inbox.getMessage(from message number,to message number) and it retrives the 10 messages which are put in messages1. Now we make use of a foreach array to retreive information.

    message.getFrom()[0]-->Gives Address of person who sent the message

    message.getSubject()-->Provides the subject for the message

    message.getReceivedDate()-->Gives the receiving date

    message.getMessageNumber()-->Gives the message number.Now this is most important if you want to retreive any particular message

  13. Now if we want to see any particular mail content then first we retreive that mail and then check its content type so that if its mutipart some further processing is required
    System.out.println("Lets suppose we retrieve content of message number 1");
    	Message message=inbox.getMessage(1);
    	String content=null;
    	if(message.getContentType().toLowerCase().contains("text/plain")){
    		content=(String)message.getContent();
    		
    		}
    	   else if(message.getContentType().toLowerCase().contains("text/html")){
    		content=(String)message.getContent();
    		
    		}
    	   else
    	   {
    		  Multipart multipart = (Multipart) message.getContent();
    		  
    		  for (int x = 0; x < multipart.getCount(); x++) {
    		  BodyPart bodyPart = multipart.getBodyPart(x);
    		  content=bodyPart.getContent().toString();
    
    		  }
    	   }
    		content=content.replaceAll("<.*?>","");
    		System.out.println("Contents of mail are:\n"+content);

    Explaination:

    Now when we extract the contents of the message we need to see the content type of that message. If the message is a multipart then its composed of many messages and we need to sort out the body part of each. For text and HTML content there is no need of any special operation. In the case of multipart we count the number of components and then extract the body parts.

    content.replaceAll("<.*?>","");-->Done so that to remove the HTML tag for the output we obtain.

  14. Now if we want to show all possible mails instead of showing some selected mails then do the following (remember it shows mails in older to newer fashion)
    Message[] messages = inbox.getMessages();
    	for(Message msg:messages)
    	{
    		System.out.println("From:"+msg.getFrom()[0]+"\nSubject:"+msg.getSubject()+"\nDate Receving:"+msg.getReceivedDate()+"\nMessage Number:"+msg.getMessageNumber());
    		System.out.println("--------------------------------------------------------------");
    	}

    Explaination:

    inbox.getMessages() retreives all the messages which are shown by mean of foreach array.

  15. If you wanna see messages in newer to older fashion then just reverse the messages array and then display data
  16. For logout do the following
    inbox.close(true);
    store.close();

Points of Interest

This was just the basics, in next part we will discuss some advanced things like making caches for offline viewing, sending mails, view only unread mails, marking mails, and searching mails.

I have made a demo (covers more than this one but will be explained in next parts) in Download demo project(1MB).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)