Ejemplo n.º 1
0
        public async Task SendAsync(Email email, string fromEmail = null, CancellationToken cancellation = default)
        {
            // Prepare the SendGridMessage
            var msg = new SendGridMessage();

            msg.SetFrom(new EmailAddress(email: fromEmail ?? _options.DefaultFromEmail, name: _options.DefaultFromName));
            msg.AddTo(new EmailAddress(email.ToEmail));
            msg.SetSubject(email.Subject);
            msg.AddContent(MimeType.Html, email.Body);
            msg.AddCustomArg(EmailIdKey, email.EmailId.ToString());
            msg.AddCustomArg(TenantIdKey, email.TenantId.ToString());

            // Send it to SendGrid using their official C# library
            await SendEmailAsync(msg, cancellation);
        }
        private async Task Resend(IDurableEntityContext context)
        {
            var state = context.GetState <MailEntity>();

            var sendGrid = new SendGridClient(Environment.GetEnvironmentVariable("SENDGRID_APIKEY"));

            var sendGridMessage = new SendGridMessage
            {
                From             = new EmailAddress(Environment.GetEnvironmentVariable("FROM_ADDRESS")),
                Subject          = state.Message.Subject,
                PlainTextContent = state.Message.Body
            };

            sendGridMessage.AddTo(state.Message.To);
            sendGridMessage.AddCustomArg("entitykey", context.Key);

            await sendGrid.SendEmailAsync(sendGridMessage);

            context.SignalEntity(new EntityId(nameof(MailStatusEntity), state.Status), "Remove", context.Key);

            state.Status        = "resending";
            state.LastUpdatedAt = DateTime.Now;

            context.SignalEntity(new EntityId(nameof(MailStatusEntity), state.Status), "Add", context.Key);

            context.SetState(state);
        }
Ejemplo n.º 3
0
        private SendGridMessage GetSendGridMessage(List <EmailInfoItem> batch, EmailAddress from,
                                                   SendGridAccount sendGridAccount, EmailInfo emailInfo)
        {
            var sendGridMessage = new SendGridMessage();

            sendGridMessage.SetFrom(from);

            if (emailInfo.TemplateData != null)
            {
                sendGridMessage.TemplateId = sendGridAccount.TemplateId;
            }
            else
            {
                sendGridMessage.SetGlobalSubject(emailInfo.Subject);

                if (!string.IsNullOrEmpty(emailInfo.PlainTextContent))
                {
                    sendGridMessage.AddContent(MimeType.Text, emailInfo.PlainTextContent);
                }

                if (!string.IsNullOrEmpty(emailInfo.HtmlContent))
                {
                    sendGridMessage.AddContent(MimeType.Html, emailInfo.HtmlContent);
                }
            }

            for (var personalizationIndex = 0; personalizationIndex < batch.Count; ++personalizationIndex)
            {
                var emailInfoItem = batch[personalizationIndex];

                var to = GetEmailAddress(emailInfoItem);
                sendGridMessage.AddTo(to, personalizationIndex);

                if (emailInfo.TemplateData != null)
                {
                    sendGridMessage.SetTemplateData(emailInfo.TemplateData, personalizationIndex);
                }

                if (emailInfoItem.Email != null)
                {
                    sendGridMessage.AddCustomArg("EmailId", emailInfoItem.Email.Id.ToString(), personalizationIndex);
                }
            }

            if (emailInfo.SendAt != null)
            {
                sendGridMessage.SendAt = new DateTimeOffset(emailInfo.SendAt.Value).ToUnixTimeSeconds();
            }

            if (emailInfo.AttachmentName != null)
            {
                sendGridMessage.AddAttachment(emailInfo.AttachmentName, emailInfo.AttachmentBase64Content);
            }

            return(sendGridMessage);
        }
Ejemplo n.º 4
0
        public async Task SendBulkAsync(IEnumerable <Email> emails, string fromEmail = null, CancellationToken cancellation = default)
        {
            // Prepare the SendGridMessage
            var msg = new SendGridMessage();

            msg.SetFrom(new EmailAddress(email: fromEmail ?? _options.DefaultFromEmail, name: _options.DefaultFromName));
            msg.AddContent(MimeType.Html, Placeholder);
            foreach (var(email, index) in emails.Select((e, i) => (e, i)))
            {
                msg.AddTo(new EmailAddress(email.ToEmail), index);
                msg.SetSubject(email.Subject, index);
                msg.AddSubstitutions(new Dictionary <string, string> {
                    { Placeholder, email.Body }
                }, index);
                msg.AddCustomArg(EmailIdKey, email.EmailId.ToString(), index);
                msg.AddCustomArg(TenantIdKey, email.TenantId.ToString(), index);
            }

            // Send it to SendGrid using their official C# library
            await SendEmailAsync(msg, cancellation);
        }
        private async Task Send(IDurableEntityContext context)
        {
            var message = context.GetInput <MailMessage>();

            var sendGrid = new SendGridClient(Environment.GetEnvironmentVariable("SENDGRID_APIKEY"));

            var sendGridMessage = new SendGridMessage
            {
                From             = new EmailAddress("*****@*****.**"),
                Subject          = message.Subject,
                PlainTextContent = message.Body
            };

            sendGridMessage.AddTo(message.To);
            sendGridMessage.AddCustomArg("entitykey", context.Key);

            await sendGrid.SendEmailAsync(sendGridMessage);

            context.SetState(new MailEntity {
                Message = message, SentAt = DateTime.Now
            });
        }
Ejemplo n.º 6
0
 public void AddAuthenticationSignature(SendGridMessage msg)
 {
     msg.AddCustomArg("AuthenticationSignature", "SomeAuthenticationSignature");
 }