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); }
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 void Add(NotificationProcessingAuditInfo audit) { // throw new NotImplementedException(); }