Exemplo n.º 1
0
        /// <summary>
        /// Archive this email item to CRM.
        /// </summary>
        /// <param name="olItem">The email item to archive.</param>
        /// <param name="reason">The reason it is being archived.</param>
        /// <returns>A result object indicating success or failure.</returns>
        public static ArchiveResult Archive(this Outlook.MailItem olItem, EmailArchiveReason reason, string excludedEmails = "")
        {
            ArchiveResult result;

            Outlook.UserProperty olProperty = olItem.UserProperties[CrmIdPropertyName];

            if (olProperty == null)
            {
                result = olItem.AsArchiveable(reason).Save(excludedEmails);

                if (result.IsSuccess)
                {
                    olItem.Categories = string.IsNullOrEmpty(olItem.Categories) ?
                                        SuiteCRMCategoryName :
                                        $"{olItem.Categories},{SuiteCRMCategoryName}";
                    olItem.EnsureProperty(CrmIdPropertyName, result.EmailId);
                }
            }
            else
            {
                result = ArchiveResult.Success(olProperty.Value, new[] { new AlreadyArchivedException(olItem) });
            }

            return(result);
        }
        /// <summary>
        /// Archive this email item to CRM.
        /// </summary>
        /// <param name="olItem">The email item to archive.</param>
        /// <param name="reason">The reason it is being archived.</param>
        /// <param name="moduleKeys">Keys (standardised names) of modules to search.</param>
        /// <param name="excludedEmails">email address(es) which should not be linked.</param>
        /// <returns>A result object indicating success or failure.</returns>
        public static ArchiveResult Archive(this Outlook.MailItem olItem, EmailArchiveReason reason, IEnumerable <CrmEntity> moduleKeys, string excludedEmails = "")
        {
            ArchiveResult result = olItem.AsArchiveable(reason).Save(moduleKeys, excludedEmails);

            if (result.IsSuccess)
            {
                try
                {
                    if (string.IsNullOrEmpty(olItem.Categories))
                    {
                        olItem.Categories = SuiteCRMCategoryName;
                    }
                    else if (olItem.Categories.IndexOf(SuiteCRMCategoryName) == -1)
                    {
                        olItem.Categories = $"{olItem.Categories},{SuiteCRMCategoryName}";
                    }

                    olItem.EnsureProperty(CrmIdPropertyName, result.EmailId);
                }
                catch (COMException cex)
                {
                    ErrorHandler.Handle("Could not set property while archiving email", cex);
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// What date/time should be assigned to the archived email?
        /// </summary>
        /// <param name="olItem">The email to be archived.</param>
        /// <param name="reason">The reason the email is being archived.</param>
        /// <returns>An appropriate date time.</returns>
        public static DateTime ArchiveTime(this Outlook.MailItem olItem, EmailArchiveReason reason)
        {
            DateTime result;
            var      now = DateTime.UtcNow;

            switch (reason)
            {
            case EmailArchiveReason.Outbound:
            case EmailArchiveReason.SendAndArchive:
                result = olItem.CreationTime;
                if (result > now)
                {
                    /* if the actual date hasn't yet been set, Outlook will
                     * nonchalantly return 1st January 4501 */
                    result = now;
                }
                break;

            case EmailArchiveReason.Inbound:
            default:
                result = olItem.SentOn;
                break;
            }

            return(result);
        }
 private void SaveMailItemIfNecessary(Outlook.MailItem olItem, EmailArchiveReason reason)
 {
     if (reason == EmailArchiveReason.SendAndArchive)
     {
         olItem.Save();
     }
 }
        /// <summary>
        /// Constuct an achiveable email object represtenting me.
        /// </summary>
        /// <param name="olItem">Me.</param>
        /// <param name="reason">The reason I should be archived.</param>
        /// <returns>An achiveable email object represtenting me.</returns>
        public static ArchiveableEmail AsArchiveable(this Outlook.MailItem olItem, EmailArchiveReason reason)
        {
            ArchiveableEmail mailArchive = new ArchiveableEmail(MailItemExtensions.SuiteCRMUserSession, MailItemExtensions.Log);

            mailArchive.From       = olItem.GetSenderSMTPAddress();
            mailArchive.To         = string.Empty;
            mailArchive.CrmEntryId = olItem.GetCRMEntryId();

            Log.Info($"MailItemExtension.AsArchiveable: serialising mail {olItem.Subject} dated {olItem.SentOn}.");

            foreach (Outlook.Recipient recipient in olItem.Recipients)
            {
                string address = recipient.GetSmtpAddress();

                switch (recipient.Type)
                {
                case (int)Outlook.OlMailRecipientType.olCC:
                    mailArchive.CC = ExtendRecipientField(mailArchive.CC, address);
                    break;

                case (int)Outlook.OlMailRecipientType.olBCC:
                    // unlikely to happen and in any case we don't store these
                    break;

                default:
                    mailArchive.To = ExtendRecipientField(mailArchive.To, address);
                    break;
                }
            }

            mailArchive.CC = olItem.CC;

            mailArchive.ClientId = olItem.EnsureEntryID();
            mailArchive.Subject  = olItem.Subject;
            mailArchive.Sent     = olItem.ArchiveTime(reason);
            mailArchive.Body     = olItem.Body;
            mailArchive.HTMLBody = Tidy(olItem.HTMLBody);
            mailArchive.Reason   = reason;
            mailArchive.Category = olItem.UserProperties[CRMCategoryPropertyName] != null ?
                                   olItem.UserProperties[CRMCategoryPropertyName].Value :
                                   string.Empty;

            if (Properties.Settings.Default.ArchiveAttachments)
            {
                foreach (Outlook.Attachment attachment in olItem.Attachments)
                {
                    mailArchive.Attachments.Add(new ArchiveableAttachment
                    {
                        DisplayName = attachment.DisplayName,
                        FileContentInBase64String = olItem.GetAttachmentAsBytes(attachment)
                    });
                }
            }

            return(mailArchive);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Constuct an achiveable email object represtenting me.
        /// </summary>
        /// <param name="olItem">Me.</param>
        /// <param name="reason">The reason I should be archived.</param>
        /// <returns>An achiveable email object represtenting me.</returns>
        public static ArchiveableEmail AsArchiveable(this Outlook.MailItem olItem, EmailArchiveReason reason)
        {
            ArchiveableEmail mailArchive = new ArchiveableEmail(MailItemExtensions.SuiteCRMUserSession, MailItemExtensions.Log);

            mailArchive.From = olItem.GetSenderSMTPAddress();
            mailArchive.To   = string.Empty;

            Log.Info($"EmailArchiving.SerialiseEmailObject: serialising mail {olItem.Subject} dated {olItem.SentOn}.");

            foreach (Outlook.Recipient recipient in olItem.Recipients)
            {
                string address = recipient.GetSmtpAddress();

                if (mailArchive.To == string.Empty)
                {
                    mailArchive.To = address;
                }
                else
                {
                    mailArchive.To += ";" + address;
                }
            }

            mailArchive.OutlookId = olItem.EnsureEntryID();
            mailArchive.Subject   = olItem.Subject;
            mailArchive.Sent      = olItem.ArchiveTime(reason);
            mailArchive.Body      = olItem.Body;
            mailArchive.HTMLBody  = Tidy(olItem.HTMLBody);
            mailArchive.Reason    = reason;
            mailArchive.Category  = olItem.UserProperties[CRMCategoryPropertyName] != null ?
                                    olItem.UserProperties[CRMCategoryPropertyName].Value :
                                    string.Empty;

            if (Properties.Settings.Default.ArchiveAttachments)
            {
                foreach (Outlook.Attachment attachment in olItem.Attachments)
                {
                    mailArchive.Attachments.Add(new ArchiveableAttachment
                    {
                        DisplayName = attachment.DisplayName,
                        FileContentInBase64String = olItem.GetAttachmentAsBytes(attachment)
                    });
                }
            }

            return(mailArchive);
        }
Exemplo n.º 7
0
        private bool ProcessNewMailItem(EmailArchiveReason archiveType, Outlook.MailItem mailItem, string excludedEmails = "")
        {
            bool result;

            if (mailItem == null)
            {
                log.Info(catalogue.GetString("New 'mail item' was null"));
                result = false;
            }
            else
            {
                this.EmailArchiver.ProcessEligibleNewMailItem(mailItem, archiveType, excludedEmails);
                result = true;
            }

            return(result);
        }
        public void ProcessEligibleNewMailItem(Outlook.MailItem olItem, EmailArchiveReason reason, string excludedEmails = "")
        {
            var parentFolder = olItem.Parent as Outlook.Folder;

            if (parentFolder == null)
            {
                Log.Debug($"NULL email folder for {reason} “{olItem.Subject}”");
                return;
            }

            if (EmailShouldBeArchived(reason, parentFolder.Store))
            {
                olItem.Archive(reason, excludedEmails);
            }
            else
            {
                Log.Debug($"NOT archiving {reason} email (folder {parentFolder.Name})");
            }
        }
        private bool EmailShouldBeArchived(EmailArchiveReason type, Outlook.Store store)
        {
            bool result;
            var  storeId = store.StoreID;

            switch (type)
            {
            case EmailArchiveReason.Inbound:
                result = Properties.Settings.Default.AccountsToArchiveInbound != null &&
                         Properties.Settings.Default.AccountsToArchiveInbound.Contains(storeId);
                break;

            case EmailArchiveReason.Outbound:
                result = Properties.Settings.Default.AccountsToArchiveOutbound != null &&
                         Properties.Settings.Default.AccountsToArchiveOutbound.Contains(storeId);
                break;

            default:
                result = false;
                break;
            }

            return(result);
        }
        public ArchiveResult ArchiveEmailWithEntityRelationships(Outlook.MailItem olItem, IEnumerable <CrmEntity> selectedCrmEntities, EmailArchiveReason reason)
        {
            var result = olItem.Archive(reason);

            if (result.IsSuccess)
            {
                var warnings = CreateEmailRelationshipsWithEntities(result.EmailId, selectedCrmEntities);
                result = ArchiveResult.Success(
                    result.EmailId,
                    result.Problems == null ?
                    warnings :
                    result.Problems.Concat(warnings));
            }

            return(result);
        }
 public ArchiveResult ArchiveEmailWithEntityRelationships(Outlook.MailItem olItem, IEnumerable <CrmEntity> selectedCrmEntities, EmailArchiveReason reason)
 {
     return(olItem.Archive(reason, selectedCrmEntities));
 }
 public static ArchiveResult Archive(this Outlook.MailItem olItem, EmailArchiveReason reason)
 {
     return(Archive(olItem, reason, EmailArchiving.defaultModuleKeys.Select(x => new CrmEntity(x, null))));
 }