博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过C#发送自定义的html格式邮件
阅读量:5020 次
发布时间:2019-06-12

本文共 10934 字,大约阅读时间需要 36 分钟。

通过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 += "
"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { MailBody += "
"; } MailBody += "
"; for (int row = 0; row < data.Rows.Count; row++) { MailBody += "
"; for (int col = 0; col < data.Columns.Count; col++) { MailBody += "
"; } MailBody += "
"; } MailBody += "
   "; MailBody += data.Columns[hcol].ColumnName; MailBody += "   
   "; MailBody += data.Rows[row][col].ToString(); MailBody += "   
"; MailBody += ""; Mail.SendStrMail(MailBody); } //单一表格邮件内容 public static void SendMsg(DataGridView data) { string MailBody = "

以下内容为系统自动发送,请勿直接回复,谢谢。

"; MailBody += "
"; MailBody += "
"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { MailBody += "
"; } MailBody += "
"; for (int row = 0; row < data.Rows.Count; row++) { MailBody += "
"; for (int col = 0; col < data.Columns.Count; col++) { MailBody += "
"; } MailBody += "
"; } MailBody += "
   "; MailBody += data.Columns[hcol].HeaderText.ToString(); MailBody += "   
   "; MailBody += data.Rows[row].Cells[col].Value.ToString(); MailBody += "   
"; MailBody += ""; Mail.SendStrMail(MailBody); } //多表格邮件内容 public static void SendLargeMsg(DataTable data,string title="") { if (title != "") LargeMailBody += "

"+title+"

"; LargeMailBody += "
"; LargeMailBody += "
"; LargeMailBody += "
"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { LargeMailBody += "
"; } LargeMailBody += "
"; for (int row = 0; row < data.Rows.Count; row++) { LargeMailBody += "
"; for (int col = 0; col < data.Columns.Count; col++) { LargeMailBody += "
"; } LargeMailBody += "
"; } LargeMailBody += "
   "; LargeMailBody += data.Columns[hcol].ColumnName; LargeMailBody += "   
   "; LargeMailBody += data.Rows[row][col].ToString(); LargeMailBody += "   
"; LargeMailBody += "
"; } //多表格邮件内容 public static void SendLargeMsg(DataGridView data, string title = "") { if (title != "") LargeMailBody += "

" + title + "

"; LargeMailBody += "
"; LargeMailBody += "
"; LargeMailBody += "
"; for (int hcol = 0; hcol < data.Columns.Count; hcol++) { LargeMailBody += "
"; } LargeMailBody += "
"; for (int row = 0; row < data.Rows.Count; row++) { LargeMailBody += "
"; for (int col = 0; col < data.Columns.Count; col++) { LargeMailBody += "
"; } LargeMailBody += "
"; } LargeMailBody += "
   "; LargeMailBody += data.Columns[hcol].HeaderText.ToString(); 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("没有可以发送的内容!"); } }

我在工作中经常用,大家可以改成自己的。

转载于:https://www.cnblogs.com/StupidsCat/archive/2012/06/15/2550779.html

你可能感兴趣的文章
1.在数组中找到与给定总和的配对
查看>>
树的子结构
查看>>
关于根据Build Platform或者OS 加载x86或者x64 dll的问题
查看>>
程序员高效开发的几个技巧
查看>>
js-权威指南学习笔记19.2
查看>>
hexo 搭建博客
查看>>
关于 UIWebView 几个高级用法
查看>>
maven创建的项目中无法创建src/main/java 解决方案
查看>>
华为软件开发云测评报告二:代码检查
查看>>
集合1
查看>>
js 原生 ajax
查看>>
关键词 virtual
查看>>
建造者模式(屌丝专用)
查看>>
UVALive 4730 Kingdom +段树和支票托收
查看>>
公布windows的&quot;Universal Apps&quot; Unity3D游戏
查看>>
[APIO2010]特别行动队
查看>>
[SCOI2016]幸运数字
查看>>
SpringBoot 集成ehcache
查看>>
初步swift语言学习笔记2(可选类型?和隐式可选类型!)
查看>>
Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
查看>>