コード例 #1
0
        public virtual void Send(Notice notice)
        {
            var         email       = notice as EmailNotice;
            MailMessage mailMessage = new MailMessage();

            mailMessage.Subject    = email.Subject;
            mailMessage.Body       = email.Content;
            mailMessage.IsBodyHtml = email.IsHtml;

            if (email.To != null)
            {
                foreach (var item in email.To)
                {
                    mailMessage.To.Add(new MailAddress(item));
                }
            }
            if (email.Cc != null)
            {
                foreach (var item in email.Cc)
                {
                    mailMessage.CC.Add(new MailAddress(item));
                }
            }
            if (email.Bcc != null)
            {
                foreach (var item in email.Bcc)
                {
                    mailMessage.Bcc.Add(new MailAddress(item));
                }
            }
            if (email.Attachments != null)
            {
                foreach (var item in email.Attachments)
                {
                    mailMessage.Attachments.Add(new Attachment(item));
                }
            }
            SmtpClient client = _smtpProvider.Get();

            if (email.From.IsNullOrWhiteSpace())
            {
                email.From = (client.Credentials as NetworkCredential).UserName;
            }
            mailMessage.From      = new MailAddress(email.From, email.DisplayName ?? email.From);
            client.SendCompleted += Client_SendCompleted;

            client.SendAsync(mailMessage, email);
        }
コード例 #2
0
 public bool Send(bool successful)
 {
     try
     {
         var subject = successful ? _settings.SuccessfulSubject : _settings.FailureSubject;
         var from    = string.IsNullOrWhiteSpace(_settings.FromAlias) ? _settings.FromAddress : string.Format("{0} <{1}>", _settings.FromAlias, _settings.FromAddress);
         var to      = string.Join(",", _settings.ToAddresses);
         var client  = _smtpProvider.Get();
         client.Send(new MailMessage(from, to, subject, subject));
         return(true);
     }
     catch (Exception ex)
     {
         _logger.Error(ex.Message);
         _logger.Error(ex.StackTrace);
         return(false);
     }
 }