public async Task<bool> SendAsync(EmailMessage email, bool deleteAttachmentes, params string[] attachments) { await Task.Delay(1).ConfigureAwait(false); var config = this.Config as Config; if(config == null) { email.Status = Status.Cancelled; return false; } try { var client = new RestClient { BaseUrl = new Uri("https://api.mailgun.net/v3"), Authenticator = new HttpBasicAuthenticator("api", config.ApiKey) }; var request = new RestRequest { Resource = "{domain}/messages", Method = Method.POST }; request.AddParameter("domain", config.DomainName, ParameterType.UrlSegment); request.AddParameter("from", this.GetEmailAccount(email.FromEmail, email.FromName)); request.AddParameter("reply-to", this.GetEmailAccount(email.ReplyToEmail, email.ReplyToName)); foreach(string recipient in email.SentTo.Split(',')) { request.AddParameter("to", recipient.Trim()); } request.AddParameter("subject", email.Subject); request.AddParameter("text", email.Message); client.Execute(request); return true; } // ReSharper disable once CatchAllClause catch(Exception ex) { email.Status = Status.Failed; Log.Warning(@"Could not send email to {To} using Mailgun API. {Ex}. ", email.SentTo, ex); } finally { if(deleteAttachmentes) { 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 message = new SendGridMessage(); message.From = new MailAddress(email.FromEmail, email.FromName); message.AddTo(email.SentTo.Split(',').ToList()); message.Subject = email.Subject; 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; }
public static EmailMessage GetMessage(Config config, EmailQueue mail) { var message = new EmailMessage { FromName = mail.FromName, FromEmail = mail.FromEmail, ReplyToEmail = mail.ReplyTo, ReplyToName = mail.ReplyToName, Subject = mail.Subject, SentTo = mail.SendTo, Message = mail.Message, Type = Type.Outward, EventDateUtc = DateTimeOffset.UtcNow, Status = Status.Unknown }; return message; }
public static EmailMessage GetMessage(Config config, EmailQueue mail) { var message = new EmailMessage { FromName = mail.FromName, FromEmail = mail.ReplyTo, Subject = mail.Subject, SentTo = mail.SendTo, Message = mail.Message, Type = Type.Outward, EventDateUtc = DateTime.UtcNow, Status = Status.Unknown }; if (string.IsNullOrWhiteSpace(message.FromEmail)) { message.FromName = config.FromName; message.FromEmail = config.FromEmail; } return message; }
public async Task<bool> SendAsync(EmailMessage email, SmtpHost host, ICredentials credentials, bool deleteAttachmentes, params string[] attachments) { if (string.IsNullOrWhiteSpace(email.SentTo)) { throw new ArgumentNullException(email.SentTo); } if (string.IsNullOrWhiteSpace(email.Message)) { throw new ArgumentNullException(email.Message); } var addresses = email.SentTo.Split(','); foreach (var validator in addresses.Select(address => new Validator(address))) { validator.Validate(); if (!validator.IsValid) { return false; } } addresses = addresses.Distinct().ToArray(); email.SentTo = string.Join(",", addresses); email.Status = Status.Executing; using (var mail = new MailMessage(email.FromEmail, email.SentTo)) { if (attachments != null) { foreach (string file in attachments) { if (!string.IsNullOrWhiteSpace(file)) { if (File.Exists(file)) { var attachment = new Attachment(file, MediaTypeNames.Application.Octet); var disposition = attachment.ContentDisposition; disposition.CreationDate = File.GetCreationTime(file); disposition.ModificationDate = File.GetLastWriteTime(file); disposition.ReadDate = File.GetLastAccessTime(file); disposition.FileName = Path.GetFileName(file); disposition.Size = new FileInfo(file).Length; disposition.DispositionType = DispositionTypeNames.Attachment; mail.Attachments.Add(attachment); } } } } var sender = new MailAddress(email.FromEmail, email.FromName); using (var smtp = new SmtpClient(host.Address, host.Port)) { smtp.DeliveryMethod = host.DeliveryMethod; smtp.PickupDirectoryLocation = host.PickupDirectory; smtp.EnableSsl = host.EnableSsl; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(credentials.Username, credentials.Password); try { mail.Subject = email.Subject; mail.Body = email.Message; mail.IsBodyHtml = email.IsBodyHtml; mail.SubjectEncoding = Encoding.UTF8; email.Status = Status.Completed; mail.ReplyToList.Add(sender); await smtp.SendMailAsync(mail); return true; } catch (SmtpException ex) { email.Status = Status.Failed; Log.Warning(@"Could not send email to {To}. {Ex}. ", email.SentTo, ex); } finally { foreach (IDisposable item in mail.Attachments) { item?.Dispose(); } if (deleteAttachmentes) { DeleteFiles(attachments); } } } } return false; }
public async Task<bool> SendAsync(EmailMessage email) { return await this.SendAsync(email, false, null).ConfigureAwait(false); }
public Task<bool> SendAsync(EmailMessage email) { return this.SendAsync(email, false, null); }
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; }