通过C#发送邮件,可以根据自己的需求更改。
这个是个配置文件的类,可以用,也可以改,也可以不用。
using System;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace WaterCubeCheck{ [Serializable] public class dataStruct//配置结构 { public string mingcheng1 = "服务器1", mingcheng2 = "服务器2", lianjie1 = "Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123456", lianjie2 = "Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123456", Toaddress = "test@163.com,test1@163.com", Fromaddress = "test@139.com", Fromaddressname = "测试", MailSub = "测试", FromMailPassword = "123456", SmtpServer = "smtp.139.com"; public bool issendingEmail = false; public int starttime = 9; public int endtime = 19; public int sendtime = 60; public dataStruct() { mingcheng1 = "服务器1"; mingcheng2 = "服务器2"; lianjie1 = "Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123456"; lianjie2 = "Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123456"; Toaddress = "test@163.com"; Fromaddress = "test1@163.com"; Fromaddressname = "Server"; MailSub = "测试"; FromMailPassword = "123456"; SmtpServer = "smtp.163.com"; issendingEmail = false; starttime = 9; endtime = 19; sendtime = 60; } } [Serializable] public class MailBodyCon { public string machStr = ""; public string pattenStr = ""; public int count = 0; public MailBodyCon() { machStr = ""; pattenStr = ""; count = 0; } } public static class appConfig { public static dataStruct conf = new dataStruct(); public static MailBodyCon body = new MailBodyCon(); ////// 保存到配置文件 /// ///返回是否成功 public static bool savetofile() { try { string path = AppDomain.CurrentDomain.BaseDirectory; Stream fs = new FileStream(path + "app.cfg", FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, conf); fs.Close(); } catch { } return true; } ////// 读取配置文件 /// ///返回是否成功 public static bool readfromfile() { try { string path = AppDomain.CurrentDomain.BaseDirectory; Stream fs = new FileStream(path + "app.cfg", FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); conf = (dataStruct)formatter.Deserialize(fs); fs.Close(); } catch { } return true; } }}
这个是邮件内容的拼接以及发送类。
using System;using System.Collections.Generic;using System.Text;using System.Net.Mail;using System.Windows.Forms;using System.Data;using System.IO;namespace WaterCubeCheck{ public class MailClass { ////// 发送邮件 /// /// 邮件内容 public void SendStrMail(string data) { try { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); if (appConfig.conf.Toaddress.IndexOf(',') > -1) { string[] mails = appConfig.conf.Toaddress.Split(',');//多个收信地址用逗号隔开 for (int counti = 0; counti < mails.Length; counti++) { if (mails[counti].Trim() != "") { msg.To.Add(mails[counti]); } } } else { msg.To.Add(appConfig.conf.Toaddress);//添加单一收信地址 } msg.To.Add(appConfig.conf.Fromaddress); msg.From = new System.Net.Mail.MailAddress(appConfig.conf.Fromaddress, appConfig.conf.Fromaddressname, System.Text.Encoding.UTF8); /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ string sub = appConfig.conf.MailSub;//邮件标题 if (appConfig.body.count == 0) { msg.Subject = appConfig.conf.MailSub; } else { msg.Subject = data; } msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 //long tol = GateCount + HandCount; msg.Body = data; msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 msg.IsBodyHtml = true;//是否是HTML邮件 msg.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential(appConfig.conf.Fromaddress, appConfig.conf.FromMailPassword); //在zj.com注册的邮箱和密码 client.Host = appConfig.conf.SmtpServer;//邮件发送服务器,上面对应的是该服务器上的发信帐号和密码 object userState = msg; try { client.SendAsync(msg, userState);//开始发送 } catch (System.Net.Mail.SmtpException ex) { MessageBox.Show(ex.Message.ToString()); } } catch (Exception ee) { MessageBox.Show(ee.Message.ToString()); } } } //发送带表格邮件 public class SendMail { public static MailClass Mail=new MailClass(); public static string LargeMailBody="";//初始化多表格邮件内容 //单一表格邮件内容 public static void SendMsg(DataTable data) { string MailBody = "以下内容为系统自动发送,请勿直接回复,谢谢。
"; MailBody += "
"; MailBody += data.Columns[hcol].ColumnName; MailBody += " | "; } MailBody += "
"; MailBody += data.Rows[row][col].ToString(); MailBody += " | "; } MailBody += "
以下内容为系统自动发送,请勿直接回复,谢谢。
"; MailBody += "
"; MailBody += data.Columns[hcol].HeaderText.ToString(); MailBody += " | "; } MailBody += "
"; MailBody += data.Rows[row].Cells[col].Value.ToString(); MailBody += " | "; } MailBody += "
"+title+"
"; LargeMailBody += " "; LargeMailBody += " "; LargeMailBody += "
"; LargeMailBody += "
"; } //多表格邮件内容 public static void SendLargeMsg(DataGridView data, string title = "") { if (title != "") LargeMailBody += ""; LargeMailBody += data.Columns[hcol].ColumnName; LargeMailBody += " | "; } LargeMailBody += "
"; LargeMailBody += data.Rows[row][col].ToString(); LargeMailBody += " | "; } LargeMailBody += "
" + title + "
"; LargeMailBody += " "; LargeMailBody += " "; LargeMailBody += "
"; LargeMailBody += "
"; } } } "; LargeMailBody += data.Columns[hcol].HeaderText.ToString(); LargeMailBody += " | "; } LargeMailBody += "
"; LargeMailBody += data.Rows[row].Cells[col].Value.ToString(); LargeMailBody += " | "; } LargeMailBody += "
调用的时候用这个方法
private void button1_Click(object sender, EventArgs e) { t1 = databind(qishishijian.Value, jieshushijian.Value); t2 = databind1(qishishijian.Value, jieshushijian.Value); SendMail.LargeMailBody = ""; try { SendMail.SendLargeMsg(t1, "测试内容1); SendMail.SendLargeMsg(t2, "测试内容2"); SendMail.Mail.SendStrMail("以下内容为系统自动发送,请勿直接回复,谢谢。
" + SendMail.LargeMailBody); } catch { MessageBox.Show("没有可以发送的内容!"); } }
我在工作中经常用,大家可以改成自己的。