private void MarkAsRoutingEmail(PXGraph graph, CRSMEmail message)
        {
            var cache = graph.Caches[message.GetType()];

            message.ClassID    = CRActivityClass.EmailRouting;
            message.RefNoteID  = null;
            message.BAccountID = null;
            message.ContactID  = null;
            cache.Update(message);
        }
Exemplo n.º 2
0
            private void PersistAM()
            {
                var cached = _graph.Caches[_activityMessage.GetType()].Locate(_activityMessage);

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

                _activityMessage = (CRSMEmail)cache.CreateCopy(cached);
            }
        protected override bool Process(Package package)
        {
            PXGraph      graph   = package.Graph;
            EMailAccount account = package.Account;
            CRSMEmail    message = package.Message;

            if (account != null && (
                    (account.ForbidRouting ?? false) ||
                    !(account.RouteEmployeeEmails ?? false)
                    ))
            {
                return(false);
            }

            bool?isFromInternalUser = IsFromInternalUser(graph, message);
            var  recipients         = new MailAddressList();

            if (isFromInternalUser == true)
            {
                recipients.AddRange(GetExternalRecipient(graph, message));
            }
            else if (isFromInternalUser == false)
            {
                recipients.AddRange(GetInternalRecipient(graph, message));
            }
            else
            {
                return(false);                // for those users and employees which are disabled or RouteEmails switched off
            }
            RemoveAddress(recipients, message.MailFrom);
            RemoveAddress(recipients, message.MailTo);
            RemoveAddress(recipients, message.MailCc);
            RemoveAddress(recipients, message.MailBcc);

            if (recipients.Count == 0)
            {
                return(false);
            }

            if (isFromInternalUser == true)
            {
                SendCopyMessageToOutside(graph, package.Account, message, recipients);
                MarkAsRoutingEmail(graph, message);
                MarkAsRead(graph, message);
            }
            else
            {
                SendCopyMessageToInside(graph, package.Account, message, recipients);
            }

            graph.EnsureCachePersistence(message.GetType());

            return(true);
        }
