通过C#发送自定义的html格式邮件
通过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(); /// <summary> /// 保存到配置文件 /// </summary> /// <returns>返回是否成功</returns> 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; } /// <summary> /// 读取配置文件 /// </summary> /// <returns>返回是否成功</returns> 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 { /// <summary> /// 发送邮件 /// </summary> /// <param name="data">邮件内容</param> 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 = "<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">"; MailBody += "<div align=\"center\">"; MailBody += "<tr>"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { MailBody += "<td bgcolor=\"999999\"> "; MailBody += data.Columns[hcol].ColumnName; MailBody += " </td>"; } MailBody += "</tr>"; for (int row = 0; row < data.Rows.Count; row++) { MailBody += "<tr>"; for (int col = 0; col < data.Columns.Count; col++) { MailBody += "<td bgcolor=\"dddddd\"> "; MailBody += data.Rows[row][col].ToString(); MailBody += " </td>"; } MailBody += "</tr>"; } MailBody += "</table>"; MailBody += "</div>"; Mail.SendStrMail(MailBody); } //单一表格邮件内容 public static void SendMsg(DataGridView data) { string MailBody = "<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">"; MailBody += "<div align=\"center\">"; MailBody += "<tr>"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { MailBody += "<td bgcolor=\"999999\"> "; MailBody += data.Columns[hcol].HeaderText.ToString(); MailBody += " </td>"; } MailBody += "</tr>"; for (int row = 0; row < data.Rows.Count; row++) { MailBody += "<tr>"; for (int col = 0; col < data.Columns.Count; col++) { MailBody += "<td bgcolor=\"dddddd\"> "; MailBody += data.Rows[row].Cells[col].Value.ToString(); MailBody += " </td>"; } MailBody += "</tr>"; } MailBody += "</table>"; MailBody += "</div>"; Mail.SendStrMail(MailBody); } //多表格邮件内容 public static void SendLargeMsg(DataTable data,string title="") { if (title != "") LargeMailBody += "<p style=\"font-size: 10pt\">"+title+"</p>"; LargeMailBody += "<div align=\"center\">"; LargeMailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">"; LargeMailBody += "<tr>"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { LargeMailBody += "<td bgcolor=\"999999\"> "; LargeMailBody += data.Columns[hcol].ColumnName; LargeMailBody += " </td>"; } LargeMailBody += "</tr>"; for (int row = 0; row < data.Rows.Count; row++) { LargeMailBody += "<tr>"; for (int col = 0; col < data.Columns.Count; col++) { LargeMailBody += "<td bgcolor=\"dddddd\"> "; LargeMailBody += data.Rows[row][col].ToString(); LargeMailBody += " </td>"; } LargeMailBody += "</tr>"; } LargeMailBody += "</table><br>"; LargeMailBody += "</div>"; } //多表格邮件内容 public static void SendLargeMsg(DataGridView data, string title = "") { if (title != "") LargeMailBody += "<p style=\"font-size: 10pt\">" + title + "</p>"; LargeMailBody += "<div align=\"center\">"; LargeMailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">"; LargeMailBody += "<tr>"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { LargeMailBody += "<td bgcolor=\"999999\"> "; LargeMailBody += data.Columns[hcol].HeaderText.ToString(); LargeMailBody += " </td>"; } LargeMailBody += "</tr>"; for (int row = 0; row < data.Rows.Count; row++) { LargeMailBody += "<tr>"; for (int col = 0; col < data.Columns.Count; col++) { LargeMailBody += "<td bgcolor=\"dddddd\"> "; LargeMailBody += data.Rows[row].Cells[col].Value.ToString(); LargeMailBody += " </td>"; } LargeMailBody += "</tr>"; } LargeMailBody += "</table><br>"; LargeMailBody += "</div>"; } } }
调用的时候用这个方法
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("<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p>" + SendMail.LargeMailBody); } catch { MessageBox.Show("没有可以发送的内容!"); } }
我在工作中经常用,大家可以改成自己的。