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..............
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..............
Hi,
ReplyDeletePlease send the code to "illuminataks@gmail.com".
Thanks,
Aks
Hi,
ReplyDeleteGreat work!!!
can you please send me the complete code on my id that is bipinyadav85@gmail.com.
thanks
Bipin
can u plz send me code to
ReplyDeletesidduyerpula@gmail.com
hi can u send me the mail goliarun@gmail.com
ReplyDeletePlease send me the code. Feels great to understand google API and services. email id: pavan.kulkarni45@gmail.com
ReplyDeletehi can u send me the code.
ReplyDeletesend mail to this id ppushparaj87@gmail.com
Please send me complete code on my mail Id dnyaneshwardukare2@gmail.com
ReplyDeletePlease send me the complete code.
ReplyDeletebalujavac@gmail.com
could u send me complete code pls.... rajlaxmi126@gmail.com
ReplyDelete