public MailAttributes(MailAttributes mailAttributes, 
            bool copyTo = true, bool copyCc = true,
            bool copyBcc = true, bool copyReplyTo = true, bool referenceAttachments = true,
            bool copyHeaders = true, bool copyExtraProperties = true, 
            bool copyPostProcessors = true)
        {
            From = mailAttributes.From;
            Subject = mailAttributes.Subject;
            SubjectEncoding = mailAttributes.SubjectEncoding;
            Priority = mailAttributes.Priority;

            IsCcToSupported = mailAttributes.IsCcToSupported;
            IsBccSupported = mailAttributes.IsBccSupported;
            IsReplyToSupported = mailAttributes.IsReplyToSupported;

            MessageEncoding = mailAttributes.MessageEncoding;
            Body = mailAttributes.Body;
            TextBody = mailAttributes.TextBody;
            HtmlBody = mailAttributes.HtmlBody;

            To = copyTo ? mailAttributes.To.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                .ToList() : new List<MailAddress>();
            Cc = copyCc ? mailAttributes.Cc.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                    .ToList() : new List<MailAddress>();
            Bcc = copyBcc ? mailAttributes.Bcc.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                    .ToList() : new List<MailAddress>();
            ReplyTo = copyReplyTo ? mailAttributes.ReplyTo.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                    .ToList() : new List<MailAddress>();
            Attachments = referenceAttachments ? mailAttributes.Attachments : new AttachmentCollection();
            Headers = copyHeaders ? mailAttributes.Headers.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) : new Dictionary<string, string>();
            ExtraProperties = copyExtraProperties ? mailAttributes.ExtraProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) : new Dictionary<string, string>();
            PostProcessors = copyPostProcessors ? mailAttributes.PostProcessors.Select(pp => pp).ToList() : new List<IPostProcessor>();

            AlternateViews = new List<AlternateView>();
        }
