private void SendUnmatchedDispatchMail(SubscriberInfoBySubscriberIds ds, string dispatchFromData, string messagebody, string toData) { string from = _AppSettings.EmlOnErrorEmailFrom; string to = _AppSettings.EmlOnUnmmatchedDispatchEmailTo; string subject = string.Format("Unmatched dispatch for [{0}]", ds.Fullname); string messageBody = string.Format("An unmatched dispatch has been received for this subscriber."); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("Their on-screen name is: [{0}]", ds.Subscribername)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("Their agency log-in name is: [{0}]", ds.Loginname)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("Their current status is: [{0}]", ds.Statusname)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("The dispatch came from: [{0}]", dispatchFromData)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("They are located in: [{0}], [{1}]", ds.Mailingcountry, ds.Mailingstate)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("The message was: [{0}]", messagebody)); messageBody = string.Join(Environment.NewLine, messageBody, string.Format("This message was sent to: [{0}]", toData)); try { SendMail(from, to, subject, messageBody); } catch (Exception smtpEx) { Logger.Error("Could not send unmatcheddispatch info email. Error: {0}", smtpEx.Message); } }
private async Task SaveUnmatchedDispatch(DispatchMessage message) { DispatchUnmatchedMessages ds = new DispatchUnmatchedMessages(); int IDvalue = 0; ds.Arrivedon = DateTime.Now; ds.Messageheader = message.Messageheader; ds.Messagesubject = message.Messagesubject; ds.Messagebody = message.Messagebody; ds.Messagefrom = message.Originationemailaddress; ds.Messageto = message.Destinationemailaddress; char[] letters = "@abcdefghijklmnopqrstuvwxyz".ToCharArray(); char[] numbers = "0123456789".ToCharArray(); int letterPosition = -1; int numberPosition = -1; int atPosition = -1; string newDestinationsIDs = string.Empty; string destinationEmailAddress = message.Destinationemailaddress; if (message.XReceiver.ToLower().Contains(_AppSettings.Domain)) { destinationEmailAddress = string.Join(",", (object)message.Destinationemailaddress, message.XReceiver.ToLower()); } string[] destinationIDs = destinationEmailAddress.ToLower().Split(','); foreach (string s in destinationIDs) { letterPosition = s.IndexOfAny(letters); Logger.Debug("string: {0}, letterPosition: {1}", s, letterPosition); if (letterPosition > 0) { newDestinationsIDs = string.Join(",", newDestinationsIDs, s.Substring(0, letterPosition)); } else { numberPosition = s.IndexOfAny(numbers); if ((letterPosition == 0) && (numberPosition > 0)) { string newS = s.Substring(numberPosition); atPosition = newS.IndexOfAny(letters); newDestinationsIDs = string.Join(",", newDestinationsIDs, newS.Substring(0, atPosition)); Logger.Debug("string: {0}, letterPosition: {1}, numberPosition: {2}, newDestinationsIDs: {3}", s, letterPosition, numberPosition, newDestinationsIDs); } } } if (newDestinationsIDs.IndexOf(",", StringComparison.Ordinal) == 0) { newDestinationsIDs = newDestinationsIDs.Substring(1); } Logger.Debug("newDestinationsIDs, suspected subscribers: {0}", newDestinationsIDs); // clean up subscriberIDs newDestinationsIDs = Clean(newDestinationsIDs); List <SubscriberInfoBySubscriberIds> SubscriberDS = await _dataAccessProcessor.GetSubscriberInfoBySubscriberIdsInUnmatchedDispatch(newDestinationsIDs); bool sendUnmatchedDispatchMessage = false; if (SubscriberDS != null && SubscriberDS.Count > 0) { SubscriberInfoBySubscriberIds subscriberInfo = SubscriberDS.FirstOrDefault(); ds.Destsubscriberid = subscriberInfo.Id; ds.Destsubscribername = subscriberInfo.Subscribername; ds.Destsubscriberstatusid = subscriberInfo.Statusid; ds.Destsubscriberstatusname = subscriberInfo.Statusname; sendUnmatchedDispatchMessage = (subscriberInfo.Statusid == 1 || subscriberInfo.Statusid == 7); Logger.Debug("Unmatcheddispatch subscriber information. FullName: {0}, ScreenName: {1}, AgencyLogin: {2}, Status: {3}, County: {4}, State: {5}, From Header: {6}", subscriberInfo.Fullname, subscriberInfo.Subscribername, subscriberInfo.Loginname, subscriberInfo.Statusname, subscriberInfo.Mailingcountry, subscriberInfo.Mailingstate, string.Join(",", message.XSender, message.Originationemailaddress)); Logger.Debug("Send mail with unmatched dispatch subscriber information: {0}", sendUnmatchedDispatchMessage); _dispatchUnmatchedMessagesRepository.AddAsync(ds); Logger.Info("Unmatched dispatch subscriber stored in the database"); if (sendUnmatchedDispatchMessage) { SendUnmatchedDispatchMail(subscriberInfo, string.Join(",", message.XSender, message.Originationemailaddress), message.Messagebody, string.Join(",", message.XReceiver, message.Destinationemailaddress)); } } }