Beispiel #1
0
        /// <summary>
        /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址)
        /// </summary>
        /// <param name="type">类型:收件人、抄送人、密送人</param>
        /// <param name="dicAddress">Email地址,显示名称</param>
        public void AddReceive(EmailAddrType type, Dictionary <string, string> dicAddress)
        {
            MailValidatorHelper.ValideArgumentNull <Dictionary <string, string> >(dicAddress, "dicAddress");
            if (dicAddress.Count > 0)
            {
                Dictionary <string, string> dic = null;
                switch (type)
                {
                case EmailAddrType.To:
                    dic = DicTo;
                    break;

                case EmailAddrType.CC:
                    dic = DicCC;
                    break;

                case EmailAddrType.Bcc:
                    dic = DicBcc;
                    break;

                case EmailAddrType.From:
                    throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR);
                }

                foreach (KeyValuePair <string, string> keyValue in dicAddress)
                {
                    MailValidatorHelper.ValideStrNullOrEmpty(keyValue.Key, "dicAddress", MailValidatorHelper.EMAIL_ADDRESS_DIC_ERROR);
                    if (dic.Count > 0 && !dic.ContainsKey(keyValue.Key))
                    {
                        dic.Add(keyValue.Key, keyValue.Value);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址)
        /// </summary>
        /// <param name="type">类型:收件人、抄送人、密送人</param>
        /// <param name="addressList">Email地址列表</param>
        public void AddReceive(EmailAddrType type, IEnumerable <string> addressList)
        {
            MailValidatorHelper.ValideArgumentNull <IEnumerable <string> >(addressList, "addressList");

            if (addressList.Count() > 0)
            {
                Dictionary <string, string> dic = null;
                switch (type)
                {
                case EmailAddrType.To:
                    dic = DicTo;
                    break;

                case EmailAddrType.CC:
                    dic = DicCC;
                    break;

                case EmailAddrType.Bcc:
                    dic = DicBcc;
                    break;

                case EmailAddrType.From:
                    throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR);
                }

                foreach (string address in addressList)
                {
                    MailValidatorHelper.ValideStrNullOrEmpty(address, "addressList", MailValidatorHelper.EMAIL_ADDRESS_LIST_ERROR);
                    if (dic.Count > 0 && !dic.ContainsKey(address))
                    {
                        dic.Add(address, String.Empty);
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址)
        /// </summary>
        /// <param name="type">类型:收件人、抄送人、密送人</param>
        /// <param name="address">Email地址</param>
        /// <param name="displayName">显示名称</param>
        public void AddReceive(EmailAddrType type, string address, string displayName)
        {
            MailValidatorHelper.ValideStrNullOrEmpty(address, "address");

            Dictionary <string, string> dic = null;

            switch (type)
            {
            case EmailAddrType.To:
                dic = DicTo;
                break;

            case EmailAddrType.CC:
                dic = DicCC;
                break;

            case EmailAddrType.Bcc:
                dic = DicBcc;
                break;

            case EmailAddrType.From:
                throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR);
            }

            if (dic.Count == 0 || !dic.ContainsKey(address))
            {
                dic.Add(address, displayName);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 将收件人、抄送人、密送人添加到 MailMessage 中
        /// </summary>
        /// <param name="type">收件人、抄送人、密送人</param>
        /// <param name="mMailMessage">待发送的MailMessage类</param>
        private void InnerSetAddress(EmailAddrType type, MailMessage mMailMessage)
        {
            MailAddressCollection       receiveCol = null;
            Dictionary <string, string> dicReceive = null;
            bool hasAddress = false;

            switch (type)
            {
            case EmailAddrType.To:
            {
                if (m_DicTo != null && m_DicTo.Count > 0)
                {
                    dicReceive = m_DicTo;
                    receiveCol = mMailMessage.To;
                    hasAddress = true;
                }
            }
            break;

            case EmailAddrType.CC:
            {
                if (m_DicCC != null && m_DicCC.Count > 0)
                {
                    dicReceive = m_DicCC;
                    receiveCol = mMailMessage.CC;
                    hasAddress = true;
                }
            }
            break;

            case EmailAddrType.Bcc:
            {
                if (m_DicBcc != null && m_DicBcc.Count > 0)
                {
                    dicReceive = m_DicBcc;
                    receiveCol = mMailMessage.Bcc;
                    hasAddress = true;
                }
            }
            break;

            case EmailAddrType.From:
                throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR);
            }
            if (hasAddress)
            {
                foreach (KeyValuePair <string, string> keyValue in dicReceive)
                {
                    receiveCol.Add(new MailAddress(keyValue.Key, keyValue.Value));
                }
            }
        }
Beispiel #5
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
        }