private async void SendSms(NotificationSMS mail, NotificationProcessingAudit audit) { _log.Info(" sending sms "); var info = new NotificationProcessingAuditInfo { Id = audit.Id.ToString() + "_SMS", DateInserted = DateTime.Now, NotificationId = audit.Id, Type = "SMS", Contact = string.Join(",", mail.Recipitents.ToArray()) }; try { var smsuri = _settingsRepository.GetByKey(SettingsKeys.SmsUri); string smsurivalue = ""; if (smsuri != null) smsurivalue = smsuri.Value; var client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(1); client.BaseAddress = new Uri(smsurivalue); string urlSuffix = "api/gateway/sms/postnotification"; _log.InfoFormat("Sending sms to {0} {1}",smsuri,urlSuffix); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.PostAsJsonAsync(urlSuffix, mail ); response.EnsureSuccessStatusCode(); string r = await response.Content.ReadAsStringAsync(); info.Info = r; _processingAuditRepository.Add(info); } catch (Exception ex) { info.Info = ex.Message; if (ex.InnerException != null) info.Info += " --> " + ex.InnerException.Message; _processingAuditRepository.Add(info); } }
public HttpResponseMessage Notify(JObject jnotification) { var request = Request; Guid Id = Guid.Empty; var responseBasic = new ResponseBasic(); HttpStatusCode returnCode = HttpStatusCode.OK; try { responseBasic.ResultInfo = "invalid jsonnotificaction"; NotificationBase notification = JsonConvert.DeserializeObject<NotificationBase>(jnotification.ToString()); responseBasic.ResultInfo = "valid jsonnotificaction"; _log.InfoFormat("Received notification id {0} : Notification type {1} ", notification.Id, notification.TypeRef); bool isValid = notification != null; Id = notification.Id; if (isValid) { _log.InfoFormat("Id {0} Placed on bus", notification.Id); var n = new NotificationProcessingAudit(); n.DateInserted = DateTime.Now; n.Id = notification.Id; n.Status=NotificationProcessingStatus.Pending; n.Type = notification.TypeRef; n.JsonNotification = JsonConvert.SerializeObject(notification); _notificationProcessing.Add(n); responseBasic.Result = "Notification Processed"; } } catch (Exception ex) { responseBasic.Result = "Processing Failed"; responseBasic.ErrorInfo = ex.Message; _log.Error("Failed to process Notification", ex); } HttpResponseMessage response = Request.CreateResponse(returnCode, responseBasic); _log.InfoFormat("ResponseMessage : NotificationId = {0} : response code = {1} : Response Result = {2}", Id, returnCode, responseBasic.Result); return response; }
private void SendEmail(MailMessage mail, NotificationProcessingAudit audit) { var smtphost = _settingsRepository.GetByKey(SettingsKeys.SmtpHost); string smtphostvalue = ""; if (smtphost != null) smtphostvalue = smtphost.Value; var smtpport = _settingsRepository.GetByKey(SettingsKeys.SmptPort); int smtportvalue = 0; if (smtpport != null) { int.TryParse(smtpport.Value, out smtportvalue); } var smtpEmail = _settingsRepository.GetByKey(SettingsKeys.SmptEmail); string smtpEmailvalue = ""; if (smtpEmail != null) smtpEmailvalue = smtpEmail.Value; var smtpusername = _settingsRepository.GetByKey(SettingsKeys.SmptUsername); string smtpusernamevalue = ""; if (smtpusername != null) smtpusernamevalue = smtpusername.Value; var smtppassword = _settingsRepository.GetByKey(SettingsKeys.SmptPassword); string smtppasswordvalue = ""; if (smtppassword != null) smtppasswordvalue = VCEncryption.DecryptString(smtppassword.Value); if (string.IsNullOrWhiteSpace(smtpEmailvalue) || string.IsNullOrWhiteSpace(smtphostvalue) || smtportvalue == 0 || string.IsNullOrWhiteSpace(smtpusernamevalue) || string.IsNullOrWhiteSpace(smtppasswordvalue)) throw new Exception("Invalid notification settings"); SmtpClient client = new SmtpClient(); mail.From = new MailAddress(smtpEmailvalue); client.Host = smtphostvalue; client.Port = smtportvalue; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(smtpusernamevalue, smtppasswordvalue); client.SendCompleted += (sender, e) => { var notification = (NotificationProcessingAudit) e.UserState; var info = new NotificationProcessingAuditInfo { Id = notification.Id.ToString()+"_Email", DateInserted = DateTime.Now, NotificationId = notification.Id, Type = "Email", Contact = string.Join(",", mail.To.Select(s=>s.Address).ToArray()) }; if (e.Error != null) { info.Info = e.Error.Message; if (e.Error.InnerException != null) info.Info += " --> " + e.Error.InnerException.Message; _processingAuditRepository.Add(info); } else { notification.Info = "Sent"; _processingAuditRepository.Add(info); } }; client.SendAsync(mail, audit); }
public void Add(NotificationProcessingAudit audit) { _logger.Info("Failed to add audit --> " + JsonConvert.SerializeObject(audit) ); //throw new NotImplementedException(); }