技术资讯   > 技术文章 > EMAIL群发实例(本站作品)
[发表时间: 2008-07-14 01:34 ]

本EMAIL群发:

1\支持无限附件 2\支持记事和数据库批量导入 3\支持导航显示群发进度

核心代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data.OleDb;
using System.IO;
public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName == "")
        {
            System.Web.HttpContext.Current.Response.Write("请先选择文件!");
        }
        else
        {
            TextBox3.Text += FileUpload1.PostedFile.FileName + ",";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string err = "注意:";
        int errCount = 0;

        if (TextBox1.Text.Trim().Length == 0)
        {
            err += "标题不能为空!";
            errCount++;
        }
        if (FCKeditor1.Value.Trim().Length == 0)
        {
            err += "内容不能为空!";
            errCount++;
        }
        if (TextBox4.Text.Trim().Length == 0)
        {
            err += "收信人EMAIL不能为空!";
            errCount++;
        }

        if (errCount != 0)
        {
            System.Web.HttpContext.Current.Response.Write(err);
            return;


        }
      
        string quer="select * from setserver";
        DataRow dr=db.getrow(quer);
        SmtpClient sc = new SmtpClient();
        sc.Host = dr["server"].ToString();
        sc.UseDefaultCredentials = false;
        sc.Credentials = new NetworkCredential(dr["from"].ToString(), dr["pws"].ToString());
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;

        string strmail = TextBox4.Text;
        string[] arrmail = strmail.Split(new char[] { '\n' });
        for (int n = 0; n < arrmail.Length - 1; n++)
        {
            Regex myreg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); //email验证
            if(myreg.IsMatch(arrmail[n].ToString().Trim()))
            {

            MailMessage mm = new MailMessage(dr["from"].ToString(), arrmail[n].ToString().Trim());
            mm.Subject = TextBox1.Text;
            mm.Body = FCKeditor1.Value;
            mm.IsBodyHtml = true;
            mm.BodyEncoding = System.Text.Encoding.UTF8;
            if (TextBox3.Text != null && TextBox3.Text != "")
            {
                string stf = TextBox3.Text.ToString();
                string[] strfile = stf.Split(new char[] { ',' });

                for (int i = 0; i < strfile.Length - 1; i++)
                {
                    // string pat = FileUpload1.PostedFile.FileName;
                    // Label1.Text += strfile[i].ToString()+"   ";
                    Attachment dat = new Attachment(strfile[i].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
                    mm.Attachments.Add(dat);
                }
            }

            try
            {
                sc.Send(mm);
                db.processBar(n, arrmail.Length-1);
               // System.Web.HttpContext.Current.Response.Write("发送成功!");
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("有错误存在");
            }
        }
        }
        System.Web.HttpContext.Current.Response.Write("发送完成!");

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        if (RadioButton1.Checked)
        {
            OleDbDataReader dr = db.getread("select * from mail");
            while (dr.Read())
            {
                sb.Append(dr["mail"].ToString() + "\n");

            }
        }
        if (RadioButton2.Checked)
        {
       
            string patthh = FileUpload2.PostedFile.FileName;
            FileStream fs = new FileStream(patthh,FileMode.Open,FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
    
               while (!sr.EndOfStream)
               {
                   sb.Append(sr.ReadLine().Trim() + "\n");
               }
        
        }
        TextBox4.Text = sb.ToString();
      
      
    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        Panel1.Visible = true;
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        Panel1.Visible = false;
    }
}