C# 电子邮件发送方法

  在软件开发时,特别是业务系统,像ERP,CRM等系统,在运维时,依靠管理员每天固定的查询了解系统运行情况,往往会因为机械的操作产生疲劳感,而且这种重复的劳动完全可以依靠系统自身在系统空闲时来完成,然后产生一份每日的运维检查报告自身的运行情况。即时的告知管理员运行时所产生的问题。还有很多的系统报告,如销售报表等。在系统开发时,把检查程序设计进去,然后在运行时,利用电子邮件定时向管理员报告运行情况,这就有效的提供了系统管理的人性化,降低了工作强度。我在做ERP时,给客户实施对此很有感触,客户往往信息化水平并不高,管理员的往往也是让办公室的人顶的,依靠他们主动了解系统的运行情况,是很有风险的,让系统主动告诉管理员系统的运行情况,然后告知一些定义的处理方法,可以让管理员不必事事求着软件公司,作为软件开发方来说,客服的压力可以去掉好多。还有一点,一般来说,客户一般不允许软件公司运维时直接远程接入服务器,同时,除了错误,管理员又看不懂系统日志。系统出错之后,让系统主动把相应的错误日志也当做附件发送给管理员,在与软件公司技术人员沟通时,也会顺畅的多,同时也保证了客户的数据安全。

   电子邮件在系统管理时,还是最好的数据采集工具。

  开发环境:VS2005   .NET2.0

  项目下载地址:CSharpEmail.zip

/*****************************************************************/
/* 作者:虞晓杰
 * Emial:fishyxj@sina.com
 * 2011.9.28
 * 功能:发送自定义电子邮件,可以用于在系统发送自定义系统邮件。
/*****************************************************************/

using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace XFish.Utils
{
    /// <summary>  
    /// 电子邮件发送器类
    /// </summary>  
    public class EMailTransmitter
    {
        #region 类内部变量
        private MailMessage _MailMessage;
        private SmtpClient _SmtpClient;
        private string _Password;
        #endregion

        #region 类属性
        /// <summary>
        /// 邮件正文
        /// </summary>
        public string Body
        {
            get { return _MailMessage.Body; }
            set { _MailMessage.Body = value; }
        }
        /// <summary>
        /// 邮件标题
        /// </summary>
        public string Title
        {
            get { return _MailMessage.Subject; }
            set { _MailMessage.Subject = value; }
        }
        /// <summary>
        /// 发送密码
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        #endregion

        #region 类初始化
        /// <summary>  
        /// 处审核后类的实例  
        /// </summary>  
        /// <param name="To">收件人地址</param>  
        /// <param name="From">发件人地址</param>  
        /// <param name="Body">邮件正文</param>  
        /// <param name="Title">邮件的主题</param>  
        /// <param name="Password">发件人密码</param>  
        public EMailTransmitter(string to, string from, string body, string title, string password)
        {
            _MailMessage = new MailMessage();
            _MailMessage.To.Add(to);
            _MailMessage.From = new System.Net.Mail.MailAddress(from);
            _MailMessage.Subject = title;
            _MailMessage.Body = body;
            _MailMessage.IsBodyHtml = true;
            _MailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            _MailMessage.Priority = System.Net.Mail.MailPriority.Normal;
            _Password = password;
        }
        #endregion

        #region 类方法
        /// <summary>
        /// 增加接收方地址
        /// </summary>
        /// <param name="to">接收方的邮件地址</param>
        public void AddReceiver(string to)
        {
            MailAddress address = new MailAddress(to);
            if (!_MailMessage.To.Contains(address))
                _MailMessage.To.Add(to);
        }
        /// <summary>
        /// 删除指定的接收方地址
        /// </summary>
        /// <param name="to">接收方的邮件地址</param>
        public void RemoveReceiver(string to)
        {
            MailAddress address = new MailAddress(to);
            if (_MailMessage.To.Contains(address))
                _MailMessage.To.Remove(address);
        }
        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="Path">附件的路径</param>
        public void AddAttachments(string path)  
        {  
            string[] paths = path.Split(',');  
            Attachment data;  
            ContentDisposition disposition;  
            for (int i = 0; i < paths.Length; i++)  
            {  
                data = new Attachment(paths[i], MediaTypeNames.Application.Octet);//实例化附件  
                disposition = data.ContentDisposition;  
                disposition.CreationDate = System.IO.File.GetCreationTime(paths[i]);//获取 附件的创建日期  
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(paths[i]);//获取附件的修改日期  
                disposition.ReadDate = System.IO.File.GetLastAccessTime(paths[i]);//获取附件的读取日期  
                _MailMessage.Attachments.Add(data);//添加到附件中  
            }  
        }
        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="path">附件的路径</param>
        public void RemoveAttachments(string path)
        {
            string[] paths = path.Split(',');
            Attachment data;
            ContentDisposition disposition;  
            for (int i = 0; i < paths.Length; i++)
            {
                data = new Attachment(paths[i], MediaTypeNames.Application.Octet);
                disposition = data.ContentDisposition;  
                disposition.CreationDate = System.IO.File.GetCreationTime(paths[i]);//获取 附件的创建日期  
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(paths[i]);//获取附件的修改日期  
                disposition.ReadDate = System.IO.File.GetLastAccessTime(paths[i]);//获取附件的读取日期  
                if(_MailMessage.Attachments.Contains(data))
                    _MailMessage.Attachments.Remove(data);
            }
        }
        /// <summary>  
        /// 异步发送邮件  
        /// </summary>  
        /// <param name="CompletedMethod">完成后调用的事件</param>  
        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {
            if (_MailMessage != null)
            {
                _SmtpClient = new SmtpClient();
                _SmtpClient.Credentials = new System.Net.NetworkCredential(_MailMessage.From.Address, _Password);//设置发件人身份的票据  
                _SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                _SmtpClient.Host = "smtp." + _MailMessage.From.Host;
                _SmtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件  
                _SmtpClient.SendAsync(_MailMessage, _MailMessage.Body);
            }
        }
        /// <summary>  
        /// 发送邮件  
        /// </summary>  
        public void Send()
        {
            if (_MailMessage != null)
            {
                _SmtpClient = new SmtpClient();
                _SmtpClient.Credentials = new System.Net.NetworkCredential(_MailMessage.From.Address, _Password);//设置发件人身份的票据  
                _SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                _SmtpClient.Host = "smtp." + _MailMessage.From.Host;
                _SmtpClient.Send(_MailMessage);
            }
        }
        #endregion
    }
}

  

posted @ 2011-09-28 20:27  筱溪飞鱼  阅读(889)  评论(0编辑  收藏