Esempio n. 1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            using (var ctx = new EmailImportDataContext())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MailboxProfile));

                foreach (var mailbox in ctx.Mailboxes)
                {
                    using (TextReader reader = new StringReader(mailbox.ProfileObject))
                    {
                        var profile = (MailboxProfile)serializer.Deserialize(reader);

                        if (profile.Enabled)
                        {
                            profiles.Add(profile);
                        }
                    }
                }

                var setting = ctx.Settings.SingleOrDefault(s => s.Name == "DefaultStoragePath");
                defaultStoragePath = (setting == null) ? null : setting.Value;

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

            comboBoxProfile.Items.AddRange(profiles.ToArray());
        }
Esempio n. 2
0
        private Boolean IsDuplicate(EmailImportDataContext ctx, Guid guid, MailMessage message)
        {
            var subject = ExistsCCNumber(message.Subject) ? MaskCCNumbers(message.Subject, '#') : message.Subject;

            return(ctx.Emails.Any(e => Object.Equals(e.MailboxGUID, guid) &&
                                  Object.Equals(e.MessageID, message.MessageId) &&
                                  Object.Equals(e.From, message.From.GetAddressOrDisplayName()) &&
                                  Object.Equals(e.DateSent, message.DateSent()) &&
                                  Object.Equals(e.DateReceived, message.DateReceived()) &&
                                  Object.Equals(e.Subject, subject)));
        }
Esempio n. 3
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);
                    }
                }
        }