mail (2)

namespace zdm.DocLib.Business
{
    /// <summary>
    /// Summary description for SendMailLogic
    /// </summary>
    public class SendMailLogic
    {
        /// <summary>
        /// 单例访问成员变量
        /// </summary>
        private static readonly SendMailLogic instance = new SendMailLogic();

        /// <summary>
        /// 返回SendMailLogic的单个实例
        /// </summary>
        public static SendMailLogic Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SendMailLogic()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public SendMailResponse SendMail( SendMailRequest request )
        {
            try
            {
                MailMessage myMail = new MailMessage();
                myMail.From = new MailAddress( request.From );
                PaserMailAddress( request.To, myMail.To );
                PaserMailAddress( request.CC, myMail.CC );
                PaserMailAddress( request.Bcc, myMail.Bcc );
                myMail.Subject = request.Subject;
                myMail.Body = request.Body;

                if (request.Attachments != null)
                {
                    for (int i = 0; i < request.Attachments.Length; i++)
                    {
                        System.Net.Mail.Attachment mailAttach = new System.Net.Mail.Attachment(ByteArrayToStream(request.Attachments[i].FileData), request.Attachments[i].FileName);

                        myMail.Attachments.Add( mailAttach );
                    }
                }

                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings[BizConsts.APPKEY_SMTP_SERVER]))
                {
                    throw new ApplicationException( "SMTP服务器未设置" );
                }

                //Smtp Server
                SmtpClient mailClient = new SmtpClient(ConfigurationManager.AppSettings[BizConsts.APPKEY_SMTP_SERVER]);

                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[BizConsts.APPKEY_SMTP_SERVERPORT]))
                {
                    //端口号
                    try
                    {
                        mailClient.Port = Int32.Parse(ConfigurationManager.AppSettings[BizConsts.APPKEY_SMTP_SERVERPORT]);
                    }
                    catch
                    {
                        throw new ApplicationException("SMTP服务器端口设置错误,端口必须设置为数值型");
                    }
                }

                if ( CheckMailUserConfig() )
                {
                    //需要指定帐户信息
                    mailClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings[BizConsts.APPKEY_MAIL_USER], ConfigurationManager.AppSettings[BizConsts.APPKEY_MAIL_USERPW]);

                    mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                }
              
                mailClient.Send(myMail);

                SendMailResponse response = new SendMailResponse();

                response.ErrorCode = BizConsts.ERRCODE_SUCCESSFUL.ToString();
                response.ErrorMsg = string.Empty;

                return response;
            }
            catch (SmtpFailedRecipientsException e)
            {
                Trace.Write("无法发送邮件到所有邮件地址:" + e.Message);

                SendMailResponse response = new SendMailResponse();

                response.ErrorCode = BizConsts.ERRCODE_FAILED.ToString();
                response.ErrorMsg = e.Message;

                return response;
            }
            catch (SmtpFailedRecipientException e)
            {
                Trace.Write("无法发送邮件到个别邮件地址:" + e.Message);

                SendMailResponse response = new SendMailResponse();

                response.ErrorCode = BizConsts.ERRCODE_FAILED.ToString();
                response.ErrorMsg = e.Message;

                return response;
            }
            catch (SmtpException e)
            {
                Trace.Write("发送邮件时的Smtp异常:" + e.Message);

                SendMailResponse response = new SendMailResponse();

                response.ErrorCode = BizConsts.ERRCODE_FAILED.ToString();
                response.ErrorMsg = e.Message;

                return response;
            }
            catch (Exception e)
            {
                Trace.Write("发送邮件时的异常:" + e.Message);

                throw new ApplicationException( "发送邮件时的异常" , e );
            }
        }

        /// <summary>
        /// 解析分解邮件地址
        /// </summary>
        /// <returns></returns>
        private void PaserMailAddress( string mailAddress, MailAddressCollection mailCollection  )
        {
            if (string.IsNullOrEmpty(mailAddress) )
            {
                return;
            }

            char[] separator = new char[2] {',',';' };
            string[] addressArray = mailAddress.Split(separator);

            foreach (string address in addressArray )
            {
                if(address.Trim() == "")
                {
                    continue;
                }

                mailCollection.Add(new MailAddress(address ));
            }
        }

        /// <summary>
        /// 检查是否配置了邮件帐户
        /// </summary>
        /// <returns></returns>
        private bool CheckMailUserConfig()
        {
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[BizConsts.APPKEY_MAIL_USER]))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 字节数组转换为流
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        private Stream ByteArrayToStream( byte[] byteArray )
        {
            MemoryStream mStream = new MemoryStream(byteArray);

            return mStream;
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值