Пример #1
0
        /// <summary>
        /// 发送邮件前检查需要设置的信息是否完整,收集 错误 信息
        /// </summary>
        /// <param name="dicMsg">将检查信息收集到此集合</param>
        private void InnerCheckSendMail4Error(Dictionary <MailInfoType, string> dicMsg)
        {
            #region 处理 发件/收件

            if (string.IsNullOrEmpty(From))
            {
                From            = "*****@*****.**";
                FromDisplayName = "h3bpm";
            }
            else
            {
                if (!MailValidatorHelper.IsEmail(From))
                {
                    string strTemp = string.Format(MailInfoHelper.GetMailInfoStr(MailInfoType.FromFormat), FromDisplayName, From);
                    dicMsg.Add(MailInfoType.FromFormat, strTemp);
                }
            }

            if (string.IsNullOrEmpty(To))
            {
                dicMsg.Add(MailInfoType.ToEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.ToEmpty));
            }
            else
            {
                foreach (var item in To.Split(';'))
                {
                    if (!MailValidatorHelper.IsEmail(item))
                    {
                        string strTemp = string.Format(MailInfoHelper.GetMailInfoStr(MailInfoType.ToFormat), item, item);
                        dicMsg.Add(MailInfoType.ToFormat, strTemp);
                    }
                }
            }

            #endregion 处理 发件/收件

            // 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));
                }
            }
        }
Пример #2
0
        private MailMessage CreateMessagex()
        {
            #region 构建 MailMessage

            bool hasError     = false;
            var  mMailMessage = new MailMessage();
            try
            {
                mMailMessage.From = new MailAddress(From, FromDisplayName);
                //增加收件人地址等
                foreach (var item in To.Split(';'))//.Where(s => MailValidatorHelper.IsEmail(s)))
                {
                    mMailMessage.To.Add(item);
                }
                if (!string.IsNullOrEmpty(Cc))
                {
                    foreach (var item in Cc.Split(';').Where(s => MailValidatorHelper.IsEmail(s)))
                    {
                        mMailMessage.CC.Add(item);
                    }
                }
                if (!string.IsNullOrEmpty(Bcc))
                {
                    foreach (var item in Bcc.Split(';').Where(s => MailValidatorHelper.IsEmail(s)))
                    {
                        mMailMessage.Bcc.Add(item);
                    }
                }
                mMailMessage.Subject = Subject;
                mMailMessage.Body    = Body;

                if (Attachments != null && Attachments.Count > 0)
                {
                    foreach (Attachment attachment in Attachments)
                    {
                        mMailMessage.Attachments.Add(attachment);
                    }
                }

                mMailMessage.SubjectEncoding = Encoding.UTF8;
                mMailMessage.BodyEncoding    = Encoding.UTF8;
                // SmtpClient 的 Headers 中会根据 MailMessage 默认设置些值,所以应该为 UTF8 。
                mMailMessage.HeadersEncoding = Encoding.UTF8;
                mMailMessage.IsBodyHtml      = IsBodyHtml;
                if (AlternateViews != null && AlternateViews.Count > 0)
                {
                    foreach (AlternateView alternateView in AlternateViews)
                    {
                        mMailMessage.AlternateViews.Add(alternateView);
                    }
                }
                mMailMessage.Priority = (MailPriority)Priority;
            }
            catch (ArgumentNullException argumentNullEx)
            {
                hasError = true;
                throw argumentNullEx;
            }
            catch (ArgumentException argumentEx)
            {
                hasError = true;
                throw argumentEx;
            }
            catch (FormatException formatEx)
            {
                hasError = true;
                throw formatEx;
            }
            finally
            {
                if (hasError)
                {
                    if (mMailMessage != null)
                    {
                        this.InnerDisposeMessage(mMailMessage);
                        mMailMessage = null;
                    }
                    this.InnerDisposeSmtp();
                }
            }
            return(mMailMessage);

            #endregion 构建 MailMessage
        }