コード例 #1
0
        public virtual bool Send()
        {
            if (string.IsNullOrEmpty(From))
            {
                sendState = SendState.Error;
                message   = "没有发件人!";
                return(false);
            }
            if (string.IsNullOrEmpty(Subject))
            {
                sendState = SendState.Error;
                message   = "没有邮件标题!";
                return(false);
            }
            if (string.IsNullOrEmpty(Body))
            {
                sendState = SendState.Error;
                message   = "没有邮件正文!";
                return(false);
            }
            if (To.Count < 1)
            {
                sendState = SendState.Error;
                message   = "没有收件人!";
                return(false);
            }
            if (string.IsNullOrEmpty(Server))
            {
                sendState = SendState.Error;
                message   = "没有设置邮件服务器地址!";
                return(false);
            }
            if (string.IsNullOrEmpty(Password))
            {
                sendState = SendState.Error;
                message   = "没有设置邮件服务器账号密码!";
                return(false);
            }
            if (string.IsNullOrEmpty(UserName))
            {
                UserName = From;
            }
            MailMessage mailMessage = new MailMessage();

            mailMessage.From            = new MailAddress(From, FromName);
            mailMessage.Subject         = Subject.Trim();
            mailMessage.SubjectEncoding = SubjectEncoding;
            mailMessage.Body            = Body.Trim();
            mailMessage.BodyEncoding    = BodyEncoding;
            mailMessage.IsBodyHtml      = IsBodyHtml;
            foreach (string mailAttachment in mailAttachmentList)
            {
                mailMessage.Attachments.Add(new Attachment(mailAttachment));
            }
            StringEnumerator enumerator2 = To.GetEnumerator();

            try
            {
                while (enumerator2.MoveNext())
                {
                    string      current2    = enumerator2.Current;
                    string[]    array       = current2.Split(Delimiter);
                    MailAddress mailAddress = null;
                    mailAddress = ((array.Length <= 1) ? new MailAddress(array[0].Trim()) : new MailAddress(array[0].Trim(), array[1].Trim()));
                    mailMessage.To.Add(mailAddress);
                }
            }
            finally
            {
                (enumerator2 as IDisposable)?.Dispose();
            }
            if (Bcc.Count > 0)
            {
                enumerator2 = Bcc.GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        string      current3    = enumerator2.Current;
                        string[]    array2      = current3.Split(Delimiter);
                        MailAddress mailAddress = null;
                        mailAddress = ((array2.Length <= 1) ? new MailAddress(array2[0].Trim()) : new MailAddress(array2[0].Trim(), array2[1].Trim()));
                        mailMessage.Bcc.Add(mailAddress);
                    }
                }
                finally
                {
                    (enumerator2 as IDisposable)?.Dispose();
                }
            }
            if (CC.Count > 0)
            {
                enumerator2 = CC.GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        string      current3    = enumerator2.Current;
                        string[]    array3      = current3.Split(Delimiter);
                        MailAddress mailAddress = null;
                        mailAddress = ((array3.Length <= 1) ? new MailAddress(array3[0].Trim()) : new MailAddress(array3[0].Trim(), array3[1].Trim()));
                        mailMessage.CC.Add(mailAddress);
                    }
                }
                finally
                {
                    (enumerator2 as IDisposable)?.Dispose();
                }
            }
            try
            {
                SmtpClient smtpClient = new SmtpClient(Server, ServerPort);
                smtpClient.Credentials    = new NetworkCredential(UserName, Password);
                smtpClient.SendCompleted += SendCompletedCallback;
                string subject = Subject;
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                sendState = SendState.Error;
                message   = ex.Message;
                return(false);
            }
            mailMessage.Dispose();
            return(true);
        }