public void CreateTicketFromEmail(string @from, string body, string subject, IList <int> fileIds, string ticketUrlBase) { //Get default values for the new ticket from TicketDefault table var defaultTicket = _ticketRepository.GetTicketDefaultValues(); //Get customer contact id and customer id by email from var customerContact = _customerRepository.GetCustomerContactByEmail(from); //If there is no customer contact with such email create new customer contact for the customer with Id = -1 (Unknown) if (customerContact == null) { customerContact = _customerRepository.AddCustomerContact(new CustomerContactModel { EMail = from, CustomerId = -1, Priority = defaultTicket.PriorityId }); } //Create ticket var ticket = new TicketModel { CustomerId = customerContact.CustomerId, CustomerUserId = customerContact.Id, CreationDate = DateTime.Now, IssueTypeId = defaultTicket.IssueTypeId, IssueType = new IssueType { Id = defaultTicket.IssueTypeId }, PriorityId = defaultTicket.PriorityId, ProductId = defaultTicket.ProductId, Product = new Product { Id = defaultTicket.ProductId }, TypeId = defaultTicket.TypeId, TicketType = new TicketType { Id = defaultTicket.TypeId }, ProductCategoryId = defaultTicket.ProductCategoryId, ProductCategory = new ProductCategory { Id = defaultTicket.ProductCategoryId }, StatusId = (int)TicketStatusId.Open }; var ticketWorkflowRule = _ticketWorkflowRepository.GetFirstMathingRule(ticket); ticket.Department = new Department { Id = ticketWorkflowRule.Department.Id }; ticket.DepartmentId = ticketWorkflowRule.Department.Id; _ticketRepository.Insert(ticket); //Add custom field value for the ticket (from body) _ticketFieldRepository.Insert(new TicketFieldModel() { CustomFieldId = -1, CustomField = new CustomField { Id = -1 }, Value = body, TextValue = body, TicketId = ticket.Id }); //Add attchements if they exists if (fileIds != null && fileIds.Count > 0) { _ticketFieldRepository.Insert(new TicketFieldModel() { CustomFieldId = -2, CustomField = new CustomField { Id = -2 }, Value = string.Join(",", fileIds), TextValue = "Attachments", TicketId = ticket.Id }); } //Add log record var log = _ticketLogRepository.Insert(ticket.Id, (int)LogEntryTypeId.TicketCreated, "Ticket was created by email", $"Ticket was created from {from}", ""); //Find and Complile email template (replace all %field% with values var emailTemplates = _emailTemplateRepository.GetEmailTemplatesByAction(EmailActionId.TicketCreated); var substitutions = _ticketFieldRepository.GetTicketFieldsSubstitutions(ticket.Id, ticketUrlBase + ticket.Id); //Send email notifications asynchronously SendEmails(ticket.Id, emailTemplates, substitutions); }