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("https://www.google.com/m8/feeds/contacts/"+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());
       name=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());
         }else{
           contactDTO=new InstrumentDTO(email.getAddress(), email.getAddress());
         }
         myGmailContactList.add(contactDTO);
       }
       LogManager.info(this, 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) {
super();
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=")
.append(value).append("]");
return builder.toString();
}


}

-------------------------------------------------------------

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


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




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

Popular posts from this blog

Android: where to run keytool command in android

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

Upload file on Google cloud storage using Java servlet on google app engine