private static void treatBadBloodPatient(object o) { bool sendMail = true; InputData input = o as InputData; if (input.BShowWindowBox) { DialogResult ret = MessageBoxEx.Show(Properties.ResStrings.strAlertBlood, Properties.ResStrings.strAlertDetected, MessageBoxButtons.YesNo, MessageBoxIcon.Stop, 10000); if (ret == DialogResult.No) { sendMail = false; } } if (sendMail) { MainDBClass db = new MainDBClass(); if (db.OpenDB()) { smtpInfo smtp = db.getSmtpInfo(true); alertInfo alert = db.getAlertInfo(AlertType.AT_Bad_Blood_Patient, true); db.CloseDB(); if ((alert != null) && (smtp != null)) { createAndSendMail(alert, smtp, input.Device, input.UTC, input.SpareData); } } } }
public bool AddSmtp(smtpInfo smtp) { switch (useDb) { case dbUsedType.db_SqlLite: return(sqliteProvider.AddSmtp(smtp)); case dbUsedType.db_SqlServer: return(sqlServerProvider.AddSmtp(smtp)); } return(false); }
private void UpdateCTRL() { db = new MainDBClass(); if (db.OpenDB()) { smtp = db.getSmtpInfo(false); if (smtp != null) { textBoxSmtpServer.Text = smtp.smtp; textBoxPort.Text = smtp.port.ToString(); textBoxSender.Text = smtp.sender; textBoxLogin.Text = smtp.login; textBoxPwd.Text = smtp.pwd; checkBoxSSL.Checked = smtp.bUseSSL; checkBoxSMTPActive.Checked = smtp.bActive; } } }
private void button3_Click(object sender, EventArgs e) { try { smtp = new smtpInfo(); smtp.smtp = textBoxSmtpServer.Text; smtp.port = Convert.ToInt32(textBoxPort.Text); smtp.sender = textBoxSender.Text; smtp.login = textBoxLogin.Text; smtp.pwd = textBoxPwd.Text; smtp.bUseSSL = checkBoxSSL.Checked; smtp.bActive = checkBoxSMTPActive.Checked; db.DeleteSMTP(); db.AddSmtp(smtp); MessageBox.Show(Properties.ResStrings.strAlertSmtpSaved, Properties.ResStrings.strAlertSmtpInfo, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exp) { ErrorMessage.ExceptionMessageBox.Show(exp); } }
private static void treatValueRemoved(object o) { InputData input = o as InputData; if (input.BShowWindowBox) { MessageBoxEx.Show(Properties.ResStrings.strAlertValue, Properties.ResStrings.strAlertDetected, MessageBoxButtons.OK, MessageBoxIcon.Stop, 10000); } MainDBClass db = new MainDBClass(); if (db.OpenDB()) { smtpInfo smtp = db.getSmtpInfo(true); alertInfo alert = db.getAlertInfo(AlertType.AT_Limit_Value_Exceed, true); db.CloseDB(); if ((alert != null) && (smtp != null)) { createAndSendMail(alert, smtp, input.Device, input.UTC, input.SpareData); } } }
private void createAndSendMail(alertInfo alert, smtpInfo smtp) { bool mailcreated = false; //create the mail message MailMessage mail = null; string mailbody = null; mailcreated = true; mail = new MailMessage(); //set the addresses mail.From = new MailAddress(smtp.sender); string[] recpt = alert.RecipientList.Split(';'); foreach (string str in recpt) { mail.To.Add(str.Trim()); } if (!string.IsNullOrEmpty(alert.BCCRecipientList)) { string[] bcc = alert.BCCRecipientList.Split(';'); foreach (string str in bcc) { mail.Bcc.Add(str.Trim()); } } if (!string.IsNullOrEmpty(alert.CCRecipientList)) { string[] cc = alert.CCRecipientList.Split(';'); foreach (string str in cc) { mail.CC.Add(str.Trim()); } } string subject = alert.MailSubject; subject = subject.Replace("\n", "<br/>"); mail.Subject = subject; string body = alert.AlertMessage; body = body.Replace("\n", "<br/>"); mailbody = body; /*Attachment data = new Attachment(path); * mail.Attachments.Add(data);*/ if (mailcreated) { //first we create the Plain Text part AlternateView plainView = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/plain"); //then we create the Html part //to embed images, we need to use the prefix 'cid' in the img src value //the cid value will map to the Content-Id of a Linked resource. //thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo' AlternateView htmlView; htmlView = AlternateView.CreateAlternateViewFromString(mailbody + "<br />", null, "text/html"); //add the views mail.AlternateViews.Add(plainView); mail.AlternateViews.Add(htmlView); //send the message SmtpClient SmtpServer = new SmtpClient(smtp.smtp); SmtpServer.Port = smtp.port; if (smtp.bUseSSL) { SmtpServer.EnableSsl = true; } SmtpServer.Credentials = new System.Net.NetworkCredential(smtp.login, smtp.pwd); SmtpServer.Send(mail); MessageBox.Show(Properties.ResStrings.strAlertTestOk, Properties.ResStrings.stralertTestInfo, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private static void createAndSendMail(alertInfo alert, smtpInfo smtp, DeviceInfo device, UserClassTemplate utc, string spareData) { bool mailcreated = false; //create the mail message MailMessage mail = null; string mailbody = null; mailcreated = true; mail = new MailMessage(); //set the addresses mail.From = new MailAddress(smtp.sender); string[] recpt = alert.RecipientList.Split(';'); foreach (string str in recpt) { mail.To.Add(str.Trim()); } if (!string.IsNullOrEmpty(alert.BCCRecipientList)) { string[] bcc = alert.BCCRecipientList.Split(';'); foreach (string str in bcc) { mail.Bcc.Add(str.Trim()); } } if (!string.IsNullOrEmpty(alert.CCRecipientList)) { string[] cc = alert.CCRecipientList.Split(';'); foreach (string str in cc) { mail.CC.Add(str.Trim()); } } /* mail.Subject = alert.MailSubject + " " + device.DeviceName + " [S/N:" + device.SerialRFID + "]"; * mailbody = string.Empty; * * mailbody += "Alert type : <B>" + alert.AlertMessage + "</B><br />"; * mailbody += "Alert date : <B>" + DateTime.Now.ToLongDateString() + " at " + DateTime.Now.ToLongTimeString() + "</B><br />"; * mailbody += "Alert on device : <B>" + device.DeviceName + " [s/n: " + device.SerialRFID + "]" + "</B><br />"; * * switch (alert.type) * { * case AlertType.AT_Door_Open_Too_Long: * case AlertType.AT_Finger_Alert: * mailbody += "Alert generated by user : <B>" + utc.firstName + " " + utc.lastName + "</B><br />"; * break; * case AlertType.AT_Remove_Too_Many_Items: * case AlertType.AT_Limit_Value_Exceed: * mailbody += "Alert generated by user : <B>" + utc.firstName + " " + utc.lastName + "</B><br />"; * mailbody += "User removed <B>" + spareData + "</B><br />"; * break; * } */ string subject = alert.MailSubject; subject = subject.Replace("\n", "<br/>"); if (device != null) { subject = subject.Replace("[READERNAME]", device.DeviceName); subject = subject.Replace("[READERSERIAL]", device.SerialRFID); } if (utc != null) { subject = subject.Replace("[USERNAME]", utc.firstName + " " + utc.lastName); } subject = subject.Replace("[DATE]", DateTime.Now.ToLongDateString() + " at " + DateTime.Now.ToLongTimeString()); mail.Subject = subject; string body = alert.AlertMessage; body = body.Replace("\n", "<br/>"); if (device != null) { body = body.Replace("[READERNAME]", device.DeviceName); body = body.Replace("[READERSERIAL]", device.SerialRFID); } if (utc != null) { body = body.Replace("[USERNAME]", utc.firstName + " " + utc.lastName); } body = body.Replace("[DATE]", DateTime.Now.ToLongDateString() + " at " + DateTime.Now.ToLongTimeString()); body += "<br />"; switch (alert.type) { case AlertType.AT_Remove_Too_Many_Items: case AlertType.AT_Limit_Value_Exceed: body += "<br />User removed " + spareData + "<br />"; break; case AlertType.AT_Max_Fridge_Temp: body += "<br />" + spareData + "<br />"; break; case AlertType.AT_Remove_Tag_Max_Time: string[] dt = spareData.Split(';'); if (dt != null) { int nbTag = dt.Length - 1; body += "<br />All the following " + nbTag + " tag(s) have been removed from device than more " + dt[0] + " Minutes<br />"; for (int i = 1; i < dt.Length; i++) { body += " <br /> " + dt[i]; } } break; case AlertType.AT_Stock_Limit: string[] spData = spareData.Split(';'); if (spData != null) { for (int i = 0; i < spData.Length; i += 2) { body += "<br />Product " + spData[i] + " reach low stock limit : " + spData[i + 1] + " Left"; } } break; case AlertType.AT_DLC_Expired: /* string[] dt2 = spareData.Split(';'); * * if (dt2!= null) * { * int nbTag = dt2.Length - 1; * body += "<br />All the following " + nbTag + " product(s) have overtake their date of use<br />"; * * for (int i = 1; i < dt2.Length; i++) * body += " <br /> " + dt2[i]; * } */ break; case AlertType.AT_Bad_Blood_Patient: string[] dt3 = spareData.Split(';'); if (dt3 != null) { string patient = dt3[0]; body += "<br />The following blood bags removed are not for patient " + patient + "<br />"; for (int i = 1; i < dt3.Length; i++) { body += " <br /> " + dt3[i]; } } break; } mailbody = body; /*Attachment data = new Attachment(path); * mail.Attachments.Add(data);*/ if (mailcreated) { //first we create the Plain Text part AlternateView plainView = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/plain"); //then we create the Html part //to embed images, we need to use the prefix 'cid' in the img src value //the cid value will map to the Content-Id of a Linked resource. //thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo' AlternateView htmlView; htmlView = AlternateView.CreateAlternateViewFromString(mailbody + "<br />", null, "text/html"); //add the views mail.AlternateViews.Add(plainView); mail.AlternateViews.Add(htmlView); //send the message SmtpClient SmtpServer = new SmtpClient(smtp.smtp); SmtpServer.Port = smtp.port; if (smtp.bUseSSL) { SmtpServer.EnableSsl = true; } SmtpServer.Credentials = new System.Net.NetworkCredential(smtp.login, smtp.pwd); SmtpServer.Send(mail); } }