Exemplo n.º 1
0
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="mailInfo">邮件信息实体对象</param>
        /// <returns>发送结果</returns>
        public void SendAsync(EMailInfo mailInfo)
        {
            var _check = Checked(mailInfo);

            if (_check)
            {
                try
                {
                    MailMessage _mm = CreateMail(mailInfo);
                    FSmtpClient.SendAsync(_mm, "Successful");
                    IsSuccessful = true;
                }
                catch (Exception ex)
                {
                    IsSuccessful     = false;
                    LastErrorMessage = ex.Message;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建电子邮件信息对象
        /// </summary>
        /// <param name="mailInfo">邮件信息实体对象</param>
        /// <returns>电子邮件信息对象</returns>
        private MailMessage CreateMail(EMailInfo mailInfo)
        {
            Encoding    _code = Encoding.GetEncoding(mailInfo.Charset);
            MailMessage _mm   = new MailMessage();

            _mm.Priority = mailInfo.Priority;
            _mm.From     = new MailAddress(ServerInfo.UserName, ServerInfo.DisplayName, _code);
#if (NET35 || NET20)
            _mm.To.Add(string.Join(",", mailInfo.Recipient.ToArray())); //收件人
#else
            _mm.To.Add(string.Join(",", mailInfo.Recipient));           //收件人
#endif
            if (null != mailInfo.Cc && mailInfo.Cc.Count > 0)
            {
#if (NET35 || NET20)
                _mm.CC.Add(string.Join(",", mailInfo.Cc.ToArray())); //抄送人
#else
                _mm.CC.Add(string.Join(",", mailInfo.Cc));           //抄送人
#endif
            }
            if (null != mailInfo.Bcc && mailInfo.Bcc.Count > 0)
            {
#if (NET35 || NET20)
                _mm.Bcc.Add(string.Join(",", mailInfo.Bcc.ToArray())); //密送人
#else
                _mm.Bcc.Add(string.Join(",", mailInfo.Bcc));           //密送人
#endif
            }
            _mm.SubjectEncoding = _code;
            _mm.Subject         = mailInfo.Subject;
            _mm.IsBodyHtml      = mailInfo.IsHTML;
            _mm.BodyEncoding    = _code;
            _mm.Body            = mailInfo.Body;
            if (null != mailInfo.Attachments && mailInfo.Attachments.Count > 0)
            {
                while (mailInfo.Attachments.Count > 0)
                {
                    _mm.Attachments.Add(mailInfo.Attachments.Dequeue());
                }
            }
            return(_mm);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 验证检查事件处理
 /// </summary>
 /// <param name="mailInfo">邮件信息实体对象</param>
 /// <returns>验证检查结果</returns>
 private bool Checked(EMailInfo mailInfo)
 {
     IsSuccessful     = false;
     LastErrorMessage = string.Empty; //重置末次错误信息
     if (null == FSmtpClient)
     {
         LastErrorMessage = "未能创建SMTP客户端对象,请检查服务器信息是否配置正确后重试。";
         return(false);
     }
     if (null == mailInfo)
     {
         LastErrorMessage = "邮件信息为空或未配置正确,请检查后重试。";
         return(false);
     }
     if (null == mailInfo.Recipient || mailInfo.Recipient.Count <= 0)
     {
         LastErrorMessage = "收件人信息不能为空或少于1个收件人。";
         return(false);
     }
     return(true);
 }