private static void SetEmailContents(string FileName, int intAttachmentId, string strFullFilePath, string ConnectionString, ref DTOTaskDetail objDTOTaskDetail)
        {
            GeneralSettings objGeneralSettings = new GeneralSettings(ConnectionString);

            if (objGeneralSettings.StorageFileType == "AzureStorage")
            {
                CloudStorageAccount storageAccount     = null;
                CloudBlobContainer  cloudBlobContainer = null;

                // Retrieve the connection string for use with the application.
                string storageConnectionString = objGeneralSettings.AzureStorageConnection;

                // Check whether the connection string can be parsed.
                if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
                {
                    // Ensure there is a AdefHelpDesk Container
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                    cloudBlobContainer = cloudBlobClient.GetContainerReference("adefhelpdesk-files");
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(FileName);

                    // Download
                    cloudBlockBlob.FetchAttributesAsync().Wait();
                    long   fileByteLength = cloudBlockBlob.Properties.Length;
                    byte[] fileContent    = new byte[fileByteLength];
                    for (int i = 0; i < fileByteLength; i++)
                    {
                        fileContent[i] = 0x20;
                    }

                    cloudBlockBlob.DownloadToByteArrayAsync(fileContent, 0).Wait();

                    using (MemoryStream memstream = new MemoryStream(fileContent))
                    {
                        var message = MimeMessage.Load(memstream);

                        var visitor = new HtmlPreviewVisitor();
                        message.Accept(visitor);
                        objDTOTaskDetail.emailDescription = Utility.CleanOutlookFontDefinitions(visitor.HtmlBody);

                        // Add attachments (if any)
                        objDTOTaskDetail.colDTOAttachment = new List <DTOAttachment>();

                        foreach (var item in visitor.Attachments)
                        {
                            if (item.ContentDisposition.FileName != null)
                            {
                                DTOAttachment objDTOAttachment = new DTOAttachment();

                                objDTOAttachment.attachmentID     = intAttachmentId;
                                objDTOAttachment.attachmentPath   = "EML";
                                objDTOAttachment.userId           = "-1";
                                objDTOAttachment.fileName         = item.ContentDisposition.FileName;
                                objDTOAttachment.originalFileName = strFullFilePath;

                                objDTOTaskDetail.colDTOAttachment.Add(objDTOAttachment);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("AzureStorage configured but AzureStorageConnection value cannot connect.");
                }
            }
            else
            {
                using (var fileContents = System.IO.File.OpenRead(strFullFilePath))
                {
                    var message = MimeMessage.Load(fileContents);

                    var visitor = new HtmlPreviewVisitor();
                    message.Accept(visitor);
                    objDTOTaskDetail.emailDescription = Utility.CleanOutlookFontDefinitions(visitor.HtmlBody);

                    // Add attachments (if any)
                    objDTOTaskDetail.colDTOAttachment = new List <DTOAttachment>();

                    foreach (var item in visitor.Attachments)
                    {
                        if (item.ContentDisposition.FileName != null)
                        {
                            DTOAttachment objDTOAttachment = new DTOAttachment();

                            objDTOAttachment.attachmentID     = intAttachmentId;
                            objDTOAttachment.attachmentPath   = "EML";
                            objDTOAttachment.userId           = "-1";
                            objDTOAttachment.fileName         = item.ContentDisposition.FileName;
                            objDTOAttachment.originalFileName = strFullFilePath;

                            objDTOTaskDetail.colDTOAttachment.Add(objDTOAttachment);
                        }
                    }
                }
            }
        }
        public static DTOTask GetTask(DTOTask paramDTOTask, int intUserID, bool IsAdministrator, string DefaultConnection, string strCurrentUser, bool IsAuthenticated)
        {
            DTOTask objTask = new DTOTask();

            objTask.taskId = -1; // Task Not found
            var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();

            optionsBuilder.UseSqlServer(DefaultConnection);

            using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
            {
                AdefHelpDeskTasks Result;

                // Perform Search
                if (paramDTOTask.ticketPassword != "")
                {
                    // Using ticketPassword
                    Result = (from task in context.AdefHelpDeskTasks
                              .Include(details => details.AdefHelpDeskTaskDetails)
                              .Include(categories => categories.AdefHelpDeskTaskCategories)
                              where task.TicketPassword == paramDTOTask.ticketPassword
                              where task.TaskId == paramDTOTask.taskId
                              select task).FirstOrDefault();

                    var myresult = Result.TaskId;
                }
                else
                {
                    // Using TaskId
                    Result = (from task in context.AdefHelpDeskTasks
                              .Include(details => details.AdefHelpDeskTaskDetails)
                              .Include(categories => categories.AdefHelpDeskTaskCategories)
                              where task.TaskId == paramDTOTask.taskId
                              select task).FirstOrDefault();

                    // Must be a Administrator or Requester to only use TaskId
                    if (!IsAdministrator)
                    {
                        if (!(Result.RequesterUserId == intUserID))
                        {
                            if (!UtilitySecurity.IsAdministrator(strCurrentUser, DefaultConnection))
                            {
                                return(objTask);
                            }
                        }
                    }
                }

                if (Result == null)
                {
                    return(objTask);
                }

                objTask.taskId              = Result.TaskId;
                objTask.status              = Result.Status;
                objTask.assignedRoleId      = Result.AssignedRoleId;
                objTask.createdDate         = Result.CreatedDate.ToShortDateString();
                objTask.description         = Result.Description;
                objTask.dueDate             = (Result.DueDate != null) ? Result.DueDate.Value.ToShortDateString() : "";
                objTask.estimatedCompletion = (Result.EstimatedCompletion != null) ? Result.EstimatedCompletion.Value.ToShortDateString() : "";
                objTask.estimatedHours      = Result.EstimatedHours;
                objTask.estimatedStart      = (Result.EstimatedStart != null) ? Result.EstimatedStart.Value.ToShortDateString() : "";
                objTask.portalId            = Result.PortalId;
                objTask.priority            = Result.Priority;
                objTask.requesterEmail      = Result.RequesterEmail;
                objTask.requesterName       = Result.RequesterName;
                objTask.requesterPhone      = Result.RequesterPhone;
                objTask.requesterUserId     = Result.RequesterUserId;
                objTask.ticketPassword      = Result.TicketPassword;

                // Set Requester Name
                if (Result.RequesterUserId > 0)
                {
                    var User = UtilitySecurity.UserFromUserId(Result.RequesterUserId, DefaultConnection);
                    objTask.requesterName = $"{User.firstName} {User.lastName}";
                }
                else
                {
                    objTask.requesterName = Result.RequesterName;
                }

                // Add Task Categories
                objTask.selectedTreeNodes = new List <int>();
                foreach (var itemTaskCategory in Result.AdefHelpDeskTaskCategories)
                {
                    objTask.selectedTreeNodes.Add(itemTaskCategory.CategoryId);
                }

                // Add Task Details
                objTask.colDTOTaskDetail = new List <DTOTaskDetail>();

                // Get all TaskDetails
                var TaskDetails = Result.AdefHelpDeskTaskDetails.OrderByDescending(x => x.DetailId);

                // Non-Admins can only see "Comment - Visible"
                if (!IsAdministrator)
                {
                    TaskDetails = TaskDetails.Where(x => x.DetailType == "Comment - Visible").OrderByDescending(x => x.DetailId);
                }
                else
                {
                    TaskDetails = TaskDetails.OrderByDescending(x => x.DetailId);
                }

                foreach (var itemTaskDetail in TaskDetails)
                {
                    DTOTaskDetail objDTOTaskDetail = new DTOTaskDetail();

                    objDTOTaskDetail.contentType = (itemTaskDetail.ContentType != null) ? itemTaskDetail.ContentType : Constants.TXT;
                    objDTOTaskDetail.description = itemTaskDetail.Description;
                    objDTOTaskDetail.detailId    = itemTaskDetail.DetailId;
                    objDTOTaskDetail.detailType  = itemTaskDetail.DetailType;
                    objDTOTaskDetail.insertDate  = itemTaskDetail.InsertDate.ToLongDateString() + " " + itemTaskDetail.InsertDate.ToLongTimeString();
                    objDTOTaskDetail.startTime   = (itemTaskDetail.StartTime != null) ? itemTaskDetail.StartTime.Value.ToShortDateString() + " " + itemTaskDetail.StartTime.Value.ToShortTimeString() : "";
                    objDTOTaskDetail.stopTime    = (itemTaskDetail.StopTime != null) ? itemTaskDetail.StopTime.Value.ToShortDateString() + " " + itemTaskDetail.StopTime.Value.ToShortTimeString() : "";
                    objDTOTaskDetail.userId      = itemTaskDetail.UserId;
                    objDTOTaskDetail.userName    = UtilitySecurity.UserFromUserId(itemTaskDetail.UserId, DefaultConnection).userName;

                    // Add Attachments
                    objDTOTaskDetail.colDTOAttachment = new List <DTOAttachment>();

                    var AttachmentResults = (from attachment in context.AdefHelpDeskAttachments
                                             where attachment.DetailId == objDTOTaskDetail.detailId
                                             select attachment);

                    foreach (var itemAttachmement in AttachmentResults)
                    {
                        DTOAttachment objDTOAttachment = new DTOAttachment();

                        objDTOAttachment.attachmentID = itemAttachmement.AttachmentId;
                        //objDTOAttachment.attachmentPath = itemAttachmement.AttachmentPath; -- Do not send for security reasons
                        //objDTOAttachment.fileName = itemAttachmement.FileName; -- Do not send for security reasons
                        objDTOAttachment.originalFileName = itemAttachmement.OriginalFileName;
                        objDTOAttachment.userId           = itemAttachmement.UserId.ToString();

                        objDTOTaskDetail.colDTOAttachment.Add(objDTOAttachment);

                        // If file type is .EML it is a Email
                        if (Path.GetExtension(itemAttachmement.OriginalFileName).ToUpper() == Constants.EML)
                        {
                            // Construct path
                            string FullFilePath = Path.Combine(itemAttachmement.AttachmentPath, itemAttachmement.FileName).Replace(@"\", @"/");
                            // Set Email Description and ContentType
                            SetEmailContents(itemAttachmement.FileName, itemAttachmement.AttachmentId, FullFilePath, DefaultConnection, ref objDTOTaskDetail);
                            objDTOTaskDetail.contentType = Constants.EML.Replace(".", "");
                        }
                    }

                    objTask.colDTOTaskDetail.Add(objDTOTaskDetail);
                }
            }

            #region **** Save to the Log
            if ((objTask.taskId != null) && (objTask.taskId != -1))
            {
                string strLogUserName = (IsAuthenticated) ? strCurrentUser : "******";
                Log.InsertLog(DefaultConnection, Convert.ToInt32(objTask.taskId), intUserID, $"{strLogUserName} viewed ticket.");
            }
            #endregion

            return(objTask);
        }