注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 用了十年的QQ号,第二次被..
 帮助

使用C#编写邮件发送工具


2007-12-23 19:49:56
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://ssbird.blog.51cto.com/277690/56249
    前两天有个朋友让我帮他找个邮件群发工具,自己感觉找起来麻烦,就动手帮他写了一个。该工具使C#编写,与数据库相结合实现群发功能。
    界面的设计很简单,只需在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("邮件发送成功!");
   
    如此之外,还需要在Config文件中添加如下的配置信息:
   
<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上查看相关介绍.

本文出自 “Running...” 博客,请务必保留此出处http://ssbird.blog.51cto.com/277690/56249





    文章评论
 
2007-12-23 21:36:23
用C#编还是头一次看

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: