前两天有个朋友让我帮他找个邮件群发工具,自己感觉找起来麻烦,就动手帮他写了一个。该工具使C#编写,与数据库相结合实现群发功能。
界面的设计很简单,只需在WinForm上拖几个TextBox控件和一个RichTextBox控件,让用户填写邮件的标题和内容,再添加一个按钮双击后添加如下代码:
如此之外,还需要在Config文件中添加如下的配置信息:
代码中引入了.NET中的命名空间System.Net.Mail,微软在这个命名空间下提供了相关的Class来实现邮件发送功能,有兴趣的朋友可以到MSDN上查看相关介绍.
界面的设计很简单,只需在WinForm上拖几个TextBox控件和一个RichTextBox控件,让用户填写邮件的标题和内容,再添加一个按钮双击后添加如下代码:
MailMessage message = new MailMessage();
message.Subject = this.textBoxSubject.Text.Trim();
message.From = new MailAddress(this.textBoxFromMail.Text.Trim());
message.Body = this.richTextBoxContent.Text.Trim();
message.IsBodyHtml = true;
string strConnection = "Server=(local);initial catalog=mail;user id=sa;password=sa;Connect Timeout=30";
SqlConnection objConnection=new SqlConnection(strConnection);
try
{
objConnection.Open();
MessageBox.Show("数据库连接成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
SqlCommand sqlCommand = new SqlCommand("Select * from mail", objConnection);
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
message.To.Add(reader["address"].ToString());
MessageBox.Show("地址添加成功!");
}
objConnection.Close();
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
MessageBox.Show("邮件发送成功!");
message.Subject = this.textBoxSubject.Text.Trim();
message.From = new MailAddress(this.textBoxFromMail.Text.Trim());
message.Body = this.richTextBoxContent.Text.Trim();
message.IsBodyHtml = true;
string strConnection = "Server=(local);initial catalog=mail;user id=sa;password=sa;Connect Timeout=30";
SqlConnection objConnection=new SqlConnection(strConnection);
try
{
objConnection.Open();
MessageBox.Show("数据库连接成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
SqlCommand sqlCommand = new SqlCommand("Select * from mail", objConnection);
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
message.To.Add(reader["address"].ToString());
MessageBox.Show("地址添加成功!");
}
objConnection.Close();
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
MessageBox.Show("邮件发送成功!");
如此之外,还需要在Config文件中添加如下的配置信息:
<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="smtp.163.com" password="" port="25" userName="" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
<system.net>
<mailSettings>
<smtp>
<network host="smtp.163.com" password="" port="25" userName="" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
代码中引入了.NET中的命名空间System.Net.Mail,微软在这个命名空间下提供了相关的Class来实现邮件发送功能,有兴趣的朋友可以到MSDN上查看相关介绍.
转载于:https://blog.51cto.com/ssbird/56249