Exemplo n.º 4
0
        private void SendCopyMessageToInside(PXGraph graph, EMailAccount account, CRSMEmail message, IEnumerable <Mailbox> email)
        {
            var cache = graph.Caches[message.GetType()];
            var copy  = (CRSMEmail)cache.CreateCopy(message);

            copy.NoteID       = null;
            copy.EmailNoteID  = null;
            copy.IsIncome     = false;
            copy.ParentNoteID = message.NoteID;
            copy.MailTo       = ConcatAddresses(email);       //TODO: need add address description
            copy.MailCc       = null;
            copy.MailBcc      = null;
            Mailbox mailFrom;

            copy.MailFrom = Mailbox.TryParse(message.MailFrom, out mailFrom)
                ? new Mailbox(mailFrom.DisplayName, account.Address).ToString()
                : account.Address;
            copy.MPStatus = MailStatusListAttribute.PreProcess;
            copy.ClassID  = CRActivityClass.EmailRouting;
            new AddInfoEmailProcessor().Process(new EmailProcessEventArgs(graph, account, copy));
            copy.RefNoteID  = null;
            copy.BAccountID = null;
            copy.ContactID  = null;
            var imcUid = Guid.NewGuid();

            copy.ImcUID      = imcUid;
            copy.MessageId   = GetType().Name + "_" + imcUid.ToString().Replace("-", string.Empty);
            copy.OwnerID     = null;
            copy.WorkgroupID = null;

            copy = (CRSMEmail)cache.CreateCopy(cache.Insert(copy));

            //Update owner and reset owner if employee not found
            copy.OwnerID = message.OwnerID;
            try
            {
                copy = (CRSMEmail)cache.Update(copy);
            }
            catch (PXSetPropertyException)
            {
                copy.OwnerID = null;
                copy         = (CRSMEmail)cache.Update(copy);
            }

            copy.IsPrivate   = message.IsPrivate;
            copy.WorkgroupID = message.WorkgroupID;

            var noteFiles = PXNoteAttribute.GetFileNotes(cache, message);

            if (noteFiles != null)
            {
                PXNoteAttribute.SetFileNotes(cache, copy, noteFiles);
            }
        }
        //TODO: need optimizae DB requests
        internal static bool?IsFromInternalUser(PXGraph graph, CRSMEmail message)
        {
            var @from = EmailParser.ParseAddresses(message.MailFrom).FirstOrDefault().With(_ => _?.Address).With(_ => _?.Trim());

            PXSelect <Users,
                      Where2 <
                          Where <Users.guest, Equal <False>, Or <Users.guest, IsNull> >,
                          And <Users.email, Equal <Required <Users.email> > > > >
            .Clear(graph);

            var users = PXSelect <Users,
                                  Where2 <
                                      Where <Users.guest, Equal <False>, Or <Users.guest, IsNull> >,
                                      And <Users.email, Equal <Required <Users.email> > > > >
                        .Select(graph, @from);

            bool disabledUser = users.Count > 0 && users.RowCast <Users>().All(_ => _.State == Users.state.Disabled);

            if (disabledUser)
            {
                return(null);
            }

            PXSelectJoin <EPEmployee,
                          LeftJoin <Contact,
                                    On <Contact.contactID, Equal <EPEmployee.defContactID> > >,
                          Where <
                              EPEmployee.userID, IsNotNull,
                              And <Contact.eMail, Equal <Required <Contact.eMail> > > > >
            .Clear(graph);

            var employees = PXSelectJoin <EPEmployee,
                                          LeftJoin <Contact,
                                                    On <Contact.contactID, Equal <EPEmployee.defContactID> > >,
                                          Where <
                                              EPEmployee.userID, IsNotNull,
                                              And <Contact.eMail, Equal <Required <Contact.eMail> > > > >
                            .Select(graph, @from);

            bool disabledEmployee = employees.Count > 0 && employees.RowCast <EPEmployee>().All(_ => _.Status == BAccount.status.Inactive || !(_.RouteEmails ?? false));

            if (disabledEmployee)
            {
                return(null);
            }

            if (users.Count > 0 || employees.Count > 0)
            {
                return(true);
            }

            return(false);
        }
 private static void FillMailCC(PXGraph graph, CRSMEmail message, Guid?refNoteId)
 {
     if (refNoteId != null && refNoteId.HasValue && refNoteId != null)
     {
         foreach (Mailbox email in CRRelationsList <CRRelation.refNoteID> .GetEmailsForCC(graph, refNoteId.Value))
         {
             if (email != null)
             {
                 message.MailCc += email.ToString() + "; ";
             }
         }
     }
 }
        private void SendCopyMessage(PXGraph graph, EMailAccount account, CRSMEmail message, string email)
        {
            var cache = graph.Caches[message.GetType()];
            var copy  = (CRSMEmail)cache.CreateCopy(message);

            copy.NoteID       = null;
            copy.EmailNoteID  = null;
            copy.IsIncome     = false;
            copy.ParentNoteID = message.NoteID;
            copy.MailTo       = email;       //TODO: need add address description
            copy.MailCc       = null;
            copy.MailBcc      = null;
            copy.MPStatus     = MailStatusListAttribute.PreProcess;
            copy.ClassID      = CRActivityClass.EmailRouting;
            new AddInfoEmailProcessor().Process(new EmailProcessEventArgs(graph, account, copy));
            copy.RefNoteID  = null;
            copy.BAccountID = null;
            copy.ContactID  = null;
            copy.Pop3UID    = null;
            copy.ImapUID    = null;
            var imcUid = Guid.NewGuid();

            copy.ImcUID      = imcUid;
            copy.MessageId   = this.GetType().Name + "_" + imcUid.ToString().Replace("-", string.Empty);
            copy.OwnerID     = null;
            copy.WorkgroupID = null;

            copy = (CRSMEmail)cache.CreateCopy(cache.Insert(copy));

            //Update owner and reset owner if employee not found
            copy.OwnerID = message.OwnerID;
            try
            {
                cache.Update(copy);
            }
            catch (PXSetPropertyException)
            {
                copy.OwnerID = null;
                copy         = (CRSMEmail)cache.CreateCopy(cache.Update(copy));
            }

            copy.WorkgroupID = message.WorkgroupID;

            var noteFiles = PXNoteAttribute.GetFileNotes(cache, message);

            if (noteFiles != null)
            {
                PXNoteAttribute.SetFileNotes(cache, copy, noteFiles);
            }
            graph.EnsureCachePersistence(copy.GetType());
        }
