C# 发送邮件 操作类

using System;
using System.Collections.Generic;
using System.Text;

namespace XiaoFeng
{
    /*
   ===================================================================
      Author : jacky
      Email : [email protected]
      QQ : 7092734
      Site : www.zhuovi.com
   ===================================================================
   */
    /// 
    ///  发送邮件操作类
    /// Verstion : 1.0.0
    /// Author : jacky
    /// Email : [email protected]
    /// QQ : 7092734
    /// Site : www.zhuovi.com
    /// Create Time : 2018/04/10 17:25:39
    /// Update Time : 2018/04/10 17:25:39
    /// 
    public class Email
    {
        /// 
        /// 无参构造器
        /// 
        public Email() { }

        #region 属性
        /// 
        /// 发送到邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        private string _ToMail = "";
        /// 
        /// 发送到邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        public string ToMail { get { return _ToMail; } set { _ToMail = value; } }
        /// 
        /// 密送邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        private string _BCCMail = "";
        /// 
        /// 密送到邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        public string BCCMail { get { return _BCCMail; } set { _BCCMail = value; } }
        /// 
        /// 抄送邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        private string _CCMail = "";
        /// 
        /// 抄送到邮箱地址 如果是多个 用","分开 如:[email protected]:jacky,[email protected]:service
        /// 
        public string CCMail { get { return _CCMail; } set { _CCMail = value; } }
        /// 
        /// 发送邮箱地址
        /// 
        private string _FromMail = "";
        /// 
        /// 发送邮箱地址
        /// 
        public string FromMail { get { return _FromMail; } set { _FromMail = value; } }
        /// 
        ///  发送人姓名
        /// 
        private string _FromName = "";
        /// 
        /// 发送人姓名
        /// 
        public string FromName { get { return _FromName; } set { _FromName = value; } }
        /// 
        /// 标题
        /// 
        private string _Subject = "";
        /// 
        /// 标题
        /// 
        public string Subject { get { return _Subject; } set { _Subject = value; } }
        /// 
        /// 标题编码
        /// 
        private string _SubjectEncoding = "";
        /// 
        /// 标题编码
        /// 
        public string SubjectEncoding { get { return _SubjectEncoding; } set { _SubjectEncoding = value; } }
        /// 
        /// 内容
        /// 
        private string _Body = "";
        /// 
        /// 内容
        /// 
        public string Body { get { return _Body; } set { _Body = value; } }
        /// 
        /// 内容编码
        /// 
        private string _BodyEncoding = "";
        /// 
        /// 内容编码
        /// 
        public string BodyEncoding { get { return _BodyEncoding; } set { _BodyEncoding = value; } }
        /// 
        /// 用户帐号
        /// 
        private string _UserName = "";
        /// 
        /// 用户帐号
        /// 
        public string UserName { get { return _UserName; } set { _UserName = value; } }
        /// 
        /// 用户密码
        /// 
        private string _UserPwd = "";
        /// 
        /// 用户密码
        /// 
        public string UserPwd { get { return _UserPwd; } set { _UserPwd = value; } }
        /// 
        /// 邮件服务器
        /// 
        private string _SmtpHost = "";
        /// 
        /// 邮件服务器
        /// 
        public string SmtpHost { get { return _SmtpHost; } set { _SmtpHost = value; } }
        /// 
        /// 邮件服务器端口
        /// 
        private int _SmtpPort = 25;
        /// 
        /// 邮件服务器端口
        /// 
        public int SmtpPort { get { return _SmtpPort; } set { _SmtpPort = value; } }
        /// 
        /// 附件
        /// 
        private string[] _Attachments = null;
        /// 
        /// 附件
        /// 
        public string[] Attachments { get { return _Attachments; } set { _Attachments = value; } }
        /// 
        /// 邮件优先级
        /// 
        private System.Net.Mail.MailPriority _Priority = System.Net.Mail.MailPriority.Normal;
        /// 
        /// 邮件优先级
        /// 
        public System.Net.Mail.MailPriority Priority { get { return _Priority; } set { _Priority = value; } }
        /// 
        /// 发送错误信息
        /// 
        private string _ErrorMessage = "";
        /// 
        /// 发送错误信息
        /// 
        public string ErrorMessage { get { return _ErrorMessage; } set { _ErrorMessage = value; } }
        #endregion

        #region 发送邮件
        /// 
        /// 发送邮件
        /// 
        public Boolean Send()
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.From = new System.Net.Mail.MailAddress(this.FromMail, (this.FromName == "" ? this.UserName : this.FromName));
            message.To.Clear();
            foreach (string destemail in this.ToMail.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                if (destemail.Contains(":"))
                {
                    string[] mailInfo = destemail.Split(':');
                    message.To.Add(new System.Net.Mail.MailAddress(mailInfo[0].Trim(), mailInfo[1].Trim()));
                }
                else
                {
                    message.To.Add(destemail);
                }
            }
            if (this.CCMail != "")
            {
                message.CC.Clear();
                foreach (string destemail in this.CCMail.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    if (destemail.Contains(":"))
                    {
                        string[] mailInfo = destemail.Split(':');
                        message.To.Add(new System.Net.Mail.MailAddress(mailInfo[0].Trim(), mailInfo[1].Trim()));
                    }
                    else
                    {
                        message.CC.Add(destemail);
                    }
                }
            }
            if (this.BCCMail != "")
            {
                message.Bcc.Clear();
                foreach (string destemail in this.BCCMail.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    if (destemail.Contains(":"))
                    {
                        string[] mailInfo = destemail.Split(':');
                        message.To.Add(new System.Net.Mail.MailAddress(mailInfo[0].Trim(), mailInfo[1].Trim()));
                    }
                    else
                    {
                        message.Bcc.Add(destemail);
                    }
                }
            }
            message.Subject = this.Subject;//设置邮件主题 
            if (this.BodyEncoding != "") message.SubjectEncoding = Encoding.GetEncoding(this.SubjectEncoding);
            message.IsBodyHtml = true;//设置邮件正文为html格式 
            message.Body = this.Body;//设置邮件内容 

            if (this.BodyEncoding != "") message.BodyEncoding = Encoding.GetEncoding(this.BodyEncoding);
            //添加附件
            if (this.Attachments != null)
            {
                foreach (string path in this.Attachments)
                {
                    if (!string.IsNullOrEmpty(path))
                        message.Attachments.Add(new System.Net.Mail.Attachment(path));
                }
            }
            //邮件的优先级
            message.Priority = this.Priority;
            //邮件发送人地址
            message.Sender = message.From;
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(this.SmtpHost, this.SmtpPort);
            //设置发送邮件身份验证方式 
            //注意如果发件人地址是[email protected],则用户名是jacky而不是[email protected] 
            client.Credentials = new System.Net.NetworkCredential(this.UserName, this.UserPwd);
            try
            {
                client.Send(message);
                return true;
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
                return false;
            }
        }
        #endregion
    }
}

你可能感兴趣的:(c#)