前言:OA自动发送邮件的程序不好使,SQL Server 存储过程邮件发送程序好使,但是不利于标准化和维护,于是觉得是适合找些“轮子”武装一下。想起之前学习过System.Net.Mail ,于是再次重温一下。
class="code_img_closed" src="/Upload/Images/2014092316/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('32715867-2be8-4b64-a4dc-78859184dea2',event)" src="/Upload/Images/2014092316/2B1B950FA3DF188F.gif" alt="" />using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Mail; using DevComponents.DotNetBar; namespace SendMailSystem { class MailSendClass { string mto; //收件人地址 string mfrom; //发件人地址 string mtitle; //邮件标题 string mbody; //邮件内容 string msmtp; //SMTP地址 string msmtpuid; //发件人用户名 string msmtppwd; //发件人密码 string mattach; //附件内容 /// <summary> /// 邮件发送参数值 /// </summary> /// <param name="mailto">收件人地址</param> /// <param name="mailfrom">发件人地址</param> /// <param name="mailtitle">邮件标题</param> /// <param name="mailbody">邮件内容</param> /// <param name="mailattch">附件内容</param> /// <param name="smtp">SMTP地址</param> /// <param name="smtpuid">发件人用户名</param> /// <param name="smtppwd">发件人密码</param> public void MailSend(string mailto, string mailfrom, string mailtitle, string mailbody,string mailattch,string smtp, string smtpuid, string smtppwd) { mto = mailto; mfrom = mailfrom; mtitle = mailtitle; mbody = mailbody; msmtp = smtp; msmtpuid = smtpuid; msmtppwd = smtppwd; mattach = mailattch; } public void mSend() { SmtpClient client = new SmtpClient(msmtp); //实例化 Smtp发送服务器 类 client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过远程SMTP服务器传送邮件,这里的network表示你要使用的远程SMTP服务器。 client.UseDefaultCredentials = true; //Smtp服务器需要验证 client.Credentials = new NetworkCredential(msmtpuid, msmtppwd); //验证 发件人账号和密码 //MailMessage mmsg = new MailMessage(new MailAddress(mfrom), new MailAddress(mto)); //单个发送时 MailMessage mmsg = new MailMessage(); //实例化 Smtp客户端 类 mmsg.Priority = MailPriority.High;//邮件优先级 mmsg.From = new MailAddress(mfrom, "ERP系统", Encoding.GetEncoding(936));//收件方看到的邮件来源,可以修改接收地址 //mmsg.CC.Add(mto);//邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号 分开 mmsg.To.Add(mto); //邮件的收件者 mmsg.Subject = mtitle; //邮件标题 mmsg.SubjectEncoding = Encoding.UTF8; mmsg.Body = mbody; //正文 mmsg.IsBodyHtml = true; //邮件正文是否是HTML格式 mmsg.BodyEncoding = Encoding.UTF8; mmsg.IsBodyHtml = true; if (mattach != "") { string[] arrayS = mattach.ToString().Substring(0, mattach.ToString().Length - 1).Split(','); for (int i = 0; i < arrayS.Length; i++) { mmsg.Attachments.Add(new Attachment(arrayS[i].ToString())); } } try { client.Send(mmsg); MessageBoxEx.Show("邮件发送成功!"); } catch (Exception e) { MessageBoxEx.Show("邮件发送失败:\n" + e.Message); } } } }Class Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; namespace SendMailSystem { public partial class Form1 : Office2007Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void buttonX1_Click(object sender, EventArgs e) { MailSendClass classl = new MailSendClass(); string mailaddress = ""; if (listBox1.Items.Count >0) { for (int i = 0; i < listBox1.Items.Count; i ++ ) { mailaddress += listBox1.Items[i].ToString() + ","; } } classl.MailSend(mailto1.Text, mailfrom1.Text, mailtitle1.Text, mailbody1.Text, mailaddress, smtp1.Text, smtpuid1.Text, smtppwd1.Text); classl.mSend(); } private void buttonX2_Click(object sender, EventArgs e) { string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "D:\\Patch"; openFileDialog1.Filter = "All files (*.*)|*.*"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) resultFile = openFileDialog1.FileName; if (resultFile != "") { //mattach1.Text += resultFile + ","; listBox1.Items.Add(resultFile); } //ListViewItem li = new ListViewItem(); //li.Text = resultFile; //li.SubItems.Add(resultFile); //listViewEx1.Items.Add(li); } private void buttonX3_Click(object sender, EventArgs e) { //if (mattach1.Text.ToString() != "") //{ //string[] arrayS = mattach1.Text.ToString().Substring(0, mattach1.Text.ToString().Length-1).Split(','); //for (int i = 0; i < arrayS.Length; i++) //{ // MessageBoxEx.Show(arrayS[i].ToString()); //} //} if (listBox1.Items.Count >0) { string mailaddress = ""; if (listBox1.Items.Count > 0) { for (int i = 0; i < listBox1.Items.Count; i++) { mailaddress += listBox1.Items[i].ToString() + ","; } } MessageBoxEx.Show(mailaddress); } } private void buttonX4_Click(object sender, EventArgs e) { if (listBox1.Items.Count >0) { if (listBox1.SelectedIndex >= 0) { listBox1.Items.RemoveAt(listBox1.SelectedIndex); } } } private void buttonX5_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { listBox1.Items.Clear(); } } } }View Code
最后,上图。