Exemplo n.º 8
0
        private void SendCopyMessageToOutside(PXGraph graph, EMailAccount account, CRSMEmail message, IEnumerable <MailAddress> addresses)
        {
            var cache = graph.Caches[message.GetType()];
            var copy  = (CRSMEmail)cache.CreateCopy(message);

            copy.NoteID       = null;
            copy.EmailNoteID  = null;
            copy.IsIncome     = false;
            copy.ParentNoteID = message.NoteID;
            MailAddress address;

            copy.MailFrom = EmailParser.TryParse(message.MailFrom, out address)
                                ? new MailAddress(account.Address, address.DisplayName).ToString()
                                : account.Address;
            copy.MailTo   = PXDBEmailAttribute.ToString(addresses); //TODO: need add address description
            copy.MailCc   = null;
            copy.MailBcc  = null;
            copy.MPStatus = MailStatusListAttribute.PreProcess;
            copy.ClassID  = CRActivityClass.Email;
            var imcUid = Guid.NewGuid();

            copy.ImcUID       = imcUid;
            copy.MessageId    = GetType().Name + "_" + imcUid.ToString().Replace("-", string.Empty);
            copy.IsPrivate    = message.IsPrivate;
            copy.OwnerID      = null;
            copy.ParentNoteID = null;

            copy = (CRSMEmail)cache.CreateCopy(cache.Insert(copy));

            //Update owner and reset owner if employee not found
            copy.OwnerID = message.OwnerID;
            try
            {
                copy = (CRSMEmail)cache.Update(copy);
            }
            catch (PXSetPropertyException)
            {
                copy.OwnerID = null;
                copy         = (CRSMEmail)cache.Update(copy);
            }

            copy.ParentNoteID = message.NoteID;

            var noteFiles = PXNoteAttribute.GetFileNotes(cache, message);

            if (noteFiles != null)
            {
                PXNoteAttribute.SetFileNotes(cache, copy, noteFiles);
            }
        }
Exemplo n.º 9
0
        internal static bool IsOwnerEqualUser(PXGraph graph, CRSMEmail message, Guid?owner)
        {
            var @from = EmailParser.ParseAddresses(message.MailFrom).FirstOrDefault().With(_ => _.Address).With(_ => _.Trim());

            PXSelect <Users, Where <Users.email, Equal <Required <Users.email> > > > .Clear(graph);

            var usersEmail = (Users)PXSelect <Users, Where <Users.email, Equal <Required <Users.email> > > > .Select(graph, @from);

            if (usersEmail != null)
            {
                return(usersEmail.PKID == owner);
            }
            return(false);
        }
        protected override bool Process(Package package)
        {
            PXGraph      graph   = package.Graph;
            EMailAccount account = package.Account;
            CRSMEmail    message = package.Message;

            if (account != null && (account.ForbidRouting ?? false))
            {
                return(false);
            }

            var isFromInternalUser = IsFromInternalUser(graph, message);
            var recipients         = new MailAddressList();

            if (isFromInternalUser)
            {
                recipients.AddRange(GetExternalRecipient(graph, message));
            }
            else
            {
                recipients.AddRange(GetInternalRecipient(graph, message));
            }

            RemoveAddress(recipients, message.MailFrom);
            RemoveAddress(recipients, message.MailTo);
            RemoveAddress(recipients, message.MailCc);
            RemoveAddress(recipients, message.MailBcc);

            if (recipients.Count == 0)
            {
                return(false);
            }

            if (isFromInternalUser)
            {
                SendCopyMessageToOutside(graph, package.Account, message, recipients);
                MarkAsRoutingEmail(graph, message);
                MarkAsRead(graph, message);
            }
            else
            {
                SendCopyMessageToInside(graph, package.Account, message, recipients);
            }

            graph.EnsureCachePersistence(message.GetType());

            return(true);
        }
