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:
- First of all you need to add three jars which are:
- mail.jar
- mailapi.jar
- smtp.jar
You can do this by adding them in the build path in your IDE
- Now we import the neccessary classes:
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;
- For checking mails we make use of IMAP protocol, so we set it by below code:
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.
- Now we make the session and the store object which connect tothe corresponding gmail IMAP server:
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
- Now when we are connected we move to the inbox folder which contains all our mails:
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.
- You may check the total number of messages by using the below command:
System.out.println("Total number of messages-->"+inbox.getMessageCount());
- Now for checking your recent messages (For eg your top 10 messgaes) do the following:
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
- 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.
- 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.
- If you wanna see messages in newer to older fashion then just reverse the messages array and then display data
- 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).