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 transmission = new Transmission(); transmission.Content.From.Email = config.FromEmail; transmission.Content.Subject = email.Subject; transmission.Content.Html = email.Message; if (attachments != null) { transmission.Content.Attachments = AttachmentHelper.AddAttachments(attachments); } var recipients = email.SendTo.Or("").Split(',').Select(x => x.Trim()); foreach (string recipient in recipients) { transmission.Recipients.Add(new Recipient { Address = new Address(recipient), Type = RecipientType.To }); } var client = new Client(config.ApiKey); client.CustomSettings.SendingMode = SendingModes.Async; var response = await client.Transmissions.Send(transmission).ConfigureAwait(false); var status = response.StatusCode; switch (status) { case HttpStatusCode.OK: case HttpStatusCode.Created: case HttpStatusCode.Accepted: case 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 SpartPost API. {Ex}. ", email.SendTo, ex); } finally { if (deleteAttachmentes && email.Status == Status.Completed) { FileHelper.DeleteFiles(attachments); } } return(false); }
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.FromIdentifier, email.FromName), Subject = email.Subject }; if (!string.IsNullOrWhiteSpace(email.ReplyToEmail)) { message.ReplyTo = new Email(email.ReplyToEmail, email.ReplyToName); } foreach (string address in email.SendTo.Split(',')) { personalization.AddTo(new Email(address.Trim())); } message.AddPersonalization(personalization); var content = new Content { Value = email.Message, Type = email.IsBodyHtml ? "text/html" : "text/plain" }; message.AddContent(content); message = AttachmentHelper.AddAttachments(message, attachments); var sg = new SendGridAPIClient(config.ApiKey, config.ApiDomain); dynamic response = await sg.client.mail.send.post(requestBody : message.Get()); HttpStatusCode status = response.StatusCode; switch (status) { case HttpStatusCode.OK: case HttpStatusCode.Created: case HttpStatusCode.Accepted: case 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.SendTo, ex); } finally { if (deleteAttachmentes) { FileHelper.DeleteFiles(attachments); } } return(false); }