public async Task <bool> CreateCall(Tuple <int, string> emailType, Message message) { _log.Information($"ImportService::Creating call for email: {message.Id}||{message.InboundMessage.MessageID}"); var departmentData = _dataService.GetDataProviderByEmailCode(emailType.Item2); if (departmentData != null) { try { int emailFormatType = 0; if (departmentData.Options == null || departmentData.Options.EmailFormatType < 0) { departmentData.Options.EmailFormatType = (int)CallEmailTypes.Generic; } else { emailFormatType = departmentData.Options.EmailFormatType; } List <Call> activeCalls = new List <Call>(); if (CallTypesThatNeedActiveCalls.CallTypes.Contains(emailFormatType)) { activeCalls = await CallsApi.GetActiveCalls(departmentData.DepartmentInfo.Id); } int defaultPriority = (int)ApiClient.Common.CallPriority.High; if (departmentData.CallPriorities != null && departmentData.CallPriorities.Any()) { var defaultPrio = departmentData.CallPriorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false); if (defaultPrio != null) { defaultPriority = defaultPrio.Id; } } var call = _callsService.GenerateCallFromEmail((CallEmailTypes)emailFormatType, message.InboundMessage, departmentData.DepartmentInfo.ManagingUserId, departmentData.DepartmentInfo.Members, departmentData.DepartmentInfo, activeCalls, departmentData.Units, defaultPriority); if (call != null) { call.DepartmentId = departmentData.DepartmentInfo.Id; var savedCall = await CallsApi.AddNewCall(call); } } catch (Exception ex) { _log.Error(ex, $"ImportService::Exception attempting to create call for email: {message.Id}||{message.InboundMessage.MessageID}"); } } return(false); }
public HttpResponseMessage Receive(PostmarkInboundMessage message) { if (message != null) { try { var mailMessage = new MimeMessage(); if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && message.FromFull.Email.Trim() == "*****@*****.**") { return(new HttpResponseMessage(HttpStatusCode.Created)); } if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name)) { mailMessage.From.Add(new MailboxAddress(message.FromFull.Name.Trim(), message.FromFull.Email.Trim())); } else { mailMessage.From.Add(new MailboxAddress("Inbound Email Dispatch", "*****@*****.**")); } if (!String.IsNullOrWhiteSpace(message.Subject)) { mailMessage.Subject = message.Subject; } else { mailMessage.Subject = "Dispatch Email"; } var builder = new BodyBuilder(); if (!String.IsNullOrWhiteSpace(message.HtmlBody)) { builder.HtmlBody = HttpUtility.HtmlDecode(message.HtmlBody); } if (!String.IsNullOrWhiteSpace(message.TextBody)) { builder.TextBody = message.TextBody; } int type = 0; // 1 = dispatch // 2 = email list // 3 = group dispatch // 4 = group message string emailAddress = String.Empty; string bounceEmail = String.Empty; string name = String.Empty; #region Trying to Find What type of email this is foreach (var email in message.ToFull) { if (StringHelpers.ValidateEmail(email.Email)) { if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.DispatchTestDomain}")) { type = 1; if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}")) { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchDomain}", ""); } else { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchTestDomain}", ""); } name = email.Name; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress(email.Name, email.Email)); break; } else if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.ListsTestDomain}")) { type = 2; if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}")) { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsDomain}", ""); } else { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsTestDomain}", ""); } if (emailAddress.Contains("+") && emailAddress.Contains("=")) { var tempBounceEmail = emailAddress.Substring(emailAddress.IndexOf("+") + 1); bounceEmail = tempBounceEmail.Replace("=", "@"); emailAddress = emailAddress.Replace(tempBounceEmail, ""); emailAddress = emailAddress.Replace("+", ""); } name = email.Name; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress(email.Name, email.Email)); break; } else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupsTestDomain}")) { type = 3; if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}")) { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsDomain}", ""); } else { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsTestDomain}", ""); } name = email.Name; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress(email.Name, email.Email)); break; } else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupTestMessageDomain}")) { type = 4; if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}")) { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupMessageDomain}", ""); } else { emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupTestMessageDomain}", ""); } name = email.Name; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress(email.Name, email.Email)); break; } } } // Some providers aren't putting email address in the To line, process the CC line if (type == 0) { foreach (var email in message.CcFull) { if (StringHelpers.ValidateEmail(email.Email)) { var proccedEmailInfo = ProcessEmailAddress(email.Email); if (proccedEmailInfo.Item1 > 0) { type = proccedEmailInfo.Item1; emailAddress = proccedEmailInfo.Item2; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress(email.Name, email.Email)); } } } } // If To and CC didn't work, try the header. if (type == 0) { try { if (message.Headers != null && message.Headers.Count > 0) { var header = message.Headers.FirstOrDefault(x => x.Name == "Received-SPF"); if (header != null) { var lastValue = header.Value.LastIndexOf(char.Parse("=")); var newEmail = header.Value.Substring(lastValue + 1, (header.Value.Length - (lastValue + 1))); newEmail = newEmail.Trim(); if (StringHelpers.ValidateEmail(newEmail)) { emailAddress = newEmail; var proccedEmailInfo = ProcessEmailAddress(newEmail); type = proccedEmailInfo.Item1; emailAddress = proccedEmailInfo.Item2; mailMessage.To.Clear(); mailMessage.To.Add(new MailboxAddress("Email Importer", newEmail)); } } } } catch (Exception ex) { Logging.LogException(ex); } } #endregion Trying to Find What type of email this is if (type == 1) // Dispatch { #region Dispatch Email var departmentId = _departmentSettingsService.GetDepartmentIdForDispatchEmail(emailAddress); if (departmentId.HasValue) { try { var emailSettings = _departmentsService.GetDepartmentEmailSettings(departmentId.Value); List <IdentityUser> departmentUsers = _departmentsService.GetAllUsersForDepartment(departmentId.Value, true); var callEmail = new CallEmail(); if (!String.IsNullOrWhiteSpace(message.Subject)) { callEmail.Subject = message.Subject; } else { callEmail.Subject = "Dispatch Email"; } if (!String.IsNullOrWhiteSpace(message.HtmlBody)) { callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody); } else { callEmail.Body = message.TextBody; } callEmail.TextBody = message.TextBody; foreach (var attachment in message.Attachments) { try { if (Convert.ToInt32(attachment.ContentLength) > 0) { if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr")) { byte[] filebytes = Convert.FromBase64String(attachment.Content); callEmail.DispatchAudioFileName = attachment.Name; callEmail.DispatchAudio = filebytes; } } } catch { } } if (emailSettings == null) { emailSettings = new DepartmentCallEmail(); emailSettings.FormatType = (int)CallEmailTypes.Generic; emailSettings.DepartmentId = departmentId.Value; emailSettings.Department = _departmentsService.GetDepartmentById(departmentId.Value, false); } else if (emailSettings.Department == null) { emailSettings.Department = _departmentsService.GetDepartmentById(departmentId.Value); } var activeCalls = _callsService.GetLatest10ActiveCallsByDepartment(emailSettings.Department.DepartmentId); var units = _unitsService.GetUnitsForDepartment(emailSettings.Department.DepartmentId); var priorities = _callsService.GetActiveCallPrioritesForDepartment(emailSettings.Department.DepartmentId); int defaultPriority = (int)CallPriority.High; if (priorities != null && priorities.Any()) { var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false); if (defaultPrio != null) { defaultPriority = defaultPrio.DepartmentCallPriorityId; } } var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail, emailSettings.Department.ManagingUserId, departmentUsers, emailSettings.Department, activeCalls, units, defaultPriority); if (call != null) { call.DepartmentId = departmentId.Value; var savedCall = _callsService.SaveCall(call); var cqi = new CallQueueItem(); cqi.Call = savedCall; cqi.Profiles = _userProfileService.GetAllProfilesForDepartment(call.DepartmentId).Select(x => x.Value).ToList(); cqi.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(cqi.Call.DepartmentId); _queueService.EnqueueCallBroadcast(cqi); return(new HttpResponseMessage(HttpStatusCode.Created)); } } catch (Exception ex) { Logging.LogException(ex); return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } #endregion Dispatch } else if (type == 2) // Email List { #region Distribution Email var list = _distributionListsService.GetDistributionListByAddress(emailAddress); if (list != null) { if (String.IsNullOrWhiteSpace(bounceEmail)) { try { List <Model.File> files = new List <Model.File>(); try { if (message.Attachments != null && message.Attachments.Any()) { foreach (var attachment in message.Attachments) { if (Convert.ToInt32(attachment.ContentLength) > 0) { Model.File file = new Model.File(); byte[] filebytes = Convert.FromBase64String(attachment.Content); file.Data = filebytes; file.FileName = attachment.Name; file.DepartmentId = list.DepartmentId; file.ContentId = attachment.ContentID; file.FileType = attachment.ContentType; file.Timestamp = DateTime.UtcNow; files.Add(_fileService.SaveFile(file)); } } } } catch { } var dlqi = new DistributionListQueueItem(); dlqi.List = list; dlqi.Users = _departmentsService.GetAllUsersForDepartment(list.DepartmentId); if (files != null && files.Any()) { dlqi.FileIds = new List <int>(); dlqi.FileIds.AddRange(files.Select(x => x.FileId).ToList()); } dlqi.Message = new InboundMessage(); dlqi.Message.Attachments = new List <InboundMessageAttachment>(); if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name)) { dlqi.Message.FromEmail = message.FromFull.Email.Trim(); dlqi.Message.FromName = message.FromFull.Name.Trim(); } dlqi.Message.Subject = message.Subject; dlqi.Message.HtmlBody = message.HtmlBody; dlqi.Message.TextBody = message.TextBody; dlqi.Message.MessageID = message.MessageID; _queueService.EnqueueDistributionListBroadcast(dlqi); } catch (Exception ex) { Logging.LogException(ex); return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } else { return(new HttpResponseMessage(HttpStatusCode.Created)); } } return(new HttpResponseMessage(HttpStatusCode.Created)); #endregion Distribution Email } if (type == 3) // Group Dispatch { #region Group Dispatch Email var departmentGroup = _departmentGroupsService.GetGroupByDispatchEmailCode(emailAddress); if (departmentGroup != null) { try { var emailSettings = _departmentsService.GetDepartmentEmailSettings(departmentGroup.DepartmentId); //var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroup(departmentGroup.DepartmentGroupId); var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup); var callEmail = new CallEmail(); callEmail.Subject = message.Subject; if (!String.IsNullOrWhiteSpace(message.HtmlBody)) { callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody); } else { callEmail.Body = message.TextBody; } foreach (var attachment in message.Attachments) { try { if (Convert.ToInt32(attachment.ContentLength) > 0) { if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr")) { byte[] filebytes = Convert.FromBase64String(attachment.Content); callEmail.DispatchAudioFileName = attachment.Name; callEmail.DispatchAudio = filebytes; } } } catch { } } if (emailSettings == null) { emailSettings = new DepartmentCallEmail(); emailSettings.FormatType = (int)CallEmailTypes.Generic; emailSettings.DepartmentId = departmentGroup.DepartmentId; if (departmentGroup.Department != null) { emailSettings.Department = departmentGroup.Department; } else { emailSettings.Department = _departmentsService.GetDepartmentById(departmentGroup.DepartmentId); } } var activeCalls = _callsService.GetActiveCallsByDepartment(emailSettings.Department.DepartmentId); var units = _unitsService.GetAllUnitsForGroup(departmentGroup.DepartmentGroupId); var priorities = _callsService.GetActiveCallPrioritesForDepartment(emailSettings.Department.DepartmentId); int defaultPriority = (int)CallPriority.High; if (priorities != null && priorities.Any()) { var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false); if (defaultPrio != null) { defaultPriority = defaultPrio.DepartmentCallPriorityId; } } var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail, emailSettings.Department.ManagingUserId, departmentGroupUsers.Select(x => x.User).ToList(), emailSettings.Department, activeCalls, units, defaultPriority); if (call != null) { call.DepartmentId = departmentGroup.DepartmentId; var savedCall = _callsService.SaveCall(call); var cqi = new CallQueueItem(); cqi.Call = savedCall; cqi.Profiles = _userProfileService.GetSelectedUserProfiles(departmentGroupUsers.Select(x => x.UserId).ToList()); cqi.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(cqi.Call.DepartmentId); _queueService.EnqueueCallBroadcast(cqi); return(new HttpResponseMessage(HttpStatusCode.Created)); } } catch (Exception ex) { Logging.LogException(ex); return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } #endregion Group Dispatch Email } if (type == 4) // Group Message { #region Group Message var departmentGroup = _departmentGroupsService.GetGroupByMessageEmailCode(emailAddress); if (departmentGroup != null) { try { //var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroup(departmentGroup.DepartmentGroupId); var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup); var newMessage = new Message(); newMessage.SentOn = DateTime.UtcNow; newMessage.SendingUserId = departmentGroup.Department.ManagingUserId; newMessage.IsBroadcast = true; newMessage.Subject = message.Subject; newMessage.SystemGenerated = true; if (!String.IsNullOrWhiteSpace(message.HtmlBody)) { newMessage.Body = HttpUtility.HtmlDecode(message.HtmlBody); } else { newMessage.Body = message.TextBody; } foreach (var member in departmentGroupUsers) { if (newMessage.GetRecipients().All(x => x != member.UserId)) { newMessage.AddRecipient(member.UserId); } } var savedMessage = _messageService.SaveMessage(newMessage); _messageService.SendMessage(savedMessage, "", departmentGroup.DepartmentId, false); return(new HttpResponseMessage(HttpStatusCode.Created)); } catch (Exception ex) { Logging.LogException(ex); return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } #endregion Group Message } return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } catch (Exception ex) { Framework.Logging.LogException(ex); throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.ToString()) }); } } else { // If our message was null, we throw an exception throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("Error parsing Inbound Message, message is null.") }); } }
public async Task <Tuple <bool, string> > Process(CallEmailQueueItem item) { bool success = true; string result = ""; _callEmailProvider = Bootstrapper.GetKernel().Resolve <ICallEmailProvider>(); if (!String.IsNullOrWhiteSpace(item?.EmailSettings?.Hostname)) { CallEmailsResult emailResult = _callEmailProvider.GetAllCallEmailsFromServer(item.EmailSettings); if (emailResult?.Emails != null && emailResult.Emails.Count > 0) { var calls = new List <Call>(); _callsService = Bootstrapper.GetKernel().Resolve <ICallsService>(); _queueService = Bootstrapper.GetKernel().Resolve <IQueueService>(); _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>(); _unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>(); // Ran into an issue where the department users didn't come back. We can't put the email back in the POP // email box so just added some simple retry logic here. List <IdentityUser> departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true); var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(item.EmailSettings.DepartmentId); int retry = 0; while (retry < 3 && departmentUsers == null) { Thread.Sleep(150); departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true); retry++; } foreach (var email in emailResult.Emails) { var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(item.EmailSettings.Department.DepartmentId); var units = await _unitsService.GetUnitsForDepartmentAsync(item.EmailSettings.Department.DepartmentId); var priorities = await _callsService.GetActiveCallPrioritiesForDepartmentAsync(item.EmailSettings.Department.DepartmentId); int defaultPriority = (int)CallPriority.High; if (priorities != null && priorities.Any()) { var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false); if (defaultPrio != null) { defaultPriority = defaultPrio.DepartmentCallPriorityId; } } var call = _callsService.GenerateCallFromEmail(item.EmailSettings.FormatType, email, item.EmailSettings.Department.ManagingUserId, departmentUsers, item.EmailSettings.Department, activeCalls, units, defaultPriority, priorities); if (call != null) { call.DepartmentId = item.EmailSettings.DepartmentId; if (!calls.Any(x => x.Name == call.Name && x.NatureOfCall == call.NatureOfCall)) { calls.Add(call); } } } if (calls.Any()) { var departmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(item.EmailSettings.DepartmentId); foreach (var call in calls) { try { // Adding this in here to try and fix the error below with ObjectContext issues. var newCall = CreateNewCallFromCall(call); if (newCall.Dispatches != null && newCall.Dispatches.Any()) { // We've been having this error here: // The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects. // So I'm wrapping this in a try catch to prevent all calls form being dropped. var savedCall = await _callsService.SaveCallAsync(newCall); var cqi = new CallQueueItem(); cqi.Call = savedCall; cqi.Profiles = profiles.Values.ToList(); cqi.DepartmentTextNumber = departmentTextNumber; await _queueService.EnqueueCallBroadcastAsync(cqi); } } catch (Exception ex) { result = ex.ToString(); Logging.LogException(ex); } } } await _departmentsService.SaveDepartmentEmailSettingsAsync(emailResult.EmailSettings); _callsService = null; _queueService = null; _departmentsService = null; _callEmailProvider = null; _userProfileService = null; _departmentSettingsService = null; } } return(new Tuple <bool, string>(success, result)); }