码迷,mamicode.com
首页 > Web开发 > 详细

网页获取邮箱并实现qq群发功能

时间:2015-07-03 15:34:26      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

转载请标明出处!

技术分享

技术分享

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void send(string sender, string pwd, string receiver, string title, string content)
        {
            SmtpClient client = new SmtpClient();//邮箱传输协议类
            client.Host = "smtp.qq.com";//邮件服务器
            client.Port = 25;//smtp主机上的端口号,默认是25.
            client.DeliveryMethod = SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器
            client.Credentials = new NetworkCredential(sender, pwd); //凭证,发件人登录邮箱的用户名和密码

            MailAddress Sender = new MailAddress(sender); //发件人Email,在邮箱是显示
            MailAddress Receiver = new MailAddress(receiver); //收件人Email,在邮箱是显示
            MailMessage mailMessage = new MailMessage(sender, receiver); //创建一个电子邮件类
            mailMessage.Subject =txttitle.Text;//标题
            mailMessage.Body = txtcontent.Text;//内容可为html格式文本
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码
            mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码
            mailMessage.IsBodyHtml = true;//邮件内容是否为html格式
            mailMessage.Priority = MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示).
            //添加附件
            if (file!=null)
            {
                Attachment ss = new Attachment(file); //file是所选择的文件的路径    Attachment电子邮件的附件
                mailMessage.Attachments.Add(ss);
            }
            //Attachment ss = new Attachment(file); //file是所选择的文件的路径    Attachment电子邮件的附件
            //mailMessage.Attachments.Add(ss);
            try
            {
                client.Send(mailMessage);//发送邮件
                mailMessage.Dispose();//释放资源 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtadd.Text))
            {
                MessageBox.Show("请添加收件人邮箱");
            }
            else
            {
                ListViewItem lt = new ListViewItem();
                lt.Text = txtadd.Text.Trim();
                listView1.Items.Add(lt);
                txtadd.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                send(txtsend.Text, txtpwd.Text, listView1.Items[i].Text, txtadd.Text,txtcontent.Text);
                Thread.Sleep(2000);
            }
            MessageBox.Show("发送成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtsend.Text = "434418081@qq.com";
            txtpwd.UseSystemPasswordChar = true;
             textbiaoti.Text = "http://1111.baidu.com/p/3860763762";
        }
        string file;
        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Word文件.doc|*.doc|表格文件.xls|*.xls|所有文件|*.*"; //筛选文件类型
            DialogResult dr = openFileDialog1.ShowDialog();  //创建一个枚举类型的变量dr来接收打开这个对话框
            if (dr == DialogResult.OK) //如果点的是确定,才会执行下面的代码
            {
                file = openFileDialog1.FileName;//定义一个变量来接收路径
                //创建文件流 来将路径转换
                StreamReader path = new StreamReader(file, Encoding.Default); //Encoding.Default 编码方式
                MessageBox.Show("添加附件成功");
                path.Close();//用完流之后必须关闭
            }
        }

        //---------------------------------------------------------------
        public String GetWebContent(String url, Encoding encode)// encode 编码
        {
            url = textbiaoti.Text.Trim(); //需要获取源代码的网页  
            WebRequest request = WebRequest.Create(url); //WebRequest.Create方法,返回WebRequest的子类HttpWebRequest  
            WebResponse response = request.GetResponse(); //WebRequest.GetResponse方法,返回对 Internet 请求的响应  
            Stream resStream = response.GetResponseStream(); //WebResponse.GetResponseStream 方法,从 Internet 资源返回数据流。   
            Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312  
            StreamReader sr = new StreamReader(resStream, enc); //命名空间:System.IO。 StreamReader 类实现一个 TextReader (TextReader类,表示可读取连续字符系列的读取器),使其以一种特定的编码从字节流中读取字符。   
            txtshow.Text = sr.ReadToEnd(); //输出(HTML代码),ContentHtml为Multiline模式的TextBox控件  
            return txtshow.Text;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string x = GetWebContent(textbiaoti.Text, Encoding.UTF8);
                txtshow.Text = "";
                Regex r = new Regex(@"([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})");//正则 
                System.Text.RegularExpressions.MatchCollection mc = r.Matches(x);
                for (int i = 0; i < mc.Count; i++)
                {
                    if (mc.Count == 0)
                    {
                        txtshow.Text = "未查询到符合邮箱地址的信息。";
                    }
                    else
                    {
                        txtshow.Text += mc[i].Value + "\r\n";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取网站信息错误:" + ex.Message);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ListViewItem lt = new ListViewItem();
            lt.Text = txtshow.SelectedText;
            listView1.Items.Add(lt);
        }
    }
}

 

网页获取邮箱并实现qq群发功能

标签:

原文地址:http://www.cnblogs.com/Mr-xue/p/4618701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!