public AlertDelivery SendMessage(AlertDelivery alertDelivery) { //SmsUrlWithCredientials = "http://api.textlocal.in/send/?username="******"&apiKey=" + apiKey + "&numbers=" + numbers + "&message=" + message + "&sender=" + sender; //string SmsUrlWithCredientials = "http://api.textlocal.in/send/[email protected]&apiKey=vzCOfy3HGAc-NkjC02r1abi5iEz1tiqTYmkbvCpKM2&numbers={0}&message={1}&sender=TXTLCL"; string _SmsApiUrl = alertDelivery.SMSApiSettings.ApiUrl ?? SmsApiUrl; string _SmsApiUrlWithCredientials = alertDelivery.SMSApiSettings.ApiUrlWithCredientials ?? SmsApiUrlWithCredientials; alertDelivery.ToAddresses.ForEach(e => { if (e.MediumType == AlertMediumType.SMS) { string _url = string.Format(_SmsApiUrl, "&", e.ToAddress, alertDelivery.SmsContent.PlainText, alertDelivery.SmsContent.Subject); string _data = string.Format(_SmsApiUrlWithCredientials, "&", e.ToAddress, alertDelivery.SmsContent.PlainText, alertDelivery.SmsContent.Subject); string result = SendSms(_url, _data); if (!string.IsNullOrWhiteSpace(result)) { // Not a c# issue if (result.Contains("\"status\":\"success\"")) { e.SentStatus = DeliveryStatus.Success; e.ErrorMessage = result; } else if (result.Contains("\"status\":\"failure\"")) { e.SentStatus = DeliveryStatus.Failed; e.ErrorMessage = result; } else { // Technical issue e.SentStatus = DeliveryStatus.Failed; e.ErrorMessage = "Technical issue " + result; } } else { e.SentStatus = DeliveryStatus.Failed; e.ErrorMessage = result; } } }); return alertDelivery; }
private void StoreDeliverLog(string DeviceId, int DeviceAlertId, DeviceAlarmType AlertType, AlertDelivery ad) { try { ad.ToAddresses.ForEach(receivers => { Data.StoreData_ExecuteNonQuery(DataBase.Alert, CommandType.StoredProcedure, "A_StoreEmailLog", new SqlParameter[] { new SqlParameter("DeviceAlertId", DeviceAlertId), new SqlParameter("DeviceId", DeviceId), new SqlParameter("AlertType", AlertType), new SqlParameter("Email", receivers.ToAddress), new SqlParameter("EmailSubject", (ad.EmailContent != null) ? ad.EmailContent.Subject: string.Empty), new SqlParameter("EmailPlainText", (ad.EmailContent != null) ? ad.EmailContent.PlainText: string.Empty), new SqlParameter("EmailHtmlContent", (ad.EmailContent != null) ? ad.EmailContent.HtmlContent: string.Empty), new SqlParameter("SmsSubject", (ad.SmsContent!= null) ?ad.SmsContent.Subject: string.Empty), new SqlParameter("SmsPlainText", (ad.SmsContent!= null) ?ad.SmsContent.PlainText: string.Empty), new SqlParameter("MediumType", Convert.ToInt32(receivers.MediumType)), new SqlParameter("SentStatus", receivers.SentStatus.ToString()), new SqlParameter("ErrorMessage", receivers.ErrorMessage) }); }); } catch (Exception ex) { Console.WriteLine(ex); } }
public void ProcessFiredAlerts() { // Get Generated alerts List<ATDevice> generatedAlerts = GetFiredAlerts(this.ProcessorId); generatedAlerts.ForEach(d => { ATCustomerDetail customerDetail = new ATCustomerDetail(); customerDetail = GetCustomerDetails(d.Id); if (customerDetail != null && customerDetail.IsAccountExpired == false) { // Check for the particular customer is having message count (Email/SMS) d.Alerts.ForEach(alert => { try { // Get templates by its alert type from Global cached variable AlertDelivery ed = new AlertDelivery(); // Get Email lists ed.ToAddresses = GetFiredAlertsReceivers(alert.Id); ed.ToAddresses = ed.ToAddresses ?? new List<AlertDeliveryNStatus>(); // TODO Get SMS lists if (ed.ToAddresses.Count > 0) { try { if (ed.ToAddresses.Where(m => m.MediumType == AlertMediumType.Email).ToList().Count > 0 && customerDetail.EMAILBalance > 0) { ed.EmailContent = TemplateParser.GetEmailTemplateWithValues(alert.AlarmType, d); ed.SmtpSettings = new SmtpSettings(); ed = new EmailAlert().SendMail(ed); customerDetail.EMAILBalance = customerDetail.EMAILBalance - ed.ToAddresses.Select(m => m.MediumType == AlertMediumType.Email && m.SentStatus == DeliveryStatus.Success).Count(); } } catch (Exception ex) { } try { if (ed.ToAddresses.Where(m => m.MediumType == AlertMediumType.SMS).ToList().Count > 0 && customerDetail.SMSBalance > 0) { ed.SmsContent = TemplateParser.GetSmsTemplateWithValues(alert.AlarmType, d); ed.SMSApiSettings = new SMSApiSettings(); ed = new SmsAlert().SendMessage(ed); customerDetail.SMSBalance = customerDetail.SMSBalance - ed.ToAddresses.Select(m => m.MediumType == AlertMediumType.SMS && m.SentStatus == DeliveryStatus.Success).Count(); } } catch (Exception ex) { } StoreDeliverLog(d.Id, alert.Id, alert.AlarmType, ed); } } catch (Exception ex) { Console.WriteLine(ex); } }); SaveCustomerDetails(customerDetail.UserId, customerDetail.EMAILBalance, customerDetail.SMSBalance, customerDetail.NOTIFICATIONBalance); } }); // Save status UpdateFiredAlerts(this.ProcessorId); }
//[TestMethod] public void ProcessTestEmail() { AlertDelivery ed = new AlertDelivery(); ed.EmailContent = TemplateParser.GetEmailTemplateWithValues(DeviceAlarmType.SpeedAlarm, new object { }); ed.SmtpSettings = new SmtpSettings(); ed.ToAddresses = new List<AlertDeliveryNStatus>(); ed.ToAddresses.Add(new AlertDeliveryNStatus() { ToAddress = "*****@*****.**" }); ed.ToAddresses.Add(new AlertDeliveryNStatus() { ToAddress = "*****@*****.**" }); new EmailAlert().SendMail(ed); }
public AlertDelivery SendMail(AlertDelivery emailDelivery) { SmtpSettings smtpSettings = emailDelivery.SmtpSettings; if (smtpSettings == null) { smtpSettings = new SmtpSettings(); } smtpSettings.Host = smtpSettings.Host ?? Base_Host; smtpSettings.Port = smtpSettings.Port ?? Base_Port; smtpSettings.EnableSsl = smtpSettings.EnableSsl ?? Base_EnableSsl; smtpSettings.Timeout = smtpSettings.Timeout ?? Base_Timeout; smtpSettings.DeliveryMethod = smtpSettings.DeliveryMethod ?? Base_DeliveryMethod; smtpSettings.UseDefaultCredentials = smtpSettings.UseDefaultCredentials ?? Base_UseDefaultCredentials; smtpSettings.CredentialUsername = smtpSettings.CredentialUsername ?? Base_CredentialUsername; smtpSettings.CredentialPassword = smtpSettings.CredentialPassword ?? Base_CredentialPassword; smtpSettings.CredentialUsernameDisplay = smtpSettings.CredentialUsernameDisplay ?? Base_CredentialUsernameDisplay; // Command line argument must the the SMTP host. SmtpClient client = new SmtpClient(); client.Host = smtpSettings.Host; client.Port = smtpSettings.Port ?? default(int); client.EnableSsl = smtpSettings.EnableSsl ?? default(bool); client.Timeout = smtpSettings.Timeout ?? default(int); client.DeliveryMethod = smtpSettings.DeliveryMethod ?? default(SmtpDeliveryMethod); client.UseDefaultCredentials = smtpSettings.UseDefaultCredentials ?? default(bool); client.Credentials = new NetworkCredential(smtpSettings.CredentialUsername, smtpSettings.CredentialPassword); MailAddress from = new MailAddress( smtpSettings.CredentialUsername, smtpSettings.CredentialUsernameDisplay, Encoding.UTF8 ); emailDelivery.ToAddresses.ForEach(e => { if (e.MediumType == AlertMediumType.Email) { MailMessage message = new MailMessage(); message.From = from; message.To.Add(new MailAddress(e.ToAddress)); message.Subject = emailDelivery.EmailContent.Subject; message.Body = emailDelivery.EmailContent.PlainText; AlternateView alternate = AlternateView.CreateAlternateViewFromString(emailDelivery.EmailContent.HtmlContent, new ContentType("text/html")); message.AlternateViews.Add(alternate); try { client.Send(message); Thread.Sleep(150); e.SentStatus = DeliveryStatus.Success; } catch (Exception ex) { e.SentStatus = DeliveryStatus.Failed; e.ErrorMessage = ex.Message + "\n" + (ex.InnerException == null ? "" : ex.InnerException.ToString()); } finally { message = null; } } }); return emailDelivery; }