Example #2
0
        public MailAttributes(MailAttributes mailAttributes,
                              bool copyTo             = true, bool copyCc              = true,
                              bool copyBcc            = true, bool copyReplyTo         = true, bool referenceAttachments = true,
                              bool copyHeaders        = true, bool copyExtraProperties = true,
                              bool copyPostProcessors = true)
        {
            From     = mailAttributes.From;
            Subject  = mailAttributes.Subject;
            Priority = mailAttributes.Priority;

            IsCcToSupported    = mailAttributes.IsCcToSupported;
            IsBccSupported     = mailAttributes.IsBccSupported;
            IsReplyToSupported = mailAttributes.IsReplyToSupported;

            MessageEncoding = mailAttributes.MessageEncoding;
            Body            = mailAttributes.Body;
            TextBody        = mailAttributes.TextBody;
            HtmlBody        = mailAttributes.HtmlBody;

            To = copyTo ? mailAttributes.To.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                 .ToList() : new List <MailAddress>();
            Cc = copyCc ? mailAttributes.Cc.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                 .ToList() : new List <MailAddress>();
            Bcc = copyBcc ? mailAttributes.Bcc.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                  .ToList() : new List <MailAddress>();
            ReplyTo = copyReplyTo ? mailAttributes.ReplyTo.Select(mailAddress => new MailAddress(mailAddress.Address, mailAddress.DisplayName))
                      .ToList() : new List <MailAddress>();
            Attachments     = referenceAttachments ? mailAttributes.Attachments : new AttachmentCollection();
            Headers         = copyHeaders ? mailAttributes.Headers.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) : new Dictionary <string, string>();
            ExtraProperties = copyExtraProperties ? mailAttributes.ExtraProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) : new Dictionary <string, string>();
            PostProcessors  = copyPostProcessors ? mailAttributes.PostProcessors.Select(pp => pp).ToList() : new List <IPostProcessor>();

            AlternateViews = new List <AlternateView>();
        }
        /// <summary>
        ///     Creates a MailMessage for the current MailAttribute instance.
        /// </summary>
        protected EmailMessage GenerateProspectiveMailMessage(MailAttributes mail)
        {
            //create base message
            var message = new EmailMessage
            {
                FromName = mail.From.DisplayName,
                FromEmail = mail.From.Address,
                To = mail.To.Union(mail.Cc).Select(t => new EmailAddress(t.Address, t.DisplayName)),
                BccAddress = mail.Bcc.Any() ? mail.Bcc.First().Address : null,
                Subject = mail.Subject,
                Important = mail.Priority == MailPriority.High ? true : false
            };

            // We need to set Reply-To as a custom header
            if (mail.ReplyTo.Any())
            {
                message.AddHeader("Reply-To", string.Join(" , ", mail.ReplyTo));
            }

            // Adding content to the message
            foreach (var view in mail.AlternateViews)
            {
                var reader = new StreamReader(view.ContentStream, Encoding.UTF8, true, 1024, true);

                var body = reader.ReadToEnd();

                if (view.ContentType.MediaType == MediaTypeNames.Text.Plain)
                {
                    message.Text = body;
                }
                if (view.ContentType.MediaType == MediaTypeNames.Text.Html)
                {
                    message.Html = body;
                }
            }

            // Going through headers and adding them to the message
            mail.Headers.ToList().ForEach(h => message.AddHeader(h.Key, h.Value));

            // Adding the attachments
            var attachments = new List<EmailAttachment>();
            foreach (var mailAttachment in mail.Attachments.Select(attachment => ActionMailerNext.Utils.AttachmentCollection.ModifyAttachmentProperties(attachment.Key, attachment.Value, false)))
            {
                using (var stream = new MemoryStream())
                {
                    mailAttachment.ContentStream.CopyTo(stream);
                    var base64Data = Convert.ToBase64String(stream.ToArray());
                    attachments.Add(new EmailAttachment
                    {
                        Content = base64Data,
                        Name = mailAttachment.Name,
                        Type = mailAttachment.ContentType.MediaType,
                    });
                }
            }

            message.Attachments = attachments;

            return message;
        }
 /// <summary>
 ///     Returns a populated context to be used for the OnMailSending()
 ///     method in MailerBase.
 /// </summary>
 /// <param name="mail">The message you wish to wrap within this context.</param>
 public MailSendingContext(MailAttributes mail)
 {
     Mail = mail;
     Cancel = false;
 }
 private void AsyncSendCompleted(MailAttributes mail)
 {
     _interceptor.OnMailSent(mail);
 }
        public virtual async Task<List<IMailResponse>> SendAsync(MailAttributes mailAttributes)
        {
            var mail = GenerateProspectiveMailMessage(mailAttributes);
            var response = new List<IMailResponse>();
            var request = new SendMessageRequest(mail);

            await _client.SendMessage(request).ContinueWith(x => response.AddRange(x.Result.Select(result => new MandrillMailResponse
            {
                Email = result.Email,
                Status = MandrillMailResponse.GetProspectiveStatus(result.Status.ToString()),
                RejectReason = result.RejectReason,
                Id = result.Id
            })));

            return response;
        }
        public virtual List<IMailResponse> Send(MailAttributes mailAttributes)
        {
            var mail = GenerateProspectiveMailMessage(mailAttributes);
            var response = new List<IMailResponse>();
            var request = new SendMessageRequest(mail);

            var resp = AsyncHelpers.RunSync(() => _client.SendMessage(request));
            response.AddRange(resp.Select(result => new MandrillMailResponse
            {
                Email = result.Email,
                Status = MandrillMailResponse.GetProspectiveStatus(result.Status.ToString()),
                RejectReason = result.RejectReason,
                Id = result.Id
            }));

            return response;
        }
 /// <summary>
 ///     Returns a populated context to be used for the OnMailSending()
 ///     method in MailerBase.
 /// </summary>
 /// <param name="mail">The message you wish to wrap within this context.</param>
 public MailSendingContext(MailAttributes mail)
 {
     Mail   = mail;
     Cancel = false;
 }