欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用MailAddress类发送html格式邮件的实例代码

程序员文章站 2024-02-22 10:56:16
1.首先引入命名空间using system.net.mail;2.将发送的邮件的功能封装成一个类,该类中包含了发送邮件的基本功能:收件人(多人),抄送(多人),发送人,主...

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>");
            }
        }