通过C#发送自定义的html格式邮件
生活随笔
收集整理的這篇文章主要介紹了
通过C#发送自定义的html格式邮件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
通過C#發(fā)送郵件,可以根據(jù)自己的需求更改。
這個是個配置文件的類,可以用,也可以改,也可以不用。
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary;namespace WaterCubeCheck {[Serializable]public class dataStruct//配置結構{public stringmingcheng1 = "服務器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 stringmachStr = "";public stringpattenStr = "";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;}} }這個是郵件內容的拼接以及發(fā)送類。
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>/// 發(fā)送郵件/// </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個參數(shù)分別是發(fā)件人地址(可以隨便寫),發(fā)件人姓名,編碼*/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;//郵件發(fā)送服務器,上面對應的是該服務器上的發(fā)信帳號和密碼object userState = msg;try{client.SendAsync(msg, userState);//開始發(fā)送}catch (System.Net.Mail.SmtpException ex){MessageBox.Show(ex.Message.ToString());}}catch (Exception ee){MessageBox.Show(ee.Message.ToString());}}}//發(fā)送帶表格郵件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\">以下內容為系統(tǒng)自動發(fā)送,請勿直接回復,謝謝。</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\">以下內容為系統(tǒng)自動發(fā)送,請勿直接回復,謝謝。</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\">以下內容為系統(tǒng)自動發(fā)送,請勿直接回復,謝謝。</p>" + SendMail.LargeMailBody);}catch { MessageBox.Show("沒有可以發(fā)送的內容!"); }}?
總結
以上是生活随笔為你收集整理的通过C#发送自定义的html格式邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何正确认识真理和价值的关系(如何正确认
- 下一篇: C# 代理HTTP请求