C#批量发送邮件程序

来源:互联网 发布:sql server select 编辑:程序博客网 时间:2024/05/16 19:25

Form1.cs
//c#批量发送邮件程序.远程抓取网页源代码,作为邮件内容.首先批量从文本文件中读取客户邮件地址,然后发送

安装network平台,,添加引用 System.Web.Dll

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Web.Mail;
using System.Net;
using System.Text;

namespace WebMail
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MenuItem menuItem4;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.ListBox listBox2;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.TextBox textBox4;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.TextBox textBox5;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.TextBox textBox6;
  private System.Windows.Forms.TextBox textBox7;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.menuItem4 = new System.Windows.Forms.MenuItem();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.listBox2 = new System.Windows.Forms.ListBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.label3 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.label5 = new System.Windows.Forms.Label();
   this.textBox4 = new System.Windows.Forms.TextBox();
   this.button3 = new System.Windows.Forms.Button();
   this.label6 = new System.Windows.Forms.Label();
   this.button4 = new System.Windows.Forms.Button();
   this.textBox5 = new System.Windows.Forms.TextBox();
   this.button5 = new System.Windows.Forms.Button();
   this.label7 = new System.Windows.Forms.Label();
   this.label8 = new System.Windows.Forms.Label();
   this.textBox6 = new System.Windows.Forms.TextBox();
   this.textBox7 = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   //
   // mainMenu1
   //
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem1});
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem2,
                       this.menuItem3,
                       this.menuItem4});
   this.menuItem1.Text = "文件";
   //
   // menuItem2
   //
   this.menuItem2.Index = 0;
   this.menuItem2.Text = "检测数据库邮件地址文件";
   //
   // menuItem3
   //
   this.menuItem3.Index = 1;
   this.menuItem3.Text = "检测文本邮件地址文件";
   this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // menuItem4
   //
   this.menuItem4.Index = 2;
   this.menuItem4.Text = "退出程序";
   this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 72);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(96, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "目标邮件地址列表";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(336, 72);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(120, 16);
   this.label2.TabIndex = 1;
   this.label2.Text = "邮件发送成功列表";
   //
   // listBox1
   //
   this.listBox1.HorizontalScrollbar = true;
   this.listBox1.ItemHeight = 12;
   this.listBox1.Location = new System.Drawing.Point(16, 104);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(168, 196);
   this.listBox1.TabIndex = 2;
   //
   // listBox2
   //
   this.listBox2.HorizontalScrollbar = true;
   this.listBox2.ItemHeight = 12;
   this.listBox2.Location = new System.Drawing.Point(464, 104);
   this.listBox2.Name = "listBox2";
   this.listBox2.Size = new System.Drawing.Size(168, 196);
   this.listBox2.TabIndex = 3;
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(208, 104);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(104, 32);
   this.button1.TabIndex = 4;
   this.button1.Text = "获取邮件地址";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(336, 152);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(104, 32);
   this.button2.TabIndex = 5;
   this.button2.Text = "发送促销邮件";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(16, 16);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(104, 16);
   this.label3.TabIndex = 6;
   this.label3.Text = "邮件列表文件地址";
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(144, 8);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(400, 21);
   this.textBox1.TabIndex = 7;
   this.textBox1.Text = "";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(24, 312);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(96, 24);
   this.label4.TabIndex = 8;
   this.label4.Text = "邮件标题:";
   //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(152, 312);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(352, 21);
   this.textBox2.TabIndex = 9;
   this.textBox2.Text = "这里填写各语种邮件标题";
   //
   // textBox3
   //
   this.textBox3.BackColor = System.Drawing.SystemColors.Info;
   this.textBox3.Location = new System.Drawing.Point(24, 344);
   this.textBox3.Multiline = true;
   this.textBox3.Name = "textBox3";
   this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.textBox3.Size = new System.Drawing.Size(480, 152);
   this.textBox3.TabIndex = 10;
   this.textBox3.Text = "这里填写邮件内容";
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(16, 40);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(96, 16);
   this.label5.TabIndex = 11;
   this.label5.Text = "HTML文件地址";
   //
   // textBox4
   //
   this.textBox4.Location = new System.Drawing.Point(144, 40);
   this.textBox4.Name = "textBox4";
   this.textBox4.Size = new System.Drawing.Size(400, 21);
   this.textBox4.TabIndex = 12;
   this.textBox4.Text = "http://www.gamesavor.com/promotion/gamesavorEP0003_english.html";
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(336, 104);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(104, 32);
   this.button3.TabIndex = 13;
   this.button3.Text = "获取网页代码";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // label6
   //
   this.label6.Location = new System.Drawing.Point(24, 512);
   this.label6.Name = "label6";
   this.label6.Size = new System.Drawing.Size(96, 24);
   this.label6.TabIndex = 14;
   this.label6.Text = "邮件内容长度";
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(392, 512);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(96, 24);
   this.button4.TabIndex = 15;
   this.button4.Text = "计算邮件长度";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // textBox5
   //
   this.textBox5.Location = new System.Drawing.Point(136, 512);
   this.textBox5.Name = "textBox5";
   this.textBox5.TabIndex = 16;
   this.textBox5.Text = "0";
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(208, 152);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(104, 32);
   this.button5.TabIndex = 17;
   this.button5.Text = "删除邮件地址";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // label7
   //
   this.label7.Location = new System.Drawing.Point(208, 208);
   this.label7.Name = "label7";
   this.label7.Size = new System.Drawing.Size(80, 24);
   this.label7.TabIndex = 18;
   this.label7.Text = "邮件总数";
   //
   // label8
   //
   this.label8.Location = new System.Drawing.Point(208, 256);
   this.label8.Name = "label8";
   this.label8.Size = new System.Drawing.Size(80, 24);
   this.label8.TabIndex = 19;
   this.label8.Text = "完成数量";
   //
   // textBox6
   //
   this.textBox6.Location = new System.Drawing.Point(312, 208);
   this.textBox6.Name = "textBox6";
   this.textBox6.Size = new System.Drawing.Size(64, 21);
   this.textBox6.TabIndex = 20;
   this.textBox6.Text = "0";
   //
   // textBox7
   //
   this.textBox7.Location = new System.Drawing.Point(312, 256);
   this.textBox7.Name = "textBox7";
   this.textBox7.Size = new System.Drawing.Size(64, 21);
   this.textBox7.TabIndex = 21;
   this.textBox7.Text = "0";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(640, 557);
   this.Controls.Add(this.textBox7);
   this.Controls.Add(this.textBox6);
   this.Controls.Add(this.label8);
   this.Controls.Add(this.label7);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.textBox5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.label6);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.textBox4);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.listBox2);
   this.Controls.Add(this.listBox1);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "系统批量发送HTML邮件";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {

   this.Menu=mainMenu1;
   textBox1.Text=System.Environment.CurrentDirectory+"//Email.txt";

  }

  private void menuItem3_Click(object sender, System.EventArgs e)
  {
  
  }

  private void menuItem4_Click(object sender, System.EventArgs e)
  {
   this.Close();

  }

  private void button1_Click(object sender, System.EventArgs e)
  {
  
   string txtFilePath=textBox1.Text;
   string strline="";

   StreamReader srReadLine = new StreamReader(
      (System.IO.Stream)File.OpenRead(txtFilePath),
     System.Text.Encoding.ASCII);

     srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);

     while (srReadLine.Peek() > -1)
   {
   strline=srReadLine.ReadLine();
   listBox1.Items.Add(strline);

     }
     srReadLine.Close();
   textBox6.Text=listBox1.Items.Count.ToString();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
  
   for(int i=0;i<listBox1.Items.Count;i++)
   {
   String MailToAddress1=(string)listBox1.Items[i];
    if (MailToAddress1 != "")
    {
     SendEmail(MailToAddress1);
    }

   } 
   textBox7.Text=listBox2.Items.Count.ToString(); 

  }

  public void SendEmail(string MailToAddress)
  {
   

   string MailFromAddress="csharp222@126.com";
   string MailTitle=textBox2.Text;
   string MailContent=textBox3.Text;

   string MailServer="smtp.126.com";
   string MailUserName="csharp222";
   string MailPassword="123456";


   MailMessage mailobject = new MailMessage();
   mailobject.To = MailToAddress;
   mailobject.From = MailFromAddress;

   mailobject.Subject =MailTitle;
   mailobject.Body =MailContent;
   mailobject.BodyFormat=MailFormat.Html;


   mailobject.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
   mailobject.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailUserName); //set your username here
   mailobject.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailPassword); //set your password here
   
   SmtpMail.SmtpServer = MailServer; 
   SmtpMail.Send(mailobject);
   listBox2.Items.Add(MailToAddress);
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   string strHtmlPagePath=textBox4.Text;

   int at = 0;
   int start = 0;
   int notei=0;
   int endi=0;
   int[] myIntArray = new int[5];
   while((start < strHtmlPagePath.Length) && (at > -1))
   {
    at = strHtmlPagePath.IndexOf('/', start);
    if (at == -1) break;    
    myIntArray[notei]=at;
    start = at+1;
    notei = notei+1;
    endi=at;
   }
   
   textBox2.Text=strHtmlPagePath.Substring(0,endi);
   string strEmailContent=GetWeather(strHtmlPagePath);
   string strEmailContentFileName=Application.StartupPath+"//EmailContent.txt";
 
   StreamWriter strwriterobj=File.CreateText(strEmailContentFileName);
   strwriterobj.Write(strEmailContent);
   strwriterobj.Close();
   textBox3.Text =strEmailContent;

  }

  private void button4_Click(object sender, System.EventArgs e)
  {
  textBox5.Text=GetLength(textBox3.Text).ToString();
  }
  int GetLength (string strTest)
  {
   ASCIIEncoding ascii = new ASCIIEncoding();
   byte [] byteArr = ascii.GetBytes(strTest);  /*利用ASCII对字符串编码并转化为一个   
                                                  二进制数组*/
   int i, length=0;
   for (i = 0; i < strTest.Length; i++)
   {
    length++;
   }
   return length;
  }

  public string GetWeather(string url)
  {
   string weacherhtml=String.Empty;
   HttpWebRequest rt=null;
   HttpWebResponse rs=null;
   Stream stream=null;
   StreamReader sr=null;
 
   rt=(HttpWebRequest)WebRequest.Create(url);
   rs=(HttpWebResponse)rt.GetResponse();
   stream=rs.GetResponseStream();
   sr=new StreamReader(stream,System.Text.Encoding.Default);
   weacherhtml=sr.ReadToEnd();
   sr.Close();
   stream.Close();
   rs.Close();
   return weacherhtml;

  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   listBox1.Items.Remove(listBox1.SelectedItem);
  }

 }
}
 

原创粉丝点击