partial void DeleteBatchNumber(BatchNumber instance);
 partial void InsertBatchNumber(BatchNumber instance);
 partial void UpdateBatchNumber(BatchNumber instance);
示例#4
0
        private Boolean UploadMessage(String fileName, MailboxProfile profile, String storagePath)
        {
            var options = new MailMessageLoadOptions();

            options.FileCompatibilityMode = FileCompatibilityMode.SkipValidityChecking;
            options.MessageFormat         = fileName.ToLower().EndsWith("msg") ? MessageFormat.Msg : MessageFormat.Eml;

            using (MailMessage message = MailMessage.Load(fileName, options))
                using (EmailImportDataContext ctx = new EmailImportDataContext())
                {
                    // Truncate the subject to avoid data commit errors
                    message.Subject = Truncate(message.Subject, 500);

                    if (!IsDuplicate(ctx, profile.MailboxGUID, message))
                    {
                        // Create an instance of the email database object
                        var email = new Email();

                        // Assign properties
                        email.MailboxGUID  = profile.MailboxGUID;
                        email.DateSent     = message.DateSent();
                        email.DateReceived = message.DateReceived();
                        email.From         = message.From.GetAddressOrDisplayName();
                        email.MessageID    = message.MessageId;
                        if (ExistsCCNumber(message.Subject))
                        {
                            email.Subject = MaskCCNumbers(message.Subject, '#');
                        }
                        else
                        {
                            email.Subject = message.Subject;
                        }
                        email.Timestamp = DateTime.Now;

                        // Create the dated storage path
                        var path = Path.Combine(storagePath, email.Timestamp.Value.ToString("yyyyMMdd"));

                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }

                        // Insert the new record
                        ctx.Emails.InsertOnSubmit(email);

                        // Submit the email (we need to get the email ID)
                        using (TransactionScope scope = new TransactionScope())
                        {
                            // Initial submit of changes
                            ctx.SubmitChanges();

                            // Build the mail message file name
                            email.MessageFilePath = Path.Combine(path, String.Format("{0:00000000}.eml", email.EmailID));

                            // Copy the eml file if its already in this format, if msg then save as eml
                            if (fileName.EndsWith("eml", StringComparison.OrdinalIgnoreCase))
                            {
                                File.Copy(fileName, email.MessageFilePath, true);
                            }
                            else
                            {
                                // Save in eml format
                                message.Save(email.MessageFilePath, MessageFormat.Eml);
                            }

                            // Get the batch number - THIS SHOULD NEVER HAPPEN IN A MULTI THREAD SCENARIO WITHOUT A LOCK
                            var batchNumber = ctx.BatchNumbers.SingleOrDefault(b => b.Group == profile.Group);

                            // If there is no batchNumber defined yet, create and insert one
                            if (batchNumber == null)
                            {
                                batchNumber       = new BatchNumber();
                                batchNumber.Group = profile.Group;
                                ctx.BatchNumbers.InsertOnSubmit(batchNumber);
                            }

                            // Init to 0 if null
                            if (!batchNumber.Value.HasValue)
                            {
                                batchNumber.Value = 0;
                            }

                            // Set the new batch number to this email
                            email.BatchNumber = String.Format(String.IsNullOrWhiteSpace(profile.BatchNumberFormat) ? "{0:00000000}" : profile.BatchNumberFormat, ++batchNumber.Value);

                            // Final submit of updates
                            ctx.SubmitChanges();

                            // Finally, commit to the database
                            scope.Complete();
                        }

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
        }