Example #1
0
 private void CheckIsDeliverable(MailerMessage message)
 {
     if (!message.IsDeliverable)
     {
         var ex =
             new ArgumentException(
                 $"MailerMessage with subject '{message.Subject}' is not valid for conversion into a DeliveryItem. Make sure all messages have a sender, body, and at least one recipient specified");
         Logger.LogError(1, ex, ex.Message);
         throw ex;
     }
 }
Example #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="DeliveryItem" /> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="recipient">The recipient.</param>
        public DeliveryItem(MailerMessage message, MessageRecipient recipient)
        {
            FromEmailAddress    = message?.From?.EmailAddress;
            FromDisplayName     = message?.From?.DisplayName;
            ReplyToEmailAddress = message?.From?.ReplyToEmailAddress;
            ReplyToDisplayName  = message?.From?.ReplyToDisplayName;
            ToEmailAddress      = recipient?.EmailAddress;
            ToDisplayName       = recipient?.DisplayName;
            Subject             = message?.Subject;
            Body        = message?.Body;
            Attachments = message?.Attachments;

            //merge personalized and general subs
            Substitutions = new Dictionary <string, string>(recipient?.PersonalizedSubstitutions);
            foreach (var gSub in message?.Substitutions?.Where(s => !Substitutions.ContainsKey(s.Key)) ??
                     new Dictionary <string, string>())
            {
                Substitutions.Add(gSub.Key, gSub.Value);
            }
        }
Example #3
0
        /// <summary>
        ///     Adds a message to the list of pending messages tracked by the mailer.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>A collection of delivery item identifiers.</returns>
        public virtual IEnumerable <Guid> AddMessage(MailerMessage message)
        {
            CheckIsDeliverable(message);
            var items = message.Recipients.Select(recipient =>
                                                  new DeliveryItem(message, recipient))
                        .ToList();

            using (_deliverablesLock.LockAsync().Result)
            {
                foreach (var i in items)
                {
                    Logger.LogDebug(
                        "Mailer added new delivery item ID '{deliveryItemId}' to '{to}' with subject '{subject}'",
                        i.Id,
                        i.ToEmailAddress,
                        i.Subject);
                    i.DeliveryProvider = GetType().Name;
                    ((IMailer)this).PendingDeliverables.Add(i);
                }
            }

            return(items.Select(i => i.Id));
        }