您的位置:首页 > 其它

从网页上提取用户邮箱为每个邮箱发送一封邮件

2012-07-01 21:29 357 查看
在工作中,经常遇到定期为注册用户发送相关信息的功能,.net中为我们封装了两个类MailMessage和SmtpClient,用这两个类即可实现相关的功能。

1.首先从网页http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=1&h=p_1255011420000上提取所有用户留下的邮箱

2.创建自定义的邮件,并给刚才提出的来的邮箱发送邮件

WebClient wc = new WebClient();
string html = wc.DownloadString("http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=1&h=p_1255011420000");
string reg = "[a-zA-Z0-9_\\.]+@[a-zA-Z0-9_\\.]+\\.[a-zA-Z0-9_\\.]+";
MatchCollection matches = Regex.Matches(html, reg);

List<string> listEmail = new List<string>();
foreach (Match mt in matches)
{
listEmail.Add(mt.Groups[0].Value);
}

//------------------以下是创建邮件和发送邮件的过程----------------------
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("****@qq.com");
mail.To.Add("*****@qq.com");
foreach (string email in listEmail)
{
mail.To.Add(email);
}
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = "测试邮件";
mail.BodyEncoding = Encoding.UTF8;
mail.Body = "c#程序控制控!!!!!!!";

//创建html的邮件内容
AlternateView view = AlternateView.CreateAlternateViewFromString("文字在这里,也可以是<h1>html</h1>的代码<img src=\"cid:img001\" />", Encoding.UTF8, "text/html");
LinkedResource lr = new LinkedResource(@"E:\图片\pics\雷锋.jpg");
lr.ContentId = "img001";

view.LinkedResources.Add(lr);
mail.AlternateViews.Add(view);

//为邮件添加附件
Attachment at = new Attachment(@"D:\项目\chinatt315\members\qiyetupian\batianshengtai01.jpg");
Attachment at1 = new Attachment(@"D:\项目\chinatt315\2011315hd\qytp\piyopiyo2.jpg");
mail.Attachments.Add(at);
mail.Attachments.Add(at1);

SmtpClient smtp = new SmtpClient("pop.qq.com");
smtp.Credentials = new NetworkCredential("用户名", "密码$");

//为每个邮箱发送2封相同的邮件
for (int i = 0; i < 2; i++)
{
smtp.Send(mail);
}
Console.WriteLine("发送成功");
}
catch (Exception ex)
{
Console.WriteLine("发送失败!"+ex.Message);
}

Console.ReadKey();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