欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年11月27日 14:50:30   作者:  
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧

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

復(fù)制代碼 代碼如下:

public class Email
    {
        /// <summary>
        /// 發(fā)送者
        /// </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>
        /// 發(fā)件人密碼
        /// </summary>
        public string mailPwd { get; set; }

        /// <summary>
        /// SMTP郵件服務(wù)器
        /// </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());
                }
            }
            //發(fā)件人地址
            myMail.From = maddr;

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

            //電子郵件的主題內(nèi)容使用的編碼
            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();
            //指定發(fā)件人的郵件地址和密碼以驗證發(fā)件人身份
            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);


            //設(shè)置SMTP郵件服務(wù)器
            smtp.Host = host;

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

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

        }
    }

3.頁面調(diào)用發(fā)送郵件的類

復(fù)制代碼 代碼如下:

protected void Send_Click(object sender, EventArgs e)
        {
            Email email = new Email();
            email.mailFrom = "發(fā)送人的郵箱地址";
            email.mailPwd = "發(fā)送人郵箱的密碼";
            email.mailSubject = "郵件主題";
            email.mailBody = "郵件內(nèi)容";
            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('發(fā)送成功!');history.go(-1)</script>");//發(fā)送成功則提示返回當前頁面;

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

相關(guān)文章

最新評論