使用contact-list获取邮箱通讯录

contact-list https://github.com/flyerhzm/contactlist

在一项目中使用了contact-list获取邮箱通讯录,因作者没有时间继续更新这个项目,现在不能取得163邮箱通讯录。

我对OneSixThreeImporter做了一些修改,可以获得联系人了。其中contactsUrl 使用了 "http://tg4a84.mail.163.com/jy3/address/addrprint.jsp",使用Jsoup解析DOM。

在pom中需增加jsoup:

<dependency>
   <groupId>org.jsoup</groupId>
   <artifactId>jsoup</artifactId>
   <version>1.7.3</version>
</dependency>

OneSixThreeImporter代码:

package com.huangzhimin.contacts.email;

import com.huangzhimin.contacts.Contact;
import com.huangzhimin.contacts.exception.ContactsException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *  * 导入163联系人列表
 *  *
 *  * @author flyerhzm
 *  *
 *  
 */
public class OneSixThreeImporter extends EmailImporter {

    // 登录url
    private String loginUrl = "http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D3";


    /**
     *   * 构造函数
     *   *
     *   * @param email
     *   * @param password
     *  
     */
    public OneSixThreeImporter(String email, String password) {
        super(email, password, "UTF-8");
    }

    /**
     *   * 登录163邮箱
     *   *
     *   * @throws ContactsException
     *  
     */
    public void doLogin() throws ContactsException {
        try {
            NameValuePair params[] = {new NameValuePair("verifycookie", "1"),
                    new NameValuePair("product", "mail163"),
                    new NameValuePair("username", getUsername(email)),
                    new NameValuePair("password", password),
                    new NameValuePair("selType", "jy")};
            Calendar calendar = Calendar.getInstance();
            calendar.set(2099, 11, 31);
            client.getState().addCookies(
                    new Cookie[]{
                            new Cookie(".163.com", "ntes_mail_firstpage",
                                    "normal", "/", calendar.getTime(), false),
                            new Cookie(".163.com", "loginType", "js", "/",
                                    calendar.getTime(), false)});
            String responseStr = doPost(loginUrl, params, "http://mail.163.com/");

            String redirectUrl = getJSRedirectLocation(responseStr);
            doGet(redirectUrl, loginUrl);
        } catch (Exception e) {
            throw new ContactsException("163 protocol has changed", e);
        }
    }


    /**
     *   * 进入联系人列表页面,并读取所有的联系人信息
     *   *
     *   * @return 所有的联系人信息
     *   * @throws ContactsException
     *  
     */
    public List<Contact> parseContacts() throws ContactsException {
        try {
            String contactsUrl = "http://tg4a84.mail.163.com/jy3/address/addrprint.jsp?" + getSid(lastUrl);
            String content = doGet(contactsUrl);

            List<Contact> contacts = new ArrayList<Contact>();
            Document doc = Jsoup.parse(content);
            Elements names = doc.select(".gTitleSub .mTT");
            Elements addresses = doc.select(".gTable tbody tr:first-child td");
            int size = names.size();
            for (int i = 0; i < size; i++) {
                String username = names.get(i).text().trim();
                String email = addresses.get(i).text().trim();
                contacts.add(new Contact(username, email));
            }

            return contacts;
        } catch (Exception e) {
            throw new ContactsException("163 protocol has changed", e);
        }
    }

    private String getSid(String url) {
        return url.substring(url.indexOf("sid="));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值