Get contact list from Gmail account using java

You can get all contacts exists in your gmail account using this program...

First you will need the jar file gdata (library files for fetching data from google)...
public static  List<ContactDTO> getAllGmailContacts(String gmailId,String password)
throws ServiceException, IOException {
InstrumentDTO contactDTO;
String name;
List<ContactDTO> myGmailContactList= new ArrayList<ContactDTO>();

ContactsService myService = new ContactsService("Find_Contacts_On_Gmail");
myService.setUserCredentials(gmailId, password);

    URL feedUrl = new URL(""+gmailId+"/full");

    ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);  
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
       ContactEntry entry = resultFeed.getEntries().get(i);
       System.out.println("\t" + entry.getTitle().getPlainText());
       for (Email email : entry.getEmailAddresses()) {  
    System.out.println("Friend Name:"+name+" and emailAddress:"+email.getAddress());

         if(name != null && name.trim().length()>0){
           contactDTO=new InstrumentDTO(name, email.getAddress());
           contactDTO=new InstrumentDTO(email.getAddress(), email.getAddress());
       }, resultFeed.getTitle().getPlainText()+" list size (Gmail contact list):"+myGmailContactList.size());  
    return myGmailContactList;


public class ContactDTO {

private String key;
private String value;

public ContactDTO() {

public ContactDTO(String key, String value) {
this.key = key;
this.value = value;
public String getKey() {
return key;
public void setKey(String key) {
this.key = key;
public String getValue() {
return value;
public void setValue(String value) {
this.value = value;

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ContactDTO [key=").append(key).append(", value=")
return builder.toString();



Iterate this ContactDTO list on jsp using following code.....

   <s:if test="myGmailContactList!=null">
       <tr bgcolor="red" bordercolor="RED">
           <td>Friend Name</td>
           <td>Email Address</td>
       <s:iterator value="myGmailContactList" >
         <tr><td><s:property value="key" /></td>
             <td><s:property value="value" /></td>

For complete code send mail to us..............

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

multiple checkboxes with same id