国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#使用MailAddress類發送html格式郵件的實例代碼

2020-01-24 03:03:37
字體:
來源:轉載
供稿:網友

1.首先引入命名空間using System.Net.Mail;
2.將發送的郵件的功能封裝成一個類,該類中包含了發送郵件的基本功能:收件人(多人),抄送(多人),發送人,主題,郵件正文,附件等,封裝的Email類如下:

復制代碼 代碼如下:

public class Email
    {
        /// <summary>
        /// 發送者
        /// </summary>
        public string mailFrom { get; set; }

        /// <summary>
        /// 收件人
        /// </summary>
        public string[] mailToArray { get; set; }

        /// <summary>
        /// 抄送
        /// </summary>
        public string[] mailCcArray { get; set; }

        /// <summary>
        /// 標題
        /// </summary>
        public string mailSubject { get; set; }

        /// <summary>
        /// 正文
        /// </summary>
        public string mailBody { get; set; }

        /// <summary>
        /// 發件人密碼
        /// </summary>
        public string mailPwd { get; set; }

        /// <summary>
        /// SMTP郵件服務器
        /// </summary>
        public string host { get; set; }

        /// <summary>
        /// 正文是否是html格式
        /// </summary>
        public bool isbodyHtml { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        public string[] attachmentsPath { get; set; }

        public bool Send()
        {
            //使用指定的郵件地址初始化MailAddress實例
            MailAddress maddr = new MailAddress(mailFrom);
            //初始化MailMessage實例
            MailMessage myMail = new MailMessage();


            //向收件人地址集合添加郵件地址
            if (mailToArray != null)
            {
                for (int i = 0; i < mailToArray.Length; i++)
                {
                    myMail.To.Add(mailToArray[i].ToString());
                }
            }

            //向抄送收件人地址集合添加郵件地址
            if (mailCcArray != null)
            {
                for (int i = 0; i < mailCcArray.Length; i++)
                {
                    myMail.CC.Add(mailCcArray[i].ToString());
                }
            }
            //發件人地址
            myMail.From = maddr;

            //電子郵件的標題
            myMail.Subject = mailSubject;

            //電子郵件的主題內容使用的編碼
            myMail.SubjectEncoding = Encoding.UTF8;

            //電子郵件正文
            myMail.Body = mailBody;

            //電子郵件正文的編碼
            myMail.BodyEncoding = Encoding.Default;

            myMail.Priority = MailPriority.High;

            myMail.IsBodyHtml = isbodyHtml;

            //在有附件的情況下添加附件
            try
            {
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件時有錯誤:" + err);
            }

            SmtpClient smtp = new SmtpClient();
            //指定發件人的郵件地址和密碼以驗證發件人身份
            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);


            //設置SMTP郵件服務器
            smtp.Host = host;

            try
            {
                //將郵件發送到SMTP郵件服務器
                smtp.Send(myMail);
                return true;

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                return false;
            }

        }
    }

3.頁面調用發送郵件的類

復制代碼 代碼如下:

protected void Send_Click(object sender, EventArgs e)
        {
            Email email = new Email();
            email.mailFrom = "發送人的郵箱地址";
            email.mailPwd = "發送人郵箱的密碼";
            email.mailSubject = "郵件主題";
            email.mailBody = "郵件內容";
            email.isbodyHtml = true;    //是否是HTML
            email.host = "smtp.126.com";//如果是QQ郵箱則:smtp:qq.com,依次類推
            email.mailToArray = new string[] { "******@qq.com","12345678@qq.com"};//接收者郵件集合
            email.mailCcArray = new string[] { "******@qq.com" };//抄送者郵件集合
            if (email.Send())
            {
                Response.Write("<script type='text/javascript'>alert('發送成功!');history.go(-1)</script>");//發送成功則提示返回當前頁面;

            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('發送失敗!');history.go(-1)</script>");
            }
        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 自贡市| 瓮安县| 靖江市| 黄浦区| 游戏| 上杭县| 张家界市| 青阳县| 郓城县| 漯河市| 新安县| 北辰区| 娱乐| 延津县| 巩义市| 苏尼特右旗| 贵定县| 光山县| 中阳县| 彭山县| 灵台县| 平原县| 康定县| 于田县| 荆州市| 社旗县| 福泉市| 弥渡县| 衡山县| 吉林市| 湖州市| 宁都县| 大洼县| 甘孜县| 娄底市| 泸定县| 九龙坡区| 交口县| 玛纳斯县| 广德县| 天祝|