获取邮箱联系人
米聊有个邀请邮箱好友的功能,因为项目需要实现该功能,所以这些天稍稍的研究了下。刚好今天周末,发出来跟大家一起分享分享。
首先得感谢 flyerhzm@gmail.com 这位 “牛哥” , 他把很多邮箱的联系人都给爬出来了。然后打包成jar 发布出来了。源码也放在google code 里面了。
原理是这样的:登陆邮箱,保存相关的信息(cookies等),通过httpclient访问有通讯录的页面,爬出相关的联系人昵称和邮箱。
代码如下:
EmailType:
package com.duicky.email;
/**
* 邮箱的类型
*
* @author luxiaofeng
*
*/
public class EmailType {
public static final int email_gmail = 1;
public static final int email_sina = 2;
public static final int email_163 = 3;
public static final int email_yahoo = 4;
public static final int email_hotmail = 5;
public static final int email_139 = 6;
public static final int email_sohu = 7;
public static final int email_yeah = 8;
public static final int email_126 = 9;
public static final int email_189 = 10;
public static final int email_tom = 11;
public static final int email_msn = 12;
}
EmailManager:
package com.duicky.email;
import java.util.List;
import com.huangzhimin.contacts.Contact;
import com.huangzhimin.contacts.ContactsImporter;
import com.huangzhimin.contacts.ContactsImporterFactory;
import com.huangzhimin.contacts.exception.ContactsException;
/**
*
*
* @author luxiaofeng
*
*/
public class EmailManager {
private static EmailManager emailManager = null;
private EmailManager() {
}
public static EmailManager getInstence() {
if (emailManager == null) {
emailManager = new EmailManager();
}
return emailManager;
}
/**
* 获取邮箱通讯录
*
* @param type 邮箱类型
* 1-gmail,2-sina,3-163,4-yahoo,5-hotmail,6-139,7-sohu,8-yeah,
* 9-126,10-189,11-tom,12-msn
* @param account 帐号名
*
* @param password 密码
*
* @return 邮箱通讯录字符串
*
* 如果成功 返回格式如下:
* 通讯录有好友:<c><o><n>张三</n><e>123@qq.com</e></o><o><n>李四</n><e>431
* @qq.com</e></o></c> 其中以C为大标签,o为一个通讯录好友,n为昵称,e为邮箱
* 通讯录无好友:0
* 如果失败 则返回 "-1"
*/
public String getEmailContacts(int type, String account, String password) {
StringBuilder sb = new StringBuilder();
ContactsImporter importer = null;
try {
importer = getContactsImporter(type, account, password);
if (importer == null) {
sb.append("-1");
return sb.toString();
}
List<Contact> contacts = importer.getContacts();
int size = contacts.size();
if (size > 0) {
sb.append("<c>");
for (int i = 0; i < size; i++) {
sb.append("<o><n>");
sb.append(contacts.get(i).getUsername());
sb.append("</n>");
sb.append("<e>");
sb.append(contacts.get(i).getEmail());
sb.append("</e></o>");
}
sb.append("</c>");
} else {
sb.append("0");
}
} catch (ContactsException e) {
sb.append("-1");
e.printStackTrace();
}
return sb.toString();
}
/**
* 邀请邮箱好友
* @return 成功则 返回成功个数 <count>6<count>
* 失败则 返回-1
*/
public String inviteEmailContacts() {
String result = "";
return result;
}
/**
* 获取对应邮箱类型的Importer
* @param type
* @param account
* @param password
* @return
*/
private ContactsImporter getContactsImporter(int type, String account,
String password) {
ContactsImporter importer = null;
switch (type) {
case EmailType.email_gmail:
importer = ContactsImporterFactory.getGmailContacts(account, password);
break;
case EmailType.email_sina:
importer = ContactsImporterFactory.getSinaContacts(account, password);
break;
case EmailType.email_163:
importer = ContactsImporterFactory.getOneSixThreeContacts(account, password);
break;
case EmailType.email_yahoo:
importer = ContactsImporterFactory.getYahooContacts(account, password);
break;
case EmailType.email_hotmail:
importer = ContactsImporterFactory.getHotmailContacts(account, password);
break;
case EmailType.email_139:
importer = ContactsImporterFactory.getOneThreeNineContacts(account, password);
break;
case EmailType.email_sohu:
importer = ContactsImporterFactory.getSohuContacts(account, password);
break;
case EmailType.email_yeah:
importer = ContactsImporterFactory.getSinaContacts(account, password);
break;
case EmailType.email_126:
importer = ContactsImporterFactory.getOneTwoSixContacts(account, password);
break;
case EmailType.email_189:
importer = ContactsImporterFactory.getOneEightNineContacts(account, password);
break;
case EmailType.email_tom:
importer = ContactsImporterFactory.getTomContacts(account, password);
break;
case EmailType.email_msn:
importer = ContactsImporterFactory.getMSNContacts(account, password);
break;
default:
return null;
}
return importer;
}
}
项目的需要,我把获取后的联系人用一个String返回然后通过正则表达式匹配,需要修改的兄弟可以把它改成返回一个list集合。因为发邮件的接口设计到公司内部的东西,这边就不公开了。
该项目需要导入好多jar包,jar包统一放在libs文件夹中。哥们自己buildpath。
写了个测试的类,没几行代码:
package com.duicky.email;
public class EmailManagerTest {
/**
* @param args
*/
public static void main(String[] args) {
String emailAccount = ""; // your email account
String password = ""; // you know
int type = EmailType.email_msn; // your email type , such : EmailType.email_msn;
String str = EmailManager.getInstence().getEmailContacts(type, emailAccount, password);
System.out.println("我的邮箱好友:"+str);
}
}
运行效果如下:
我的邮箱好友:<c><o><n>自己1</n><e>lxf454162034@gmail.com</e></o><o><n>自己2</n><e>luxiaofeng544@hotmail.com</e></o><o><n>自己3</n><e>luxiaofeng544@sina.com</e></o><o><n>自己4</n><e>lxf454162034@163.com</e></o></c>
该项目有点欠缺,一大部分邮箱都能拿到,还有个别邮箱通讯录无法获取(如163,139==),想获取所有的同学自己修改源码,爬去把。核心代码Google Code 的地址是http://code.google.com/p/contact-list/。
欢迎转载,请注明出处 http://blog.csdn.net/dui_cky/article/details/6785250