Exemplo n.º 11
0
        private static CRSMEmail GetPreviousExternalMessage(PXGraph graph, CRSMEmail message)
        {
            if (message.Ticket == null)
            {
                return(null);
            }

            CRSMEmail prevActivity = SelectActivity(graph, message.Ticket);

            while (prevActivity != null &&
                   prevActivity.ClassID == CRActivityClass.EmailRouting)
            {
                prevActivity = SelectParentActivity(graph, prevActivity.ParentNoteID);
            }
            return(prevActivity);
        }
Exemplo n.º 12
0
            private bool CreateMessage()
            {
                if (_message != null)
                {
                    _activityMessage = PXSelect <CRSMEmail,
                                                 Where <CRSMEmail.noteID, Equal <Required <SMEmail.refNoteID> > > > .Select(_graph, _message.RefNoteID);

                    return(true);
                }

                try
                {
                    if (_email.Message.From == null || _email.Message.From.Count() == 0)
                    {
                        return(false);
                    }

                    using (PXTransactionScope ts = new PXTransactionScope())
                    {
                        // Only two functions in scope so as not to create activity without email,
                        // but also to prevent repetition of processing messages with bad attachments
                        CreateActivity();
                        CreateEmail();

                        ts.Complete();
                    }

                    if (_message == null)
                    {
                        return(false);
                    }

                    _activityMessage = PXSelect <CRSMEmail,
                                                 Where <CRSMEmail.noteID, Equal <Required <SMEmail.refNoteID> > > > .Select(_graph, _message.RefNoteID);

                    AppendAttachments();
                    PersistAM();
                    return(true);
                }
                catch (Exception ex)
                {
                    PXTrace.WriteError(ex);
                    PersistException(ex);
                    return(false);
                }
            }
Exemplo n.º 13
0
        protected override bool Process(Package package)
        {
            CRSMEmail    activity = package.Message;
            EMailAccount account  = package.Account;
            PXGraph      graph    = package.Graph;

            if (activity.Ticket != null ||
                activity.ParentNoteID != null ||
                account.EmailAccountType != EmailAccountTypesAttribute.Exchange ||
                !(account.IncomingProcessing ?? false))
            {
                return(false);
            }

            if (String.IsNullOrEmpty(activity.MessageId) || String.IsNullOrEmpty(activity.MessageReference))
            {
                return(false);
            }

            foreach (String id in activity.MessageReference.Split(new string[] { "> <", ">,<" }, StringSplitOptions.RemoveEmptyEntries))
            {
                string refId = id;
                if (refId.IndexOf('<') != 0)
                {
                    refId = refId.Insert(0, "<");
                }
                if (refId.LastIndexOf('>') != refId.Length - 1)
                {
                    refId = refId.Insert(refId.Length, ">");
                }

                Boolean found = false;
                foreach (CRSMEmail parent in PXSelect <CRSMEmail, Where <CRSMEmail.messageId, Like <Required <CRSMEmail.messageId> > > > .SelectSingleBound(graph, null, refId))
                {
                    activity.Ticket = parent.ID;
                    found           = true;
                    break;
                }
                if (found)
                {
                    return(true);
                }
            }

            return(false);
        }
        private void InsertInformationIntoMessage(CRSMEmail message, IEnumerable <string> briefInfo)
        {
            if (briefInfo == null)
            {
                return;
            }

            var content = message.Body ?? string.Empty;
            var match   = _HTML_REGEX.Match(content);

            if (match.Success)
            {
                var bodyGroup = match.Groups["body"];
                var orgBody   = bodyGroup.Value;
                var sb        = new StringBuilder();
                foreach (string info in briefInfo)
                {
                    sb.AppendFormat("<i>{0}</i><br/>", info);
                }
                sb.AppendFormat("<br/>");
                var htmlText = sb.ToString();
                if (!orgBody.StartsWith(htmlText))
                {
                    var newBody = orgBody.Insert(0, htmlText);
                    content = content.Substring(0, bodyGroup.Index) + newBody + content.Substring(bodyGroup.Index + bodyGroup.Length);
                }
            }
            else
            {
                var sb = new StringBuilder();
                sb.AppendLine("***");
                foreach (string info in briefInfo)
                {
                    sb.AppendLine(info);
                }
                sb.AppendLine("***");
                sb.AppendLine();
                var plainText = sb.ToString();
                if (!content.StartsWith(plainText))
                {
                    content = content.Insert(0, plainText);
                }
            }
            message.Body = PX.Web.UI.PXRichTextConverter.NormalizeHtml(content);
        }
