public void SetMailAsRead(DateTime dateTime)
        {
            LoadConfig();

            NotesSession notesSession = new NotesSession();
            notesSession.Initialize(Config["PasswordLotusNotesAccount"]);
            NotesDatabase notesDb = notesSession.GetDatabase(Config["HostLotusNotesServer"], Config["PathLotusNotesDatabase"], false);
            NotesDateTime lnDateTime = notesSession.CreateDateTime(dateTime.ToShortDateString());
            NotesDocument profileDocument = notesDb.GetProfileDocument("Profile");

            try
            {
                profileDocument.ReplaceItemValue("UntilTime", lnDateTime);
                profileDocument.Save(true, true, true);
            }
            catch (Exception e)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Log(LogLevel.Error, e);
            }

            ReleaseComResources(new object[] { notesDb, notesSession, profileDocument, lnDateTime });
        }
        public List<Email> GetMail(bool getAllMail = false)
        {
            LoadConfig();
            List<Email> emails = new List<Email>();

            try
            {
                NotesSession notesSession = new NotesSession();
                notesSession.Initialize(Config["PasswordLotusNotesAccount"]);
                NotesDatabase notesDb = notesSession.GetDatabase(Config["HostLotusNotesServer"], Config["PathLotusNotesDatabase"], false);
                NotesDateTime startDateMailAll = notesSession.CreateDateTime(Config["StartDateMailAll"]);
                NotesDateTime startDateMailNew = notesSession.CreateDateTime(Config["StartDateMailNew"]);
                NotesDocument profileDocument = notesDb.GetProfileDocument("Profile");

                if (!getAllMail) // If only new mail get the last time we checked email
                {
                    if (profileDocument != null && profileDocument.HasItem("UntilTime"))
                    {
                        object[] dateTime = profileDocument.GetItemValue("UntilTime");
                        if (dateTime != null && dateTime.Length > 0)
                            startDateMailNew = notesSession.CreateDateTime(Convert.ToString(dateTime[0]));
                    }
                }

                NotesDateTime startingDateTime = (getAllMail) ? startDateMailAll : startDateMailNew;
                var documentCollection = notesDb.GetModifiedDocuments(startingDateTime);
                NotesDocument lotusNotesEmail = documentCollection.GetFirstDocument();

                while (lotusNotesEmail != null)
                {
                    if (lotusNotesEmail.IsValid
                        && (!lotusNotesEmail.IsDeleted))
                    {
                        NotesItem fromNotesItem = lotusNotesEmail.GetFirstItem("From");
                        NotesItem toNotesItem = lotusNotesEmail.GetFirstItem("INetSendTo");
                        NotesItem subjectNotesItem = lotusNotesEmail.GetFirstItem("Subject");
                        NotesItem bodyNotesItem = lotusNotesEmail.GetFirstItem("Body");

                        emails.Add(new Email
                        {
                            From = fromNotesItem == null ? "N/A" : fromNotesItem.Text,
                            To = toNotesItem == null ? "N/A" : toNotesItem.Text,
                            Subject = subjectNotesItem == null ? "N/A" : subjectNotesItem.Text,
                            Body = bodyNotesItem == null ? "N/A" : bodyNotesItem.Text,
                            Date = lotusNotesEmail.Created == null ? DateTime.MinValue : Convert.ToDateTime(lotusNotesEmail.Created.ToString()),
                            Attachments = ExtractAttachments(lotusNotesEmail)
                        });

                        ReleaseComResources(new object[] { fromNotesItem, toNotesItem, subjectNotesItem, bodyNotesItem });
                        // Always last line:
                        lotusNotesEmail = documentCollection.GetNextDocument(lotusNotesEmail);
                    }
                }

                // Time-stamp most recent mail pick-up (if not getting all mail)
                if (!getAllMail)
                {
                    SetMailAsRead(profileDocument, documentCollection);
                }

                // Release Notes objects
                ReleaseComResources(new object[] { notesDb, notesSession, profileDocument, documentCollection });

            }
            catch (Exception e)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Log(LogLevel.Error, e);
            }

            return emails;
        }
        public CalendarEvent GetCurrentMeeting(string password)
        {
            NotesSession notesSession = new NotesSession();
            NotesDatabase notesDatabase;

            try
            {
                notesSession.Initialize(password);
                string MailServer = notesSession.GetEnvironmentString("MailServer", true);
                string MailFile = notesSession.GetEnvironmentString("MailFile", true);
                notesDatabase = notesSession.GetDatabase(MailServer, MailFile, false);

                NotesDateTime minStartDate = notesSession.CreateDateTime("Today");
                NotesDateTime maxEndDate = notesSession.CreateDateTime("Tomorrow");
                // Query Lotus Notes to get calendar entries in our date range.
                // To understand this SELECT, go to http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp
                // and search for the various keywords. Here is an overview:
                //   !@IsAvailable($Conflict) will exclude entries that conflicts with another.
                //     LN doesn't show conflict entries and we should ignore them.
                //   @IsAvailable(CalendarDateTime) is true if the LN document is a calendar entry
                //   @Explode splits a string based on the delimiters ",; "
                //   The operator *= is a permuted equal operator. It compares all entries on
                //     the left side to all entries on the right side. If there is at least one
                //     match, then true is returned.
                String calendarQuery = "SELECT (!@IsAvailable($Conflict) & @IsAvailable(CalendarDateTime) & (@Explode(CalendarDateTime) *= @Explode(@TextToTime(\""
                    + minStartDate.LocalTime
                    + "-" + maxEndDate.LocalTime + "\"))))";

                NotesDocumentCollection appointments = notesDatabase.Search(calendarQuery, null, 25);
                //appointments.Count
                NotesDocument Current = appointments.GetFirstDocument();
                DateTime rightNow = DateTime.Now;
                CalendarEvent closestMeeting = null;
                TimeSpan ts = new TimeSpan(2, 0, 0, 0); //set for 2 days as longest time span

                while (Current != null)
                {
                    int repeatCount = ((object[])Current.GetItemValue("StartDateTime")).Length;
                    for (int i = 0; i < repeatCount; i++)
                    {
                        var calendarEvent = GetCallendarEvent(Current, i);
                        if (calendarEvent.Starts <= rightNow && calendarEvent.Ends > rightNow)
                        {
                            //ding ding ding, winner
                            return calendarEvent;
                        }
                        TimeSpan timeToMeeting = calendarEvent.Starts.Subtract(rightNow);
                        if (timeToMeeting < ts && calendarEvent.Starts > rightNow)
                        {
                            ts = timeToMeeting;
                            closestMeeting = calendarEvent;
                        }
                    }

                    Current = appointments.GetNextDocument(Current);
                }
                return closestMeeting;

            }
            catch (Exception)
            {
                //TODO: Catch bad stuff here and provide a message
                throw;
            }
        }