/// <summary> /// Instantiate the <see cref="SendGridMessage"/> /// </summary> /// <param name="parameters">The collection of message parameters</param> public SendGridMessage(MessageParameterCollection parameters) { if (parameters.ContainsKey(Parameter_FromAddress)) { FromAddress = parameters[Parameter_FromAddress]; } if (parameters.ContainsKey(Parameter_ToAddresses)) { ToAddresses = parameters[Parameter_ToAddresses].Split(",").ToList(); // parse csv } else { ToAddresses = new List <string>(); } if (parameters.ContainsKey(Parameter_Subject)) { Subject = parameters[Parameter_Subject]; } if (parameters.ContainsKey(Parameter_PlainContent)) { PlainContent = parameters[Parameter_PlainContent]; } if (parameters.ContainsKey(Parameter_HtmlContent)) { HtmlContent = parameters[Parameter_HtmlContent]; } }
/// <summary> /// Convert this object into message parameters /// </summary> /// <returns></returns> public MessageParameterCollection ToParameters() { var parameters = new MessageParameterCollection(); if (!string.IsNullOrEmpty(FromAddress)) { parameters.Add(Parameter_FromAddress, FromAddress); } if (ToAddresses.Count > 0) { parameters.Add(Parameter_ToAddresses, string.Join(",", ToAddresses)); } if (!string.IsNullOrEmpty(Subject)) { parameters.Add(Parameter_Subject, Subject); } if (!string.IsNullOrEmpty(PlainContent)) { parameters.Add(Parameter_PlainContent, PlainContent); } if (!string.IsNullOrEmpty(HtmlContent)) { parameters.Add(Parameter_HtmlContent, HtmlContent); } return(parameters); }
/// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/> public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters) { var sgMessage = new SendGridMessage(messageParameters); var from = new EmailAddress(sgMessage.FromAddress); var tos = sgMessage.ToAddresses.Select(to => new EmailAddress(to)).ToList(); var subject = sgMessage.Subject; var plainTextContent = sgMessage.PlainContent; var htmlContent = sgMessage.HtmlContent; var message = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent); var client = new SendGridClient(_sendGridOptions.Apikey); var response = await client.SendEmailAsync(message); if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { return(new NotificationResult(true)); } var respBody = await response.Body.ReadAsStringAsync(); return(new NotificationResult(new List <string> { respBody })); }
public override Task <NotificationResult> Send(MessageParameterCollection messageParameters) { if (_thrownException != null) { throw _thrownException; } return(Task.FromResult(_returnedResult)); }
/// <summary> /// Instantiate <see cref="EmailMessage"/> /// </summary> /// <param name="parameters">The collection of the message parameters</param> public EmailMessage(MessageParameterCollection parameters) { if (parameters.ContainsKey(Parameter_ToAddresses)) { ToAddresses = parameters[Parameter_ToAddresses].Split(',').ToList(); } else { ToAddresses = new List <string>(); } if (parameters.ContainsKey(Parameter_CCAddresses)) { CCAddresses = parameters[Parameter_CCAddresses].Split(',').ToList(); } else { CCAddresses = new List <string>(); } if (parameters.ContainsKey(Parameter_BCCAddresses)) { BCCAddresses = parameters[Parameter_BCCAddresses].Split(',').ToList(); } else { BCCAddresses = new List <string>(); } if (parameters.ContainsKey(Parameter_FromAddress)) { FromAddress = parameters[Parameter_FromAddress]; } if (parameters.ContainsKey(Parameter_Subject)) { Subject = parameters[Parameter_Subject]; } if (parameters.ContainsKey(Parameter_Body)) { Body = parameters[Parameter_Body]; } if (parameters.ContainsKey(Parameter_IsHtml)) { if (bool.TryParse(parameters[Parameter_IsHtml], out bool isHtml)) { IsHtml = isHtml; } } }
/// <summary> /// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/> /// </summary> public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters) { var emailMessage = new EmailMessage(messageParameters); // mail message var message = new MailMessage { Subject = emailMessage.Subject, Body = emailMessage.Body, IsBodyHtml = emailMessage.IsHtml }; if (!string.IsNullOrEmpty(emailMessage.FromAddress)) { message.From = new MailAddress(emailMessage.FromAddress); } else if (_smtpOptions != null && !string.IsNullOrEmpty(_smtpOptions.DefaultFromAddress)) { message.From = new MailAddress(_smtpOptions.DefaultFromAddress); } else { return(new NotificationResult(new List <string> { "From Address should not be empty" })); } foreach (var address in emailMessage.ToAddresses) { message.To.Add(new MailAddress(address)); } foreach (var address in emailMessage.CCAddresses) { message.CC.Add(new MailAddress(address)); } foreach (var address in emailMessage.BCCAddresses) { message.Bcc.Add(new MailAddress(address)); } // send email Logger.LogDebug(SmtpLogMessages.Sending_Start, emailMessage.Subject, emailMessage.ToAddresses); await _smtpClient.SendMailAsync(message); Logger.LogDebug(SmtpLogMessages.Sending_End, emailMessage.Subject, emailMessage.ToAddresses); return(new NotificationResult(true)); }
/// <summary> /// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/> /// </summary> public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters) { var emailMessage = new EmailMessage(messageParameters); // mail message var message = new MailMessage { From = new MailAddress(emailMessage.FromAddress), Subject = emailMessage.Subject, Body = emailMessage.Body, IsBodyHtml = emailMessage.IsHtml }; foreach (var address in emailMessage.ToAddresses) { message.To.Add(new MailAddress(address)); } foreach (var address in emailMessage.CCAddresses) { message.CC.Add(new MailAddress(address)); } foreach (var address in emailMessage.BCCAddresses) { message.Bcc.Add(new MailAddress(address)); } // smtp client var smtpClient = new SmtpClient(_smtpOptions.Server, _smtpOptions.Port) { EnableSsl = _smtpOptions.UseSSL }; if (!string.IsNullOrEmpty(_smtpOptions.Username)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(_smtpOptions.Username, _smtpOptions.Password); } // send email Logger.LogDebug("Sending email {subject} to {toAddresses}", message.Subject, message.To.Select(to => to.Address)); await smtpClient.SendMailAsync(message); Logger.LogDebug("Email {subject} has been sent to {toAddresses}", message.Subject, message.To.Select(to => to.Address)); return(new NotificationResult(true)); }
/// <summary> /// Create the instance of <see cref="SMSMessage"/> /// </summary> /// <param name="parameters">The collection of message parameters</param> public SMSMessage(MessageParameterCollection parameters) { if (parameters.ContainsKey(Parameter_FromNumber)) { FromNumber = parameters[Parameter_FromNumber]; } if (parameters.ContainsKey(Parameter_ToNumber)) { ToNumber = parameters[Parameter_ToNumber]; } if (parameters.ContainsKey(Parameter_Content)) { Content = parameters[Parameter_Content]; } }
/// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/> public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters) { var sgMessage = new SendGridMessage(messageParameters); EmailAddress from = null; if (!string.IsNullOrEmpty(sgMessage.FromAddress)) { from = new EmailAddress(sgMessage.FromAddress); } else if (_sendGridOptions != null && !string.IsNullOrEmpty(_sendGridOptions.DefaultFromAddress)) { from = new EmailAddress(_sendGridOptions.DefaultFromAddress); } else { return(new NotificationResult(new List <string> { "From Address should not be empty" })); } var tos = sgMessage.ToAddresses.Select(to => new EmailAddress(to)).ToList(); var subject = sgMessage.Subject; var plainTextContent = sgMessage.PlainContent; var htmlContent = sgMessage.HtmlContent; var message = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent); Logger.LogDebug(SendGridLogMessages.Sending_Start, sgMessage.Subject, sgMessage.ToAddresses); var response = await _sendGridClient.SendEmailAsync(message); if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { Logger.LogDebug(SendGridLogMessages.Sending_End, sgMessage.Subject, sgMessage.ToAddresses); return(new NotificationResult(true)); } var respBody = await response.Body.ReadAsStringAsync(); return(new NotificationResult(new List <string> { respBody })); }
/// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/> public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters) { var smsMessage = new SMSMessage(messageParameters); PhoneNumber fromNumber; if (!string.IsNullOrEmpty(smsMessage.FromNumber)) { fromNumber = new PhoneNumber(smsMessage.FromNumber); } else if (_options != null && !string.IsNullOrEmpty(_options.DefaultFromNumber)) { fromNumber = new PhoneNumber(_options.DefaultFromNumber); } else { return(new NotificationResult(new List <string> { "From Number should not be empty" })); } Logger.LogDebug(TwilioLogMessages.Sending_Start, smsMessage.ToNumber); try { var message = await MessageResource.CreateAsync( new PhoneNumber(smsMessage.ToNumber), from : fromNumber, body : smsMessage.Content, client : _twilioRestClient); Logger.LogDebug(TwilioLogMessages.Sending_End, smsMessage.ToNumber, message.Sid); return(new NotificationResult(true, new Dictionary <string, object> { { nameof(message.Sid), message.Sid } })); } catch (Exception ex) { Logger.LogWarning(ex, TwilioLogMessages.Sending_Failed, smsMessage.ToNumber, ex.Message); return(new NotificationResult(new List <string> { ex.Message })); } }
/// <summary> /// Convert this object into message parameters /// </summary> /// <returns></returns> public MessageParameterCollection ToParameters() { var parameters = new MessageParameterCollection(); if (!string.IsNullOrEmpty(FromNumber)) { parameters.Add(Parameter_FromNumber, FromNumber); } if (!string.IsNullOrEmpty(ToNumber)) { parameters.Add(Parameter_ToNumber, ToNumber); } if (!string.IsNullOrEmpty(Content)) { parameters.Add(Parameter_Content, Content); } return(parameters); }
/// <summary> /// Convert the properties into message parameters /// </summary> /// <returns></returns> public MessageParameterCollection ToParameters() { var parameters = new MessageParameterCollection(); if (ToAddresses.Count > 0) { parameters.Add(Parameter_ToAddresses, string.Join(",", ToAddresses)); } if (CCAddresses.Count > 0) { parameters.Add(Parameter_CCAddresses, string.Join(",", CCAddresses)); } if (BCCAddresses.Count > 0) { parameters.Add(Parameter_BCCAddresses, string.Join(",", BCCAddresses)); } if (!string.IsNullOrEmpty(FromAddress)) { parameters.Add(Parameter_FromAddress, FromAddress); } if (!string.IsNullOrEmpty(Subject)) { parameters.Add(Parameter_Subject, Subject); } if (!string.IsNullOrEmpty(Body)) { parameters.Add(Parameter_Body, Body); } parameters.Add(Parameter_IsHtml, IsHtml.ToString()); return(parameters); }
/// <inheritdoc cref="INotification.Send(string, MessageParameterCollection)"/> public async Task <NotificationResult> Send(string providerName, MessageParameterCollection messageParameters) { _logger.LogDebug(LogMessages.Intro_SendingMessage, providerName); var provider = GetProvider(providerName); if (provider == null) { _logger.LogWarning(LogMessages.Failed_NoProvider, providerName); return(new NotificationResult(new List <string> { $"Provider {providerName} was not found" })); } try { var result = await provider.Send(messageParameters); if (result.IsSuccess) { _logger.LogDebug(LogMessages.Success_Simple, providerName); } else { _logger.LogWarning(LogMessages.Failed_WithErrors, providerName, result.Errors); } return(result); } catch (Exception ex) { _logger.LogWarning(ex, LogMessages.Failed_Simple, providerName); return(new NotificationResult(new List <string> { ex.Message })); } }
/// <inheritdoc cref="INotification.Send(string, MessageParameterCollection)"/> public async Task <NotificationResult> Send(string providerName, MessageParameterCollection messageParameters) { _logger.LogDebug("Sending message through {providerName}", providerName); var provider = _providerFactory.GetProvider(providerName); if (provider == null) { _logger.LogWarning("Failed to send message through {providerName}. The provider was not found", providerName); return(new NotificationResult(new List <string> { $"Provider {providerName} was not found" })); } try { var result = await provider.Send(messageParameters); if (result.IsSuccess) { _logger.LogDebug("Message has been sent through {providerName}", providerName); } else { _logger.LogWarning("Failed to send message through {providerName}. Errors: {errors}", providerName, result.Errors); } return(result); } catch (Exception ex) { _logger.LogWarning(ex, "Failed to send message through {providerName}", providerName); return(new NotificationResult(new List <string> { ex.Message })); } }
/// <summary> /// The implementation of Send method that is specific to this provider /// </summary> /// <param name="messageParameters">The parameters of the message</param> /// <returns></returns> public abstract Task <NotificationResult> Send(MessageParameterCollection messageParameters);
public override Task <NotificationResult> Send(MessageParameterCollection messageParameters) { throw new NotImplementedException(); }