private string GetSessionId(IDataRecord record)
        {
            // See if we can use the name of the delivered file
            object result = record["DeliveredFileName"];

            if (result != null)
            {
                string fileName = result.ToString();
                if (ScanFilePrefix.MatchesPattern(fileName))
                {
                    ScanFilePrefix prefix = ScanFilePrefix.Parse(fileName);
                    return(prefix.SessionId);
                }
            }

            // See if we can use the fax number
            result = record["FaxNumber"];
            if (result != null)
            {
                try
                {
                    ScanFilePrefix prefix = ScanFilePrefix.ParseFromFax(result.ToString(), "u");
                    return(prefix.SessionId);
                }
                catch (FormatException)
                {
                    // Wrong format - do nothing
                }
            }

            // Use the last session we saw
            return(_sessionId);
        }
        private string GetFileName(IDataRecord record)
        {
            string fileName = record["DeliveredFileName"] as string;

            // In some cases, we may need to use the email subject instead of the file name
            if (!string.IsNullOrEmpty(fileName) && !ScanFilePrefix.MatchesPattern(fileName))
            {
                string emailSubject = record["EmailSubject"] as string;
                if (!string.IsNullOrEmpty(emailSubject))
                {
                    return(emailSubject + Path.GetExtension(fileName));
                }
            }

            return(fileName);
        }
        protected virtual bool ProcessMessage(EmailMessage message)
        {
            EmailAttachment attachment = message.Attachments.FirstOrDefault();

            if (attachment != null)
            {
                // Determine if we need to pull the file name from the subject
                string fileName = attachment.FileName;
                if (!ScanFilePrefix.MatchesPattern(attachment.FileName))
                {
                    fileName = message.Subject + Path.GetExtension(attachment.FileName);
                }
                TraceFactory.Logger.Debug("Found attachment: " + fileName);

                try
                {
                    ScanFilePrefix filePrefix = ScanFilePrefix.Parse(Path.GetFileName(fileName));

                    // Create the log for this file
                    DigitalSendJobOutputLogger log = new DigitalSendJobOutputLogger(fileName, filePrefix.ToString(), filePrefix.SessionId);
                    log.FileSentDateTime     = message.DateTimeSent;
                    log.FileReceivedDateTime = message.DateTimeReceived;
                    log.FileLocation         = _emailAddress.ToString();

                    // Save the attachment locally
                    FileInfo file = attachment.Save(_tempPath, fileName);

                    // Validate and analyze the file
                    OutputProcessor  processor = new OutputProcessor(file.FullName);
                    ValidationResult result    = null;
                    Retry.WhileThrowing(
                        () => result = processor.Validate(base.Configuration),
                        10,
                        TimeSpan.FromSeconds(2),
                        new Collection <Type>()
                    {
                        typeof(IOException)
                    });

                    DocumentProperties properties = processor.GetProperties();
                    log.FileSizeBytes = properties.FileSize;
                    log.PageCount     = properties.Pages;
                    log.SetErrorMessage(result);

                    // Clean up the file
                    processor.ApplyRetention(base.Configuration, result.Succeeded);

                    // One last check - if there was more than one attachment, flag this as an error
                    if (message.Attachments.Count > 1)
                    {
                        log.ErrorMessage += " {0} attachments with one email.".FormatWith(message.Attachments.Count);
                    }

                    // Send the output log
                    new DataLogger(GetLogServiceHost(filePrefix.SessionId)).Submit(log);
                }
                catch (Exception ex)
                {
                    LogProcessFileError(fileName, ex);
                    return(false);
                }
            }
            else
            {
                TraceFactory.Logger.Debug("Found email with subject {0} but no attachments.".FormatWith(message.Subject));
            }

            return(true);
        }