Exemplo n.º 15
0
        private MailAddressList GetFromExternal(PXGraph graph, CRSMEmail message)
        {
            var recipients = new MailAddressList();

            recipients.AddRange(GetOwnerAddressByNote(graph, message.RefNoteID, message.OwnerID));

            var routingMessage = GetRoutingMessage(graph, message);

            if (routingMessage != null)
            {
                RemoveAddress(recipients, routingMessage.MailFrom);
                RemoveAddress(recipients, routingMessage.MailTo);
                RemoveAddress(recipients, routingMessage.MailCc);
                RemoveAddress(recipients, routingMessage.MailBcc);
            }

            return(recipients);
        }
Exemplo n.º 16
0
        private IEnumerable <Mailbox> GetInternalRecipient(PXGraph graph, CRSMEmail message)
        {
            var previousMessage = GetPreviousInternalMessage(graph, message);

            if (previousMessage != null)
            {
                return(GetInternalMailOutside(previousMessage));
            }

            var result         = new List <Mailbox>();
            var initialMessage = GetParentMessage(graph, message);

            if (message.OwnerID != null)
            {
                var ownerAddress = GetOwnerAddress(graph, message.OwnerID);
                if (ownerAddress != null)
                {
                    result.Add(ownerAddress);
                }
            }
            if (initialMessage != null && initialMessage.OwnerID != message.OwnerID)
            {
                var ownerAddress = GetOwnerAddress(graph, initialMessage.OwnerID);
                if (ownerAddress != null)
                {
                    result.Add(ownerAddress);
                }
            }

            if (result.Count == 0)
            {
                var ownerAddress = GetOwnerAddressByNote(graph, message.RefNoteID);
                if (ownerAddress != null)
                {
                    result.Add(ownerAddress);
                }
                var parentOwnerAddress = GetOwnerAddress(graph, message.BAccountID);
                if (parentOwnerAddress != null)
                {
                    result.Add(parentOwnerAddress);
                }
            }
            return(result);
        }
        public EmailProcessEventArgs(PXGraph graph, EMailAccount account, CRSMEmail message)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            _graph   = graph;
            _account = account;
            _message = message;
        }
Exemplo n.º 18
0
        private List <MailAddress> GetExternalRecipient(PXGraph graph, CRSMEmail message)
        {
            var previousMessage = GetPreviousExternalMessage(graph, message);
            var result          = new List <MailAddress>();

            if (previousMessage != null)
            {
                result = GetExternalMailOutside(previousMessage);

                if (previousMessage.OwnerID != null)
                {
                    result.Add(GetOwnerAddress(graph, previousMessage.OwnerID));
                }
            }

            return(result);
            //TODO: need implementation
            //var source = FindSource(graph, (long)message.RefNoteID);
        }
