private bool FetchMaessages(Imap imap) { UidCollection uids = (UidCollection)imap.Search(true, "UNSEEN", null); Log.Info("Unseen email count: {0}", uids.Count); bool handledMail = false; if (uids.Count > 0) { Log.Info("Fetching unseen messages..."); MailMessageCollection msgs = imap.DownloadEntireMessages(uids.ToString(), true); foreach (MailMessage msg in msgs) { Log.Info("Recent message index: {0} Subject: {1}", msg.IndexOnServer, msg.Subject); if (msg.Subject.Contains(this.reqMailSubject)) { Log.Info("Pacnet confirmation message has been received with subject: {0}", msg.Subject); handledMail = true; DB.ExecuteNonQuery( "SetPacnetTopUpConfirmationRequestConfirmed", CommandSpecies.StoredProcedure, new QueryParameter("DateSent", this.dateSentReq), new QueryParameter("DateConfirmed", TimeZoneInfo.ConvertTimeToUtc(msg.DateReceived)) ); } // if has matching subject } // foreach email Log.Info("Fetching unseen messages complete."); } // if return(handledMail); }
} // imp_MessageStatus /// <summary> /// Callback for BeginIdle. It'll be called after stopping idle and will download new messages /// </summary> private void IdleCallback(IAsyncResult result) { try { imp.EndIdle(); // If not exiting, i.e. just stopping idle and we should try to download new messages // Exiting means the application is being terminated and we shouldn't try downloading new messages if (!exiting) { TimerStop(); Info("Stopped idling, will download messages"); UidCollection uids = FillUids(); if (uids.Count != 0 && LastId != uids[uids.Count - 1]) { LastId = uids[uids.Count - 1]; Info("Last received mail:{0}", LastId); MailMessageCollection msgs = imp.DownloadEntireMessages(uids.ToString(), true); HandleMessages(msgs); if (imp.IsIdle) { TimerStop(); imp.StopIdle(); } // if } // if // Messages have been downloaded and idling starts again TimerStart(); imp.BeginIdle(IdleCallback, null); Info("Started idling again"); } else { // If exiting is in progress, disconnect after stopping idle imp.Disconnect(); } // if } catch (Exception ex) { Error("Error occured while in IdleCallback:{0}", ex); Info("Trying to reconnect to mailbox"); SafeDispose(); ConnectToMailboxLoop(); } // try } // IdleCallback
} // Init public void Run() { DateTime oStartTime = DateTime.UtcNow; for ( ; ;) { DateTime oNow = DateTime.UtcNow; if (oNow.Subtract(oStartTime).CompareTo(m_oTotalWaitingTime) > 0) { Error("Email did not arrive, not waiting for it any more."); break; } // if try { var imap = new Imap { // Enable SSL/TLS if necessary SslMode = MailBee.Security.SslStartupMode.OnConnect }; // Connect to IMAP server imap.Connect(m_oConf.Server, m_oConf.Port); Info("Connected to the server"); // Log into IMAP account imap.Login(m_oConf.LoginAddress, m_oConf.LoginPassword); Info("Logged into the server"); // Select Inbox folder imap.SelectFolder("Inbox"); UidCollection uids = (UidCollection)imap.Search(true, "UNSEEN", null); Debug("Unseen email count: {0}", uids.Count); if (uids.Count > 0) { Debug("Fetching unseen messages..."); MailMessageCollection msgs = imap.DownloadEntireMessages(uids.ToString(), true); bool handledMail = false; foreach (MailMessage msg in msgs) { Info("Recent message index: {0} Subject: {1}", msg.IndexOnServer, msg.Subject); if (msg.HasAttachments) { foreach (Attachment attachment in msg.Attachments) { if (Consts.AttachmentContentTypes.Contains(attachment.ContentType)) { Info("Has pdf attachment {0}", attachment.Filename); byte[] data = attachment.GetData(); ParsePacNetText parsePacNetText = new ParsePacNetText(m_oConf.LoginAddress, m_oConf.LoginPassword); parsePacNetText.ParsePdf(data); handledMail = true; } // if appropriate attachment type } // foreach attachment } // if has attachment } // foreach email if (handledMail) { PacNetBalance.SavePacNetBalanceToDb(); } Debug("Fetching unseen messages complete."); if (handledMail) { break; } } // if imap.Disconnect(); } catch (PacNetBalanceException pex) { Error("PacNetBalanceException: {0}", pex); Mailer.Mailer.SendMail(m_oConf.LoginAddress, m_oConf.LoginPassword, "PacNet Balance Report Error", pex.ToString(), "*****@*****.**"); } catch (MailBeeStreamException mex) { Error("MailBeeStreamException: {0}", mex); } catch (Exception e) { Error("Some generic Exception: {0}", e); } // try Debug("Sleeping..."); Thread.Sleep(m_oConf.MailboxReconnectionIntervalSeconds * 1000); } // for } // Run