private void SaveMail(ref EmailConstruct m) { String msg = ""; //first get all references String checkNumber = m.SentFrom; if (m.SentFrom.StartsWith("27")) { checkNumber = m.SentFrom.Replace("27", "0"); } String refQuery = "SELECT distinct building, customer FROM tblSMS where reference = @reference OR number = @actNumber OR number = @checkNumber"; Dictionary <String, Object> sqlParms = new Dictionary <string, object>(); sqlParms.Add("@building", ""); sqlParms.Add("@customer", ""); sqlParms.Add("@message", m.Body); if (String.IsNullOrEmpty(m.Reference)) { m.Reference = GetReference(checkNumber, m.SentFrom); } sqlParms.Add("@reference", m.Reference); sqlParms.Add("@sent", m.ReceivedDate); sqlParms.Add("@direction", true); sqlParms.Add("@sender", m.SentFrom); sqlParms.Add("@actNumber", m.SentFrom); sqlParms.Add("@checkNumber", checkNumber); DataSet ds = DataHandler.getData(refQuery, sqlParms, out msg); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { sqlParms["@building"] = ds.Tables[0].Rows[0]["building"].ToString(); sqlParms["@customer"] = ds.Tables[0].Rows[0]["customer"].ToString(); sqlParms["@reference"] = m.Reference; sqlParms["@message"] = m.Body; String insertQuery = "INSERT INTO tblSMS(building, customer, number, message, reference, sent, sender)"; insertQuery += " VALUES(@building, @customer, @sender, @message, @reference, @sent, @sender)"; DataHandler.setData(insertQuery, sqlParms, out msg); } }
private void GetMail() { timer.Enabled = false; popMessages = FetchAllMessages(hostName, port, useSSL, username, password); mailMessages = new List <MailMessage>(); if (popMessages.Count > 0) { if (messages == null) { messages = new Dictionary <string, EmailConstruct>(); } foreach (Message m in popMessages) { try { if (!messages.ContainsKey(m.Headers.MessageId)) { if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs("Event Fired: MailPopped")); } EmailConstruct e = new EmailConstruct(); e.ID = m.Headers.MessageId; MailMessage mm = m.ToMailMessage(); String body = mm.Body; e.SentFrom = mm.From.Address.Replace("@2way.co.za", ""); if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs(e.SentFrom)); } if (mm.To.Count > 0) { e.SentTo = mm.To.ToList()[0].Address; } e.ReceivedDate = m.Headers.DateSent; e.Subject = mm.Subject; messages.Add(e.ID, new EmailConstruct()); if (e.Subject == "SMS to email") { #region SMS Emails List <String> refs = References(); foreach (String reference in refs) { if (e.Body.Contains(reference)) { e.Reference = reference; e.Body = e.Body.Replace(reference, ""); break; } } if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs("2")); } e.ForwardDate = new DateTime(1900, 1, 1); e.ForwardedTo = ""; e.HandledDate = new DateTime(1900, 1, 1); e.Handled = false; if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs("Before Save")); } SaveMail(ref e); if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs("After save")); } String buildingQuery = "SELECT distinct u.username, u.email, sms.customer FROM tblSMS AS sms INNER JOIN tblBuildings AS b ON sms.building = b.Code INNER JOIN"; buildingQuery += " tblUserBuildings AS ub ON b.id = ub.buildingid INNER JOIN tblUsers AS u ON ub.userid = u.id"; buildingQuery += " WHERE (sms.sender = '" + e.SentFrom + "')"; String dsStatus = ""; DataSet dsUsers = DataHandler.getData(buildingQuery, out dsStatus); if (dsUsers != null && dsUsers.Tables.Count > 0 && dsUsers.Tables[0].Rows.Count > 0) { String msgfrom = "Astrodon Debtors System"; String subject = "New Message From - " + dsUsers.Tables[0].Rows[0]["customer"].ToString(); String message = "A new sms message has been received from the above customer. Please check the Debtor System for more information."; List <String> toMails = new List <string>(); foreach (DataRow dr in dsUsers.Tables[0].Rows) { String toMail = dr["email"].ToString(); if (!String.IsNullOrEmpty(toMail)) { toMails.Add(toMail); } } if (MailSender.SendMail(msgfrom, toMails, subject, message, false, null)) { String sentMsg = String.Format("Message sent to {0}", String.Join(";", toMails.ToArray())); if (NewMessageEvent != null) { NewMessageEvent(this, new MessageArgs(sentMsg + " " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); } } } #endregion SMS Emails } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } for (int i = messages.Count; i > 0; i--) { DeleteMessageOnServer(hostName, port, useSSL, username, password, i); } } timer.Enabled = true; }