自己动手,一步一步实现邮件群发.NET源码(1)
--SmtpClient/MailMessage
作者:长江支流
关键字:.NET、邮件、群发
今天想给一些老朋友们发下邮件,在群里问大家有没群发工具,问了一会没人回答,于是自己就整了一个。现共享给需要的大伙。话不多说,直接代码贴上:
运行效果如下:
窗口:frmMailer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace GoldMailer
{
/// <summary>
/// 实现邮件的群发,并可以增加附件。
///
/// 作 者:长江支流(周方勇)
/// Email:flygoldfish@163.com QQ:150439795
/// 网 址:http://blog.youkuaiyun.com/flygoldfish
/// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
///
/// </summary>
public partial class frmMailer : Form
{
//提示对话框标题
private const string MSG_TITLE = "金质打印通邮件群发器";
//参数配置
private MailSetting m_Setting = null;
public frmMailer()
{
InitializeComponent();
}
private void frmMailer_Load(object sender, EventArgs e)
{
m_Setting = new MailSetting();
}
//发送邮件
private void btnSend_Click(object sender, EventArgs e)
{
if (txtTO.Text.Trim() == "")
{
MessageBox.Show("收件人不能为空,请重试!",MSG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtTO.Focus();
return;
}
if (MessageBox.Show("确认发送吗?", MSG_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.No)
{
return;
}
//发送邮件
SendMail();
}
//移除附件
private void btnRemoveFiles_Click(object sender, EventArgs e)
{
if (this.lstFiles.SelectedIndices.Count > 0)
{
this.lstFiles.Items.Remove(this.lstFiles.Items[this.lstFiles.SelectedIndices[0]]);
}
}
//增加附件
private void btnAddFiles_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
//单选
dlg.Multiselect = false;
if (dlg.ShowDialog() == DialogResult.OK)
{
this.lstFiles.Items.Add(dlg.FileName);
}
dlg.Dispose();
dlg = null;
}
//退出窗口
private void btnExit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确认退出?", MSG_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
this.Close();
}
}
//配置
private void btnConfig_Click(object sender, EventArgs e)
{
//自己做一个配置参数窗口对应MailSetting,或直接调用如下
WebMIS.FrameWork.Template.Config.frmConfigSetting frm = new WebMIS.FrameWork.Template.Config.frmConfigSetting();
frm.AfterSave += new EventHandler(frm_AfterSave);
frm.ShowDialog();
}
void frm_AfterSave(object sender, EventArgs e)
{
//加载配置信息
m_Setting.LoadConfig();
}
//发送邮件
private bool SendMail()
{
#region 实现...
//网络设置
SmtpClient smtp = new SmtpClient();
//将smtp的出站方式设为 Network
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = m_Setting.SmtpPort;
smtp.Host = m_Setting.SmtpServer;
smtp.EnableSsl = m_Setting.SmtpSSL;
//邮件认证,即邮箱用户名称密码
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(m_Setting.SenderMail, m_Setting.SenderPassword);
//邮件信息
MailMessage mm = new MailMessage();
mm.Priority = MailPriority.Normal;
//发件人
mm.From = new MailAddress(m_Setting.SenderMail, m_Setting.SenderDisplay);
//接收回复邮件
if (m_Setting.ReplyToMail != null && m_Setting.ReplyToMail != "")
{
//mm.ReplyTo = new MailAddress("MisGoldPrinter@163.com", "(答复)金质打印通");
mm.ReplyTo = new MailAddress(m_Setting.ReplyToMail, m_Setting.ReplyToDisplay);
}
//接收人、抄送、密送
//多个邮件英文逗号分开
//mm.CC.Add("FlyGoldFish@163.com,MISGoldPrinter@163.com");
//mm.CC.Add(new MailAddress("FlyGoldFish@163.com", "金质打印通,来自网易测试"));
try
{
mm.To.Add(ReplaceMailList(txtTO.Text));
if (txtCC.Text.Trim() != "")
{
mm.CC.Add(ReplaceMailList(txtCC.Text));
}
if (txtBCC.Text.Trim() != "")
{
mm.Bcc.Add(ReplaceMailList(txtBCC.Text));
}
}
catch (FormatException fe)
{
MessageBox.Show("有非邮件格式,请修正后重试!", MSG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
//主题
mm.Subject = txtSubject.Text;
//内容
//mm.SubjectEncoding = Encoding.Unicode;
//mm.BodyEncoding = Encoding.Unicode;
mm.IsBodyHtml = false;
mm.Body = txtBody.Text;
//mm.IsBodyHtml = true;
//mm.Body = "<font size = \"21\" color=\"blue\">长江支流金质打印通,准备升级</font>";
////,请http://goldprinter.taobao.com/查看注册
//附件
for (int i = 0; i < this.lstFiles.Items.Count; i++)
{
mm.Attachments.Add(new Attachment(this.lstFiles.Items[i].ToString()));
}
try
{
smtp.Send(mm);
MessageBox.Show("发送成功!", MSG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
MessageBox.Show("发送错误信息:" + ex.Message, MSG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtTO.Focus();
return false;
}
#endregion 实现...
}
private string ReplaceMailList(string mails)
{
//分隔符转为半角逗号分开
string strMails = mails;
strMails.Replace(';', ',');
strMails.Replace(';', ',');
strMails.Replace(',', ',');
return strMails;
}
}
}
相关源码下载信息请访问:
http://blog.youkuaiyun.com/flygoldfish
声明:本文版权为周方勇所有,欢迎转载,请保留完整的内容及出处。
flygoldfish@163.com