Exemple #1
0
        private void ChekForNewMail(object sender, ElapsedEventArgs e)
        {
            lock (_lockObj)
            {
                Connect(_imapConfig);

                IEnumerable <uint> uids = _client.Search(SearchCondition.Seen());
                _log.Info("New mails received: " + uids.Count());

                if (NewMailArrived != null)
                {
                    foreach (var uid in uids)
                    {
                        var message = _client.GetMessage(uid);
                        NewMailArrived(message, uid);
                    }
                }

                Disconnect();
            }
        }
        public bool TryGetMostRecentUnreadEmailBodyForAddressAndThenSetAsRead(string emailAddress, out string body)
        {
            // Notethat if emailAddress is empty then all emails are examined, regardless if they were
            // to address+1@gmail or [email protected] etc.

            var messageIds = _imapClient.Search(SearchCondition.To(emailAddress).And(SearchCondition.Unseen()));
            var messageId  = messageIds.OrderByDescending(id => id).FirstOrDefault();

            if (messageId <= 0)
            {
                body = null;
                return(false);
            }

            var message = _imapClient.GetMessage(messageId, false, null);

            body = message.Body;

            _imapClient.SetMessageFlags(messageId, null, new MessageFlag[] { MessageFlag.Seen });

            return(true);
        }