Ejemplo n.º 1
0
        /// <summary>
        /// 发送邮件前检查需要设置的信息是否完整,收集 错误 信息
        /// </summary>
        /// <param name="dicMsg">将检查信息收集到此集合</param>
        private void InnerCheckSendMail4Error(Dictionary <MailInfoType, string> dicMsg)
        {
            // 注意每个验证使用完 infoBuilder 都要清零 infoBuilder 。
            StringBuilder infoBuilder = new StringBuilder(128);

            this.InnerCheckAddress(infoBuilder, dicMsg, EmailAddrType.From);
            this.InnerCheckAddress(infoBuilder, dicMsg, EmailAddrType.To);

            // SmtpClient 实例未设置
            if (m_SmtpClient == null)
            {
                dicMsg.Add(MailInfoType.SmtpClientEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.SmtpClientEmpty));
            }
            else
            {
                // SMTP 主服务器设置  (默认端口为25)
                if (m_SmtpClient.Host.Length == 0)
                {
                    dicMsg.Add(MailInfoType.HostEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.HostEmpty));
                }
                // SMPT 凭证
                if (m_SmtpClient.EnableSsl && m_SmtpClient.ClientCertificates.Count == 0)
                {
                    dicMsg.Add(MailInfoType.CertificateEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.CertificateEmpty));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送邮件前检查需要设置的信息是否完整,收集 提示 信息
        /// </summary>
        /// <param name="dicMsg">将检查信息收集到此集合</param>
        private void InnerCheckSendMail4Info(Dictionary <MailInfoType, string> dicMsg)
        {
            // 注意每个验证使用完 infoBuilder 都要清零 infoBuilder 。
            StringBuilder infoBuilder = new StringBuilder(128);

            this.InnerCheckAddress(infoBuilder, dicMsg, EmailAddrType.CC);
            this.InnerCheckAddress(infoBuilder, dicMsg, EmailAddrType.Bcc);

            // 邮件主题
            if (Subject.Length == 0)
            {
                dicMsg.Add(MailInfoType.SubjectEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.SubjectEmpty));
            }

            // 邮件内容
            if (Body.Length == 0 &&
                (m_Attachments == null || (m_Attachments != null && m_Attachments.Count == 0))
                )
            {
                dicMsg.Add(MailInfoType.BodyEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.BodyEmpty));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 检查 发件人、收件人、抄送人、密送人 邮箱地址
        /// </summary>
        /// <param name="infoBuilder">StringBuilder实例</param>
        /// <param name="dicMsg">将检查信息收集到此集合</param>
        /// <param name="type">接收邮件地址类型</param>
        private void InnerCheckAddress(StringBuilder infoBuilder, Dictionary <MailInfoType, string> dicMsg, EmailAddrType type)
        {
            Dictionary <string, string> dic = null;
            MailInfoType addressFormat      = MailInfoType.None;
            MailInfoType addressEmpty       = MailInfoType.None;
            bool         allowEmpty         = true;
            // 只有 发件人 是单个地址,特别进行处理
            bool hasHandle = false;

            switch (type)
            {
            case EmailAddrType.From:
            {
                // 标识为已处理
                hasHandle = true;

                allowEmpty = false;
                if (From.Length == 0)
                {
                    dicMsg.Add(MailInfoType.FromEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.FromEmpty));
                }
                else if (!MailValidatorHelper.IsEmail(From))
                {
                    string strTemp = infoBuilder.AppendFormat(MailInfoHelper.GetMailInfoStr(MailInfoType.FromFormat), FromDisplayName, From).ToString();
                    dicMsg.Add(MailInfoType.FromFormat, strTemp);
                    infoBuilder.Length = 0;
                }
            }
            break;

            case EmailAddrType.To:
            {
                dic          = m_DicTo;
                addressEmpty = MailInfoType.ToEmpty;

                allowEmpty    = false;
                addressFormat = MailInfoType.ToFormat;
            }
            break;

            case EmailAddrType.CC:
            {
                dic           = m_DicCC;
                addressFormat = MailInfoType.CCFormat;

                allowEmpty   = true;
                addressEmpty = MailInfoType.None;
            }
            break;

            case EmailAddrType.Bcc:
            {
                dic           = m_DicBcc;
                addressFormat = MailInfoType.BccFormat;

                allowEmpty   = true;
                addressEmpty = MailInfoType.None;
            }
            break;
            }


            #region 处理 收件人、抄送人、密送人

            if (!hasHandle)
            {
                if (dic == null)
                {
                    if (!allowEmpty)
                    {
                        // 地址为空
                        dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                    }
                }
                else
                {
                    if (dic.Count > 0)
                    {
                        string strTemp = String.Empty;
                        // 邮件地址格式
                        foreach (KeyValuePair <string, string> keyValue in dic)
                        {
                            if (keyValue.Key.Length == 0)
                            {
                                if (!allowEmpty)
                                {
                                    // 地址为空
                                    dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                                }
                            }
                            else if (!MailValidatorHelper.IsEmail(keyValue.Key))
                            {
                                if (strTemp.Length == 0)
                                {
                                    strTemp = MailInfoHelper.GetMailInfoStr(addressFormat);
                                }
                                if (infoBuilder.Length > 0)
                                {
                                    infoBuilder.AppendLine();
                                }
                                infoBuilder.AppendFormat(strTemp, keyValue.Value, keyValue.Key);
                            }
                        }
                        if (infoBuilder.Length > 0)
                        {
                            dicMsg.Add(addressFormat, infoBuilder.ToString());
                            infoBuilder.Length = 0;
                        }
                    }
                    else if (!allowEmpty)
                    {
                        // 地址为空
                        dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                    }
                }
            }

            #endregion
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 异步发送邮件
        /// </summary>
        /// <param name="isSimple">是否只发送一条</param>
        /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param>
        /// <param name="isReuse">是否重用SmtpClient</param>
        private void SendMessageAsync(MailHelper mail, bool isSimple, string shiyan, string msgCount, bool autoReleaseSmtp, bool isReuse)
        {
            LogHelper.Info(String.Format("{0}:{1}\"异步\"邮件开始。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine));

            if (!isReuse || !mail.ExistsSmtpClient())
            {
                SmtpClient client = new SmtpHelper(EmailConfig.TestEmailType, false, EmailConfig.TestUserName, EmailConfig.TestPassword).SmtpClient;
                mail.AsycUserState    = String.Format("{0}:{1}\"异步\"邮件", shiyan, msgCount);
                client.SendCompleted += (send, args) =>
                {
                    AsyncCompletedEventArgs arg = args;

                    if (arg.Error == null)
                    {
                        // 需要注意的事使用 MailHelper 发送异步邮件,其UserState是 MailUserState 类型
                        LogHelper.Info(((MailUserState)args.UserState).UserState.ToString() + "已发送完成." + watch.ElapsedMilliseconds + Environment.NewLine);
                    }
                    else
                    {
                        LogHelper.Info(String.Format("{0} 异常:{1}{2}{3}"
                                                     , ((MailUserState)args.UserState).UserState.ToString() + "发送失败."
                                                     , (arg.Error.InnerException == null ? arg.Error.Message : arg.Error.Message + arg.Error.InnerException.Message)
                                                     , watch.ElapsedMilliseconds, Environment.NewLine));
                        // 标识异常已处理,否则若有异常,会抛出异常
                        ((MailUserState)args.UserState).IsErrorHandle = true;
                    }
                };
                mail.SetSmtpClient(client, autoReleaseSmtp);
            }
            else
            {
                mail.AsycUserState = String.Format("{0}:{1}\"异步\"邮件已发送完成。", shiyan, msgCount);
            }

            mail.From            = EmailConfig.TestFromAddress;
            mail.FromDisplayName = EmailConfig.GetAddressName(EmailConfig.TestFromAddress);

            string to  = m_to;
            string cc  = m_cc;
            string bcc = m_bcc;

            if (to.Length > 0)
            {
                mail.AddReceive(EmailAddrType.To, to, EmailConfig.GetAddressName(to));
            }
            if (cc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.CC, cc, EmailConfig.GetAddressName(cc));
            }
            if (bcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.Bcc, bcc, EmailConfig.GetAddressName(bcc));
            }

            mail.Subject = m_Subject;
            // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件
            mail.Body       = m_Body + "<div style='display:none'>" + Guid.NewGuid() + "</div>";
            mail.IsBodyHtml = true;

            if (filePaths != null && filePaths.Count > 0)
            {
                foreach (string filePath in FilePaths)
                {
                    mail.AddAttachment(filePath);
                }
            }

            Dictionary <MailInfoType, string> dic = mail.CheckSendMail();

            if (dic.Count > 0 && MailInfoHelper.ExistsError(dic))
            {
                // 反馈“错误+提示”信息
                LogHelper.Info(MailInfoHelper.GetMailInfoStr(dic));
            }
            else
            {
                string msg = String.Empty;
                if (dic.Count > 0)
                {
                    // 反馈“提示”信息
                    msg = MailInfoHelper.GetMailInfoStr(dic);
                }

                try
                {
                    // 发送
                    if (isSimple)
                    {
                        mail.SendOneMail();
                    }
                    else
                    {
                        mail.SendBatchMail();
                    }
                }
                catch (Exception ex)
                {
                    // 反馈异常信息
                    LogHelper.Info(String.Format("{0}\"异步\"异常:({1}){2}{3}", msgCount, watch.ElapsedMilliseconds, ex.Message, Environment.NewLine));
                }
                finally
                {
                    // 输出到界面
                    if (msg.Length > 0)
                    {
                        LogHelper.Info(msg + Environment.NewLine);
                    }
                }
            }

            mail.Reset();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="isSimple">是否只发送一条</param>
        /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param>
        /// <param name="isReuse">是否重用SmtpClient</param>
        private void SendMessage(MailHelper mail, bool isSimple, ArrayList list, Mail_Test.Model.maillist mailinfo, bool autoReleaseSmtp, bool isReuse)
        {
            mail.IsBodyHtml = true;

            string bcc = "";//密送

            if (mailinfo.mto.Length > 0)
            {
                mail.AddReceive(EmailAddrType.To, mailinfo.mto, mailinfo.toname);
            }
            if (mailinfo.mcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.CC, mailinfo.mcc, mailinfo.ccname);
            }

            if (bcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.Bcc, bcc, Config.GetAddressName(bcc));
            }

            mail.Subject = mailinfo.title;

            // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件
            mail.Body            = mailinfo.mailcontent;
            mail.From            = mailinfo.mfrom;
            mail.FromDisplayName = mailinfo.fromname;

            if (!isReuse || !mail.ExistsSmtpClient())
            {
                mail.SetSmtpClient(
                    new SmtpHelper(Config.TestEmailType, false, Config.TestUserName, Config.TestPassword).SmtpClient
                    , autoReleaseSmtp
                    );
            }

            if (list != null && list.Count > 0)
            {
                foreach (string filePath in list)
                {
                    mail.AddAttachment(filePath);
                }
            }


            Dictionary <MailInfoType, string> dic = mail.CheckSendMail();

            if (dic.Count > 0 && MailInfoHelper.ExistsError(dic))
            {
                // 反馈“错误+提示”信息
                ViewBag.msg += MailInfoHelper.GetMailInfoStr(dic);
            }
            else
            {
                string msg = String.Empty;
                if (dic.Count > 0)
                {
                    // 反馈“提示”信息
                    ViewBag.msg = MailInfoHelper.GetMailInfoStr(dic);
                }

                try
                {
                    if (isSimple)
                    {
                        mail.SendOneMail();
                    }
                    else
                    {
                        // 发送
                        mail.SendBatchMail();
                    }
                }
                catch (Exception ex)
                {
                    // 反馈异常信息
                    ViewBag.msg += (ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message) + Environment.NewLine;
                }
                finally
                {
                    // 输出到界面
                    if (msg.Length > 0)
                    {
                        ViewBag.msg += Environment.NewLine;
                    }
                }
            }

            mail.Reset();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="isSimple">是否只发送一条</param>
        /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param>
        /// <param name="isReuse">是否重用SmtpClient</param>
        private void SendMessage(MailHelper mail, bool isSimple, string shiyan, string msgCount, bool autoReleaseSmtp, bool isReuse)
        {
            AppendReplyMsg(String.Format("{0}:{1}\"同步\"邮件开始。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine));

            if (!isReuse || !mail.ExistsSmtpClient())
            {
                mail.SetSmtpClient(
                    new SmtpHelper(Config.TestEmailType, false, Config.TestUserName, Config.TestPassword).SmtpClient
                    , autoReleaseSmtp
                    );
            }

            mail.From            = Config.TestFromAddress;
            mail.FromDisplayName = Config.GetAddressName(Config.TestFromAddress);

            string to  = m_to;
            string cc  = m_cc;
            string bcc = m_bcc;

            if (to.Length > 0)
            {
                mail.AddReceive(EmailAddrType.To, to, Config.GetAddressName(to));
            }
            if (cc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.CC, cc, Config.GetAddressName(cc));
            }
            if (bcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.Bcc, bcc, Config.GetAddressName(bcc));
            }

            mail.Subject = m_Subject;
            // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件
            mail.Body       = m_Body + Guid.NewGuid();
            mail.IsBodyHtml = true;

            if (filePaths != null && filePaths.Count > 0)
            {
                foreach (string filePath in FilePaths)
                {
                    mail.AddAttachment(filePath);
                }
            }

            Dictionary <MailInfoType, string> dic = mail.CheckSendMail();

            if (dic.Count > 0 && MailInfoHelper.ExistsError(dic))
            {
                // 反馈“错误+提示”信息
                AppendReplyMsg(MailInfoHelper.GetMailInfoStr(dic));
            }
            else
            {
                string msg = String.Empty;
                if (dic.Count > 0)
                {
                    // 反馈“提示”信息
                    msg = MailInfoHelper.GetMailInfoStr(dic);
                }

                try
                {
                    if (isSimple)
                    {
                        mail.SendOneMail();
                    }
                    else
                    {
                        // 发送
                        mail.SendBatchMail();
                    }
                    AppendReplyMsg(String.Format("{0}:{1}\"同步\"邮件已发送完成。{2}{3}", shiyan, msgCount, watch.ElapsedMilliseconds, Environment.NewLine));
                }
                catch (Exception ex)
                {
                    // 反馈异常信息
                    msg = msg + (ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message) + Environment.NewLine;
                }
                finally
                {
                    // 输出到界面
                    if (msg.Length > 0)
                    {
                        AppendReplyMsg(msg + Environment.NewLine);
                    }
                }
            }

            mail.Reset();
        }