private void UpdateMessage()
            {
                Type messageType = _message.GetType();
                var  emailCache  = _graph.Caches[messageType];

                _message = (PX.Objects.CR.EPActivity)emailCache.CreateCopy(emailCache.Update(_message));
                _graph.EnshureCachePersistance(messageType);
            }
            private void PersistMessage()
            {
                var cached = _graph.Caches[_message.GetType()].Locate(_message);

                _graph.Persist();
                _graph.SelectTimeStamp();
                var cache = _graph.Caches[_message.GetType()];

                _message = (EPActivity)cache.CreateCopy(cached);
            }
            private MailProcessor(PXGraph graph, EMailAccount account, PX.Objects.CR.EPActivity message)
                : this(graph, account)
            {
                if (message == null)
                {
                    throw new ArgumentNullException("message");
                }

                _message = message;
            }
 private void Proccess()
 {
     _graph.Accessinfo.ScreenID = "CR.30.60.15";
     PXContext.SetScreenID("CR.30.60.15");
     try
     {
         if (CreateMessage())
         {
             ProcessMessage();
         }
     }
     finally
     {
         _graph.Clear();
         _message = null;
     }
 }
            private void PersistException(Exception ex)
            {
                this._graph.Clear();
                if (_message == null || _message.TaskID < 0)
                {
                    return;
                }

                _message = PXSelect <EPActivity, Where <EPActivity.taskID, Equal <Required <EPActivity.taskID> > > > .SelectWindowed(_graph, 0, 1, _message.TaskID);

                if (_message != null)
                {
                    _message           = (EPActivity)_graph.Caches[_message.GetType()].CreateCopy(_message);
                    _message.Exception = ex.Message;
                    _message.MPStatus  = MailStatusListAttribute.Failed;
                    UpdateMessage();
                    PersistMessage();
                }
            }
            private void CreateActivity()
            {
                PXCache cache = _graph.Caches[typeof(PX.Objects.CR.EPActivity)];

                _message               = (EPActivity)cache.CreateCopy(cache.Insert());
                _message.ClassID       = CRActivityClass.Email;
                _message.Type          = null;
                _message.IsBillable    = false;
                _message.MailAccountID = _account.EmailAccountID;

                var mimeMessage = _email.Message;

                _message.MailFrom  = mimeMessage.From.With(_ => _.ToString()) ?? string.Empty;
                _message.MailReply = mimeMessage.ReplyTo.With(_ => _.ToString()) ?? string.Empty;

                _message.MailTo  = mimeMessage.To.With(_ => _.ToString()) ?? string.Empty;
                _message.MailCc  = mimeMessage.Cc.With(_ => _.ToString()) ?? string.Empty;
                _message.MailBcc = mimeMessage.Bcc.With(_ => _.ToString()) ?? string.Empty;
                _message.Subject = mimeMessage.Subject.With(_ => _.ToString()) ?? " ";

                if (!string.IsNullOrEmpty(_email.UID))
                {
                    if (_account.IncomingHostProtocol == IncomingMailProtocolsAttribute._IMAP)
                    {
                        _message.ImapUID = int.Parse(_email.UID);
                    }
                    else
                    {
                        _message.Pop3UID = _email.UID;
                    }
                }
                _message.StartDate  = _email.Message.Date;
                _message.IsIncome   = true;
                _message.MessageId  = mimeMessage.MessageID;
                _message.MPStatus   = MailStatusListAttribute.PreProcess;              //TODO: need move into Automation steps
                _message.IsBillable = false;

                var body = mimeMessage.BodyHtmlText;

                if (body == null)
                {
                    if (mimeMessage.BodyText != null)
                    {
                        body = PX.Common.Tools.ConvertSimpleTextToHtml(mimeMessage.BodyText);
                    }
                }
                else
                {
                    body = mimeMessage.BodyHtmlText.
                           Replace(Environment.NewLine, " ").
                           Replace("<spanstyle", "<span style");                      //NOTE: ms outlook style conflicts with our system style

                    Regex baseTag = new Regex(@"<base(.|\n)*?>");
                    body = baseTag.Replace(body, String.Empty);
                }
                _message.Body = body ?? string.Empty;

                string clearedSubject;

                var ticket = DecodeTicket(_message.Subject, out clearedSubject);

                if (ticket != null)
                {
                    _message.Ticket = CorrectTicket(ticket);
                }

                _message.Subject = clearedSubject;

                UpdateMessage();
            }
            public static void Proccess(PXGraph graph, EMailAccount account, PX.Objects.CR.EPActivity email)
            {
                var obj = new MailProcessor(graph, account, email);

                obj.Proccess();
            }