- 浏览: 126917 次
- 性别:
- 来自: 深圳
文章分类
最新评论
C#批量发送邮件程序
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);
}
}
}
相关推荐
在C#编程环境中,控制台应用程序常常用于执行自动化任务,如批量发送邮件。这个特定的项目,"C# 控制台 批量发送邮件",是利用C#语言编写的一个控制台应用,其核心功能是实现批量发送电子邮件。开发者已经提供了一个...
在IT领域,C#是一种广泛使用...在实际项目中,根据需求可以进行相应的扩展和优化,如添加日志记录、邮件模板、批量发送等功能。希望这个C#发送邮件的小程序源码能对学习者有所启发,帮助大家更好地理解和应用相关技术。
通过以上步骤,我们可以实现一个C#程序,它能从Excel文件中读取邮箱和数据,然后批量发送个性化的邮件。在实际项目中,你可能还需要考虑日志记录、邮件发送状态跟踪等更复杂的需求,以确保邮件发送的可靠性和可维护...
通过本篇文章,我们将深入了解如何使用C#实现自动发送邮件,并涵盖邮件的批量发送、抄送以及附件添加等功能。 #### 二、关键知识点解析 ##### 1. 发件人与收件人的设置 在发送邮件前,我们需要定义发件人(`...
在了解了基本的邮件发送流程后,开发者可以进一步研究如何优化邮件发送的性能,如批量发送、错误处理和重试机制等。此外,还可以探索更高级的功能,如使用OAuth2进行身份验证,或者集成第三方邮件服务API,如...
通过这份源码,开发者可以学习到如何在C#环境下构建一个邮件发送系统,包括设置邮件服务器、配置SMTP协议、处理邮件附件以及实现批量发送等核心知识点。 首先,邮件发送的核心是SMTP(Simple Mail Transfer ...
对于批量发送邮件,可以通过循环遍历收件人列表,每次迭代都创建一个新的MailMessage实例并发送。同时,为了提高性能,可以考虑使用异步发送邮件的方法,如SmtpClient的SendAsync方法,这样在发送邮件的同时,程序...
8. **性能优化**:既然描述提到该程序比网页发送快数倍,那么它可能采用了批量发送、缓存或线程池等技术来提升效率。分析源码可以帮助我们了解这些优化策略。 总的来说,这个C#邮件发送程序提供了一个实用的邮件...
通过深入研究这个基于C#的发送邮件程序,你可以不仅掌握邮件发送的基本原理,还能了解到如何将实际的业务需求转化为代码,提升自己的编程能力。同时,这个项目也提供了一个分享知识和经验的良好平台,促进了开发者...
然而,实际应用中可能还需要考虑其他因素,如邮件队列管理、批量发送、重试机制、日志记录等,以确保邮件发送的稳定性和可靠性。在开发时,记得遵循良好的编程实践,确保代码的可读性和可维护性。
这个项目实现了一个简单的邮件发送系统,它具有多附件上传的功能,适用于那些需要通过程序自动化发送电子邮件的场景。让我们深入探讨一下其中涉及的关键知识点。 1. **C#基础**: C#是微软公司推出的面向对象的...
批量发送邮件的小工具是一种实用程序,它允许用户一次性向多个收件人发送电子邮件,极大地提高了工作效率,特别是在需要进行大规模通信时,例如企业推广、活动通知或新闻通讯等场景。这个小工具的核心功能包括: 1....
本资源提供了一个群发邮件的C#源码,它可以帮助开发者学习和实现批量发送电子邮件的功能。以下是对这个主题的详细解释: 一、C#编程基础 C#是微软公司推出的面向对象的编程语言,基于.NET框架。它具有类型安全、...
【C#邮件群发winform程序】是一款基于C#编程语言和.NET框架开发的应用程序,专为实现电子邮件的批量发送而设计。它提供了一个用户友好的Windows桌面界面,方便用户进行邮件管理和群发操作。以下是该程序的关键知识点...
批量发送邮件时,你可以将收件人信息存储在一个数据结构中,如列表或数据库,然后遍历这些信息,为每个收件人创建并发送邮件。注意,为了避免被当作垃圾邮件发送者,应遵循邮件发送的最佳实践,限制发送频率,并确保...
开发者可以根据需求自定义邮件内容,实现批量发送或定时发送等功能。 7. 学习与研究 对于初学者,理解并实践这个程序能够帮助掌握C#网络编程的基础,了解SMTP协议的工作原理。同时,通过阅读和分析代码,可以学习到...
### C#发送邮件方法详解:支持附件、已读回执与批量发送 在现代软件开发中,电子邮件功能是许多应用程序的重要组成部分,无论是用于用户验证、密码重置还是通知消息,邮件服务都扮演着不可或缺的角色。在.NET框架下...
在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows应用程序、Web应用程序以及网络服务时。本主题聚焦于如何使用C#通过POP3协议来实现邮件的接收,并且能够下载其中的附件。POP3(Post Office Protocol ...
在C#中开发一个邮件发送服务是一项常见的任务,尤其对于需要定期或批量发送邮件的应用程序而言。本系统采用C#编程语言,构建了一个稳定、可靠的邮件服务,可以在系统后台运行,并且支持开机自启动,确保服务的连续性...
C#群发邮件程序是一种基于C#编程语言开发的应用,主要功能是实现批量发送电子邮件。在电子邮件营销、通知发送或日常工作中,这种程序能够极大提高效率,避免手动逐一发送的繁琐过程。它允许用户导入Excel(通常是...