Exemplo n.º 19
0
            private Package(PXGraph graph, EMailAccount account, CRSMEmail message)
            {
                if (graph == null)
                {
                    throw new ArgumentNullException("graph");
                }
                if (account == null)
                {
                    throw new ArgumentNullException("account");
                }
                if (message == null)
                {
                    throw new ArgumentNullException("message");
                }

                _graph   = graph;
                _account = account;
                _message = message;
            }
            private void PersistException(Exception ex)
            {
                this._graph.Clear();
                if (_activityMessage == null || _graph.Caches[_activityMessage.GetType()].GetStatus(_activityMessage) == PXEntryStatus.Inserted)
                {
                    return;
                }

                _activityMessage = PXSelect <CRSMEmail, Where <CRSMEmail.noteID, Equal <Required <CRSMEmail.noteID> > > > .SelectWindowed(_graph, 0, 1, _activityMessage.NoteID);

                if (_activityMessage != null)
                {
                    _activityMessage           = (CRSMEmail)_graph.Caches[_activityMessage.GetType()].CreateCopy(_activityMessage);
                    _activityMessage.Exception = ex.Message;
                    _activityMessage.MPStatus  = MailStatusListAttribute.Failed;
                    UpdateAM();
                    PersistAM();
                }
            }
Exemplo n.º 21
0
        public static void SendMessage(CRSMEmail message)
        {
            if (message == null || message.MailAccountID == null)
            {
                throw new PXException(ErrorMessages.EmailNotConfigured);
            }

            Tuple <EMailSyncServer, EMailSyncPolicy, PXSyncMailbox> tuple = GetConfig(message.MailAccountID.Value);
            IEmailSyncProvider prov = GetExchanger(tuple.Item1, tuple.Item2);

            try
            {
                prov.SendMessage(tuple.Item3, new[] { message });
                message.Exception = null;
            }
            catch (Exception ex)
            {
                message.Exception = ex.Message;
            }
        }
Exemplo n.º 22
0
        private IEnumerable <Mailbox> GetExternalRecipient(PXGraph graph, CRSMEmail message)
        {
            var previousMessage = GetPreviousExternalMessage(graph, message);

            if (previousMessage != null)
            {
                foreach (Mailbox box in GetExternalMailOutside(previousMessage))
                {
                    yield return(box);
                }

                if (previousMessage.OwnerID != null)
                {
                    yield return(GetOwnerAddress(graph, previousMessage.OwnerID));
                }
            }

            //TODO: need implementation
            //var source = FindSource(graph, (long)message.RefNoteID);
        }
        protected static string MergeAddressList(CRSMEmail email, string addressList, string sourceList)
        {
            if (string.IsNullOrEmpty(addressList))
            {
                return(sourceList);
            }

            AddressList result = new AddressList();
            var         index  = new HashSet <string>();

            foreach (Mailbox box in AddressList.Parse(addressList))
            {
                if (index.Contains(box.Address))
                {
                    continue;
                }
                if (email.MailTo != null && email.MailTo.Contains(box.Address))
                {
                    continue;
                }
                if (email.MailCc != null && email.MailCc.Contains(box.Address))
                {
                    continue;
                }
                if (email.MailBcc != null && email.MailBcc.Contains(box.Address))
                {
                    continue;
                }
                index.Add(box.Address);

                result.Add(box);
            }
            return(result.Count == 0
                ? sourceList
                : (string.IsNullOrEmpty(sourceList)
                    ? result.ToString()
                    : sourceList + "; " + result));
        }
Exemplo n.º 24
0
        protected override bool Process(Package package)
        {
            PXGraph   graph   = package.Graph;
            CRSMEmail message = package.Message;

            if (message.IsIncome != true)
            {
                return(false);
            }

            var isFromInternalUser = message.ClassID == CRActivityClass.EmailRouting;
            var recipients         = new MailAddressList();

            if (isFromInternalUser)
            {
                recipients.AddRange(GetFromInternal(graph, message));
            }
            else
            {
                recipients.AddRange(GetFromExternal(graph, message));
            }

            RemoveAddress(recipients, message.MailFrom);
            RemoveAddress(recipients, message.MailTo);
            RemoveAddress(recipients, message.MailCc);
            RemoveAddress(recipients, message.MailBcc);

            if (recipients.Count == 0)
            {
                return(false);
            }

            SendCopyMessageToInside(package.Graph, package.Account, message, recipients);

            graph.EnsureCachePersistence(message.GetType());

            return(true);
        }
