示例#1
0
 /// <summary>
 /// Creates valid domain object
 /// </summary>
 public EmailFailure(EmailMessageRecipient recipient, Exception ex)
 {
     Recipient = recipient;
     Date = DateTime.Now;
     ErrorMessage = ex.Message;
     ErrorCode = ex.StackTrace;
 }
        protected void SendEmail(EmailMessageRecipient recipient)
        {
            var mailMessage = BuildMessage(recipient);

            var smtpClient = new SmtpClient();
            smtpClient.Send(mailMessage);
        }
        protected MailMessage BuildMessage(EmailMessageRecipient recipient)
        {
            MailMessage mailMessage = new MailMessage();
            //Add Subject
            mailMessage.Subject = recipient.Message.Subject;
            //Add Body
            mailMessage.Body = recipient.Message.Body;
            //Add IsBodyHTML
            mailMessage.IsBodyHtml = recipient.Message.IsBodyHTML;
            //Add ToEmailAddress
            mailMessage.To.Add(recipient.EmailAddress);
            //Add BccEmail if provided
            if (!string.IsNullOrEmpty(ProcessorSettings.BccEmail))
            {
                mailMessage.Bcc.Add(ProcessorSettings.BccEmail);
            }
            //Add FromEmail if provided
            if (!string.IsNullOrEmpty(recipient.Message.FromAddress))
            {
                //Add FromName if provided
                mailMessage.From = !string.IsNullOrEmpty(recipient.Message.FromName)
                                       ? new MailAddress(recipient.Message.FromAddress,
                                                         recipient.Message.FromName)
                                       : new MailAddress(recipient.Message.FromAddress);
            }
            //Add ReplyTo if provided
            if (!string.IsNullOrEmpty(recipient.Message.ReplyTo))
            {
                mailMessage.ReplyTo = new MailAddress(recipient.Message.ReplyTo);
            }
            //Add ReturnPath if provided
            if (!string.IsNullOrEmpty(recipient.Message.ReturnPath))
            {
                mailMessage.Headers.Add("Return-Path", recipient.Message.ReturnPath);
            }
            //Add Priority
            switch (recipient.Message.Importance)
            {
                case EmailImportance.Low:
                    mailMessage.Priority = MailPriority.Low;
                    break;
                case EmailImportance.Normal:
                    mailMessage.Priority = MailPriority.Normal;
                    break;
                case EmailImportance.High:
                    mailMessage.Priority = MailPriority.High;
                    break;
                default:
                    mailMessage.Priority = MailPriority.Normal;
                    break;
            }
            foreach (EmailMessageAttachment attachment in recipient.Message.Attachments)
            {
                Stream attStream = new MemoryStream(attachment.Body);
                Attachment att = new Attachment(attStream, attachment.Name);
                mailMessage.Attachments.Add(att);
            }

            return mailMessage;
        }