HtmlUnit邮件群发

来源:互联网 发布:简单力学分析软件 编辑:程序博客网 时间:2024/06/04 01:03
为了用Java编写一个邮件群发软件,接触到了htmlunit,没太大技术含量,仅是使用htmlunit经验。


 


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
 
public class MySina {
 
    private WebClient client;
    private WebRequest request;
 
    private String sinaLoginUrl = "http://mail.sina.com.cn/cgi-bin/login.php";
    private String hostSinaUrl = "";
 
    public static void main(String[] args)
            throws FailingHttpStatusCodeException, MalformedURLException,
            IOException {
 
        String username = "***";
        String password = "***";
        String newpassword = "***";
        String nickname = "***";
        MySina mySina = new MySina();
         
        if(mySina.mailLoginBySina(username, password)){     //登录
            mySina.updatePwdBySina(password,newpassword);   //修改密码
            mySina.updateNickName(nickname);                //修改帐户昵称
        }else{
            System.out.println("登录失败!请检查用户名和密码是否正确!");
        }
    }
 
    public MySina() {
        client = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
        client.setJavaScriptEnabled(false);
    }
 
    /**
     * 
     * 更改帐户昵称
     * 
     * @param nickname
     *            昵称
     * @return boolean
     * @throws FailingHttpStatusCodeException
     * @throws IOException
     */
    public boolean updateNickName(String nickname)
            throws FailingHttpStatusCodeException, IOException {
 
        String sinaSetUrl = hostSinaUrl + "basic/setting_account";
        request = new WebRequest(new URL(sinaSetUrl),HttpMethod.POST);
 
        request.setCharset("utf-8");
        request.setRequestParameters(Arrays.asList(
                new NameValuePair("nickname", nickname),
                new NameValuePair("pop3", "on"),
                new NameValuePair("imap", "on")));
 
        client.getPage(request);
        HtmlPage p = client.getPage(hostSinaUrl + "classic/index.php");
 
        if (p.getBody().getTextContent()
                .indexOf("\"NickName\":\"" + nickname + "\"") > 0) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 
     * 修改密码
     * 
     * @param oldpassword
     *            旧密码
     * @param newpassword
     *            新密码
     * @return boolean
     * @throws FailingHttpStatusCodeException
     * @throws IOException
     */
    public boolean updatePwdBySina(String oldpassword, String newpassword)
            throws FailingHttpStatusCodeException, IOException {
 
        String sinaSetUrl = "http://login.sina.com.cn/member/security/password.php";
         
        request = new WebRequest(new URL(sinaSetUrl),HttpMethod.POST);
 
        request.setCharset("gbk");
        request.setRequestParameters(Arrays.asList(
                new NameValuePair("pass",oldpassword),
                new NameValuePair("pass1", newpassword),
                new NameValuePair("pass2", newpassword)));
 
        HtmlPage p = client.getPage(request);
 
        if (p.getBody().getTextContent().indexOf("您的密码修改成功") > 0) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 登录
     * 
     * @param username
     *            用户名
     * @param password
     *            密码
     * @return boolean
     * @throws FailingHttpStatusCodeException
     * @throws MalformedURLException
     * @throws IOException
     */
    public boolean mailLoginBySina(String username, String password)
            throws FailingHttpStatusCodeException, MalformedURLException,
            IOException {
 
        HtmlPage loginPage = client.getPage(sinaLoginUrl);
        HtmlForm loginForm = loginPage.getFormByName("free");
 
        HtmlInput u = loginForm.getInputByName("u");
        HtmlInput psw = loginForm.getInputByName("psw");
        HtmlSubmitInput loginButton = loginForm.getInputByName("登录");
 
        u.setValueAttribute(username);
        psw.setValueAttribute(password);
 
        HtmlPage result = loginButton.click();
        String resultUrl = result.getUrl().toString();
 
        if (resultUrl.indexOf("classic/index.php") > 0) {
 
            String regex = "http://(.*?)/";
            hostSinaUrl = myRegex(resultUrl, regex, null);
 
            if (hostSinaUrl.length() > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
 
    /**
     * 
     * 正则匹配替换
     * 
     * @param str
     * @param reg
     * @param replace
     * @return
     */
    public String myRegex(String str, String reg, String[] replace) {
 
        String result = null;
 
        Matcher m = Pattern.compile(reg).matcher(str);
 
        while (m.find()) {
 
            result = m.group();
 
            if (replace != null && replace.length > 0) {
 
                for (String s : replace) {
                    result = result.replace(s, "");
                }
            }
        }
        return result;
    }
}
  没太大技术含量,仅是使用htmlunit经验。


  附上htmlunit jar文件和API http://files.cnblogs.com/skillCoding/htmlunit-2.9-bin.zip
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子不爱和小朋友玩怎么办 不知道怀孕抽烟了怎么办 校园欺凌来了该怎么办 拉的屎是绿色的怎么办 生完孩子痔疮痛怎么办 生过孩子脱肛该怎么办 生完宝宝有痔疮怎么办 毎次大便都脱肛怎么办 怀孕快生了便秘怎么办 35周孕晚期便秘怎么办 怀孕了老公出轨了怎么办 婆婆跟老公睡了怎么办 婆婆和老公互黏怎么办 15岁就掉头发怎么办 24岁经常掉头发怎么办 舍友打游戏太吵怎么办 家里的地砖想换怎么办 墙上的瓷砖掉了怎么办 瓷砖掉了一块瓷怎么办 白色裙子太透了怎么办 剑三石头插错了怎么办 四六级证书丢了怎么办 当月发票冲红了怎么办 当月发票红冲了怎么办 作废的发票扔了怎么办 发票公章盖错了怎么办 发票章盖的模糊怎么办 下的电影没字幕怎么办 荣耀8下载东西慢怎么办 考科目二下大雨怎么办 不求上进的人怎么办 连锁经营投的钱怎么办 古曼丽不想养了怎么办 百度云上传太慢怎么办 手机视频拍倒了怎么办 老师收手机不给怎么办 云盘视频被和谐怎么办 我喜欢上两个人怎么办 海岛大亨5没电怎么办 360网盘关闭后怎么办 有鼻毛漏出来了怎么办