Exemplo n.º 25
0
        protected static string MergeAddressList(CRSMEmail email, string addressList, string sourceList)
        {
            if (string.IsNullOrEmpty(addressList))
            {
                return(sourceList);
            }

            List <MailAddress> result = new List <MailAddress>();
            var index = new HashSet <string>();

            foreach (MailAddress address in EmailParser.ParseAddresses(addressList))
            {
                if (index.Contains(address.Address))
                {
                    continue;
                }
                if (email.MailTo != null && email.MailTo.Contains(address.Address, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }
                if (email.MailCc != null && email.MailCc.Contains(address.Address, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }
                if (email.MailBcc != null && email.MailBcc.Contains(address.Address, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }
                index.Add(address.Address);

                result.Add(address);
            }
            return(result.Count == 0
                ? sourceList
                : string.IsNullOrEmpty(sourceList)
                    ? PXDBEmailAttribute.ToString(result)
                    : PXDBEmailAttribute.AppendAddresses(sourceList, PXDBEmailAttribute.ToString(result)));
        }
        private Guid?GetKnownSender(PXGraph graph, CRSMEmail message)
        {
            var @from = Mailbox.Parse(message.MailFrom).With(_ => _.Address).With(_ => _.Trim());

            PXSelectJoin <EPEmployee,
                          InnerJoin <Contact, On <Contact.contactID, Equal <EPEmployee.defContactID> >,
                                     InnerJoin <Users, On <Users.pKID, Equal <EPEmployee.userID> > > >,
                          Where <Contact.eMail, Equal <Required <Contact.eMail> > > > .
            Clear(graph);

            var employeeEmail = (EPEmployee)PXSelectJoin <EPEmployee,
                                                          InnerJoin <Contact, On <Contact.contactID, Equal <EPEmployee.defContactID> >,
                                                                     InnerJoin <Users, On <Users.pKID, Equal <EPEmployee.userID> > > >,
                                                          Where <Contact.eMail, Equal <Required <Contact.eMail> > > > .
                                Select(graph, @from);

            if (employeeEmail != null)
            {
                return(employeeEmail.UserID);
            }

            return(null);
        }
            private bool CreateMessage()
            {
                if (_message != null)
                {
                    _activityMessage = PXSelect <CRSMEmail,
                                                 Where <CRSMEmail.noteID, Equal <Required <SMEmail.refNoteID> > > > .Select(_graph, _message.RefNoteID);

                    return(true);
                }

                try
                {
                    if (_email.Message.From == null || _email.Message.From.Count() == 0)
                    {
                        return(false);
                    }
                    CreateActivity();
                    CreateEmail();

                    if (_message == null)
                    {
                        return(false);
                    }

                    _activityMessage = PXSelect <CRSMEmail,
                                                 Where <CRSMEmail.noteID, Equal <Required <SMEmail.refNoteID> > > > .Select(_graph, _message.RefNoteID);

                    AppendAttachments();
                    PersistAM();
                    return(true);
                }
                catch (Exception ex)
                {
                    PersistException(ex);
                    return(false);
                }
            }
        protected override bool Process(Package package)
        {
            CRSMEmail    activity = package.Message;
            EMailAccount account  = package.Account;
            PXGraph      graph    = package.Graph;

            if (activity.Ticket != null ||
                activity.ParentNoteID != null ||
                account.EmailAccountType != EmailAccountTypesAttribute.Exchange ||
                !(account.IncomingProcessing ?? false))
            {
                return(false);
            }

            if (String.IsNullOrEmpty(activity.MessageId) || String.IsNullOrEmpty(activity.MessageReference))
            {
                return(false);
            }

            foreach (String id in activity.MessageReference.Split(' '))
            {
                Boolean found = false;
                foreach (CRSMEmail parent in PXSelect <CRSMEmail, Where <CRSMEmail.messageId, Like <Required <CRSMEmail.messageId> > > > .SelectSingleBound(graph, null, id))
                {
                    activity.Ticket = parent.ID;
                    found           = true;
                    break;
                }
                if (found)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 29
0
        private void ProcessEvent(EmailEventProcess graph, SendGridEvent e)
        {
            if (!String.IsNullOrEmpty(e.MessageID) && !String.IsNullOrEmpty(e.SendGridMessageID))
            {
                //Create link between internal Message ID and SendGrid MessageID for future events that will not include our own Message ID.
                VXEmailID association = graph.RemoteMessageIds.Select(e.SendGridMessageID);
                if (association == null)
                {
                    association = new VXEmailID();
                    association.RemoteMessageID   = e.SendGridMessageID;
                    association.InternalMessageID = e.MessageID;
                    graph.RemoteMessageIds.Insert(association);
                    graph.Actions.PressSave();
                }
            }

            //Locate Acumatica e-mail
            CRSMEmail email = (CRSMEmail)graph.EmailByRemoteId.Select(e.SendGridMessageID);

            if (email == null)
            {
                PXTrace.WriteError("VX.MailTrack No e-mail message found in Acumatica for SendGrid MessageID " + e.SendGridMessageID);
                return;
            }

            //Insert event; duplicates are possible as per the SendGrid documentation, but since EventID is a key we will simply discard it when inserting
            var emailEvent = new VXEMailEvent();

            emailEvent.EventID   = e.EventID;
            emailEvent.EventDate = UnixTimeStampToDateTime(e.Timestamp);
            emailEvent.NoteID    = email.NoteID;
            emailEvent.EMail     = e.Email;

            var pushMessage = new PushHelper.PushMessage();

            pushMessage.NoteID    = email.NoteID;
            pushMessage.RefNoteID = email.RefNoteID;
            pushMessage.Body      = email.Subject;

            switch (e.EventType.ToLower())
            {
            case "dropped":
                pushMessage.Title      = $"Message Dropped {e.Email}";
                emailEvent.EventType   = MailEventType.Dropped;
                emailEvent.Description = e.Reason;
                break;

            case "delivered":
                pushMessage.Title    = $"Message Delivered to {e.Email}";
                emailEvent.EventType = MailEventType.Delivered;
                break;

            case "bounce":
                pushMessage.Title      = $"Message Bounced {e.Email}";
                emailEvent.EventType   = MailEventType.Bounce;
                emailEvent.Description = e.Reason;
                break;

            case "open":
                pushMessage.Title      = $"Message Opened by {e.Email}";
                emailEvent.EventType   = MailEventType.Open;
                emailEvent.Description = e.UserAgent;
                break;

            case "click":
                pushMessage.Title      = $"Link Clicked by {e.Email}";
                pushMessage.Body      += $" ({ e.Url})";
                emailEvent.EventType   = MailEventType.Click;
                emailEvent.Description = e.Url;
                break;

            default:
                emailEvent.EventType   = MailEventType.Unknown;
                emailEvent.Description = "Unhandled event type: " + e.EventType;
                break;
            }

            graph.Events.Insert(emailEvent);
            graph.Actions.PressSave();

            //Notifications are sent to the user who created e-mail message or who is the current owner (OwnerID will be setup for e-mails that are generated by e-mail processing)
            foreach (VXUserPushNotification pushNotification in graph.PushNotifications.Select(email.CreatedByID, email.OwnerID))
            {
                //Just fire and forget
                var task = PushHelper.SendPushNotificationAsync(pushNotification, pushMessage);
            }
        }
 private static void FillMailAccount(CRSMEmail message, int MailAccountID)
 {
     message.MailAccountID = MailAccountID;
 }