Exemplo n.º 1
0
        public int LoadSampleMessage(
            int?folderId,
            uint?userFolderId,
            int?mailboxId,
            bool unread,
            Stream emlStream,
            bool add2Index = false)
        {
            var folder = folderId.HasValue ? (FolderType)folderId.Value : FolderType.Inbox;

            if (!MailFolder.IsIdOk(folder))
            {
                throw new ArgumentException(@"Invalid folder id", "folderId");
            }

            if (!mailboxId.HasValue)
            {
                throw new ArgumentException(@"Invalid mailbox id", "mailboxId");
            }

            var accounts = _engineFactory.AccountEngine.GetAccountInfoList().ToAccountData().ToList();

            var account = mailboxId.HasValue
                ? accounts.FirstOrDefault(a => a.MailboxId == mailboxId)
                : accounts.FirstOrDefault(a => a.IsDefault) ?? accounts.FirstOrDefault();

            if (account == null)
            {
                throw new ArgumentException("Mailbox not found");
            }

            var mbox = _engineFactory.MailboxEngine.GetMailboxData(
                new СoncreteUserMailboxExp(account.MailboxId, TenantId, Username));

            if (mbox == null)
            {
                throw new ArgumentException("no such mailbox");
            }

            var mimeMessage = MailClient.ParseMimeMessage(emlStream);

            var message = MessageEngine.Save(mbox, mimeMessage, SAMPLE_UIDL, new MailFolder(folder, ""), userFolderId,
                                             unread, Log);

            if (message == null)
            {
                return(-1);
            }

            if (!add2Index)
            {
                return(message.Id);
            }

            _engineFactory.IndexEngine.Add(message.ToMailWrapper(mbox.TenantId, new Guid(mbox.UserId)));

            return(message.Id);
        }
Exemplo n.º 2
0
        private void ClientOnGetMessage(object sender, MailClientMessageEventArgs mailClientMessageEventArgs)
        {
            var log = _log;

            Stopwatch watch = null;

            if (_tasksConfig.CollectStatistics)
            {
                watch = new Stopwatch();
                watch.Start();
            }

            var failed = false;

            var mailbox = mailClientMessageEventArgs.Mailbox;

            try
            {
                var mimeMessage = mailClientMessageEventArgs.Message;
                var uid         = mailClientMessageEventArgs.MessageUid;
                var folder      = mailClientMessageEventArgs.Folder;
                var unread      = mailClientMessageEventArgs.Unread;
                log = mailClientMessageEventArgs.Logger;

                var uidl = mailbox.Imap ? string.Format("{0}-{1}", uid, (int)folder.Folder) : uid;

                log.InfoFormat("Found message (UIDL: '{0}', MailboxId = {1}, Address = '{2}')",
                               uidl, mailbox.MailBoxId, mailbox.EMail);

                CoreContext.TenantManager.SetCurrentTenant(mailbox.TenantId);
                SecurityContext.AuthenticateMe(new Guid(mailbox.UserId));

                var message = MessageEngine.Save(mailbox, mimeMessage, uidl, folder, null, unread, log);

                if (message == null || message.Id <= 0)
                {
                    return;
                }

                log.InfoFormat("Message saved (id: {0}, From: '{1}', Subject: '{2}', Unread: {3})",
                               message.Id, message.From, message.Subject, message.IsNew);

                log.Info("DoOptionalOperations->START");

                DoOptionalOperations(message, mimeMessage, mailbox, folder, log);

                log.Info("DoOptionalOperations->END");
            }
            catch (Exception ex)
            {
                log.ErrorFormat("[ClientOnGetMessage] Exception:\r\n{0}\r\n", ex.ToString());

                failed = true;
            }
            finally
            {
                if (_tasksConfig.CollectStatistics && watch != null)
                {
                    watch.Stop();

                    LogStat(PROCESS_MESSAGE, mailbox, watch.Elapsed, failed);
                }
            }
        }