/// <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)); } } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="errMsg">反馈信息</param> private bool SendMessage(out string errMsg) { errMsg = string.Empty; if (this._mail == null) { errMsg = "MailHelper对象为空"; return(false); } Dictionary <MailInfoType, string> dic = this._mail.CheckSendMail(); if (dic.Count > 0 && MailInfoHelper.ExistsError(dic)) { // 反馈“错误+提示”信息 errMsg = MailInfoHelper.GetMailInfoStr(dic); return(false); } try { if (this._isSimple) { this._mail.SendOneMail(); } else { // 发送 this._mail.SendBatchMail(); } return(true); } catch (Exception ex) { // 反馈异常信息 errMsg = ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message; } finally { _mail.Reset(); } return(false); }
/// <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)); } }
/// <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 }