Beispiel #1
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
        }
        public static string GetMailInfoStr(MailInfoType type)
        {
            string str = String.Empty;

            switch (type)
            {
                #region 会 导致发送邮件错误的信息

            case MailInfoType.FromEmpty:
                str = "发信人的电子邮件地址不能为空。";
                break;

            case MailInfoType.ToEmpty:
                str = "收件人的电子邮件地址不能为空。";
                break;


            case MailInfoType.SmtpClientEmpty:
                str = "SmtpClient 实例未设置,不能发送邮件。";
                break;

            case MailInfoType.HostEmpty:
                str = "SMTP主服务器未设置,不能发送邮件。";
                break;

            case MailInfoType.CertificateEmpty:
                str = "SmtpClient 实例设置启用ssl,证书不能为空。";
                break;


            case MailInfoType.FromFormat:
                str = "发信人\"{0}\"的电子邮件地址\"{1}\"格式错误。";
                break;

            case MailInfoType.ToFormat:
                str = "收件人\"{0}\"的电子邮件地址\"{1}\"格式错误。";
                break;

                #endregion

                #region  会 导致发送邮件错误的信息

            case MailInfoType.SubjectEmpty:
                str = "待发送邮件主题为空,请确认是否继续发送。";
                break;

            case MailInfoType.BodyEmpty:
                str = "待发送邮件内容为空,请确认是否继续发送。";
                break;

            case MailInfoType.CCFormat:
                str = "抄送人\"{0}\"的电子邮件地址\"{1}\"格式错误。";
                break;

            case MailInfoType.BccFormat:
                str = "密送人\"{0}\"的电子邮件地址\"{1}\"格式错误。";
                break;

                #endregion

                //#region SmtpClient.Send 异常

                //case MailInfoType.SmtpEx:
                //    str = "SmtpException异常,状态码SmtpStatusCode:{0},异常详细信息为:{1}.";
                //    break;
                //case MailInfoType.SmtpOperationEx:
                //    str = "InvalidOperationException异常,异常详细信息为:{0}.";
                //    break;
                //case MailInfoType.SmtpFailedRecipientsEx:
                //    str = "SmtpFailedRecipientsException异常,异常详细信息为:{0}.";
                //    break;
                //case MailInfoType.SmtpDisposedEx:
                //    str = "SmtpDisposedEx异常,异常详细信息为:{0}.";
                //    break;

                //#endregion
            }

            return(str);
        }