Exemplo n.º 1
0
        /// <summary>
        /// Enqueues a mail message.
        /// </summary>
        protected virtual async Task EnqueueAsync(ProjectItem project, ContactItem from, ContactItem to, MailMessageItem mailMessage, PropertyDictionary placeholders, CancellationToken cancellationToken)
        {
            Debug.Assert(project != null);
            Debug.Assert(from != null);
            Debug.Assert(to != null);
            Debug.Assert(mailMessage != null);
            Debug.Assert(placeholders != null);

            bool   hasSubject = !string.IsNullOrEmpty(mailMessage.Subject);
            bool   hasContent = !string.IsNullOrEmpty(mailMessage.Body);
            string subject    = null;
            string content    = null;

            if (hasSubject || hasContent)
            {
                placeholders = new PropertyDictionary(placeholders);
                placeholders.AddIfNotExists(StringTemplate.IdentityIdProperty, to.Id);
                placeholders.AddIfNotExists(MailResources.MailHeaderFrom, from);
                placeholders.AddIfNotExists(MailResources.MailHeaderTo, to);

                var immutablePlaceholders = placeholders.ToImmutableDictionary();

                if (hasSubject)
                {
                    subject = await StringTemplate.InterpolateAsync(mailMessage.Subject, immutablePlaceholders, cancellationToken);
                }
                if (hasContent)
                {
                    content = await StringTemplate.InterpolateAsync(mailMessage.Body, immutablePlaceholders, cancellationToken);
                }
            }

            BackgroundJob.Enqueue <MailJobClient>(job => job.Send(
                                                      new MailJobMessage
            {
                MessageId = mailMessage.Id,
                ProjectId = project.Id,
                From      = new MailJobAddress(from.Id, from.Email.Address, from.Email.Name),
                To        = new MailJobAddress(to.Id, to.Email.Address, to.Email.Name),
                Subject   = subject,
                Body      = content,
                Users     = _userIds
            }));
        }