Exemple #1
0
        public async Task <bool> SendAsync(EmailMessage email, bool deleteAttachmentes, params string[] attachments)
        {
            var config = this.Config as Config;

            if (config == null)
            {
                email.Status = Status.Cancelled;
                return(false);
            }

            try
            {
                email.Status = Status.Executing;

                var message = new SendGridMessage
                {
                    From    = new MailAddress(email.FromEmail, email.FromName),
                    Subject = email.Subject
                };

                if (!string.IsNullOrWhiteSpace(email.ReplyToEmail))
                {
                    message.ReplyTo = new[] { new MailAddress(email.ReplyToEmail, email.ReplyToName) };
                }


                message.AddTo(email.SentTo.Split(',').Select(x => x.Trim()).ToList());

                if (email.IsBodyHtml)
                {
                    message.Html = email.Message;
                }
                else
                {
                    message.Text = email.Message;
                }

                message = AttachmentHelper.AddAttachments(message, attachments);
                var transportWeb = new Web(config.ApiKey);
                await transportWeb.DeliverAsync(message);

                email.Status = Status.Completed;
                return(true);
            }
            // ReSharper disable once CatchAllClause
            catch (Exception ex)
            {
                email.Status = Status.Failed;
                Log.Warning(@"Could not send email to {To} using SendGrid API. {Ex}. ", email.SentTo, ex);
            }
            finally
            {
                if (deleteAttachmentes)
                {
                    FileHelper.DeleteFiles(attachments);
                }
            }

            return(false);
        }
Exemple #2
0
        public async Task <bool> SendAsync(EmailMessage email, bool deleteAttachmentes, params string[] attachments)
        {
            var config = this.Config as Config;

            if (config == null)
            {
                email.Status = Status.Cancelled;
                return(false);
            }

            try
            {
                email.Status = Status.Executing;

                var personalization = new Personalization
                {
                    Subject = email.Subject
                };


                var message = new Mail
                {
                    From    = new Email(email.FromEmail, email.FromName),
                    Subject = email.Subject
                };

                if (!string.IsNullOrWhiteSpace(email.ReplyToEmail))
                {
                    message.ReplyTo = new Email(email.ReplyToEmail, email.ReplyToName);
                }


                foreach (var address in email.SentTo.Split(','))
                {
                    personalization.AddTo(new Email(address.Trim()));
                }


                message.AddPersonalization(personalization);

                var content = new Content();
                content.Value = email.Message;

                if (email.IsBodyHtml)
                {
                    content.Type = "text/html";
                }
                else
                {
                    content.Type = "text/plain";
                }

                message.AddContent(content);

                message = AttachmentHelper.AddAttachments(message, attachments);

                var     sg       = new SendGridAPIClient(config.ApiKey, "https://api.sendgrid.com");
                dynamic response = await sg.client.mail.send.post(requestBody : message.Get());

                System.Net.HttpStatusCode status = response.StatusCode;

                switch (status)
                {
                case System.Net.HttpStatusCode.OK:
                case System.Net.HttpStatusCode.Created:
                case System.Net.HttpStatusCode.Accepted:
                case System.Net.HttpStatusCode.NoContent:
                    email.Status = Status.Completed;
                    break;

                default:
                    email.Status = Status.Failed;
                    break;
                }

                return(true);
            }
            // ReSharper disable once CatchAllClause
            catch (Exception ex)
            {
                email.Status = Status.Failed;
                Log.Warning(@"Could not send email to {To} using SendGrid API. {Ex}. ", email.SentTo, ex);
            }
            finally
            {
                if (deleteAttachmentes)
                {
                    FileHelper.DeleteFiles(attachments);
                }
            }

            return(false);
        }