环境
开发环境:.net4.0+vs2013
操作系统:win8.1
内容
由于项目需要,拟开发一个模块进行邮箱验证操作,最终采取使用pop3服务器进行邮箱登陆并且获取内容
编码
/// <summary>
/// 点击链接
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static bool Click2(Info info)
{
//GC.Collect();
using (POP3_Client pop3 = new POP3_Client())
{
//选择pop3服务器的地址,这里只提供了189和139邮箱 有需要的可以自己去加
pop3.Logger = new Logger();
string _url = "";
if (info.Email.Contains("@189.cn"))
{
_url = "pop.189.cn";
}
else if (info.Email.Contains("@"))
{
_url = "pop.139.com";
}
else
{
return false;
}
//连接pop3服务器
pop3.Connect(_url, 110, false);
//尝试登陆
try
{
pop3.Authenticate(info.Email, info.PassWord, true);
}
catch (Exception ex)
{
return false;
}
//逆序遍历,可以获得最新的邮件内容
for (int i = pop3.Messages.Count - 1; i >= 0; i--)
{
POP3_ClientMessage message = pop3.Messages[i];
try
{
Mail_Message mime = Mail_Message.ParseFromByte(message.MessageToByte());
string text = mime.BodyHtmlText;
//根据关键词判断是否为自己需要读取的邮件
if (text.Contains("Confirm your signature by clicking here."))
{
//正则获得需要点击的链接并且点击
string url = MyTool.MyRegex.StringRegex(text, @"<p><strong><a href=""([\s\S]*?)""");
url = url.Replace("amp;", "");
string html = HttpAdd.OnlyGetHtml(url);//点击链接
if (html.Contains("You've successfully signed the petition below. Your signature has been verified and counted."))
{
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
}
}
}
return false;
}
第三方类库
实现此模块需要基于LumiSoft.Net该类库
百度网盘下载链接:
http://pan.baidu.com/s/1kU5BXrh 密码:evtv