using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.IO; using System.ServiceProcess; using System.Text; using System.Timers; using System.Threading.Tasks; using OpenPop; using log4net; using bsmd.database; namespace bsmd.EMailReceiveService { /// /// Simpler Windows-Service zum Abruf von NSW Antworten via E-mail. Ich verwende OpenPop.NET Library (via NuGet) /// http://hpop.sourceforge.net/ /// /// public partial class EmailReceiveService : ServiceBase { private Timer _timer; private object _timerlock = new object(); private bool processRunning = false; private ILog _log = LogManager.GetLogger(typeof(EmailReceiveService)); public EmailReceiveService() { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); InitializeComponent(); } protected override void OnStart(string[] args) { this.EventLog.Source = this.ServiceName; this.EventLog.Log = "Application"; this.EventLog.BeginInit(); if (!EventLog.SourceExists(this.EventLog.Source, this.EventLog.Log)) EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log); this.EventLog.EndInit(); this.Init(args); this.EventLog.WriteEntry("NSW EMail Receive Service started.", EventLogEntryType.Information); } protected override void OnStop() { _log.Info("EmailReceiveService stopped"); } public void Init(string[] args) { this._timer = new Timer(); this._timer.Interval = 600000; // 10 Min, TODO: Settings this._timer.Elapsed += _timer_Elapsed; this._timer.Enabled = true; if (Debugger.IsAttached) this._timer_Elapsed(null, null); } void _timer_Elapsed(object sender, ElapsedEventArgs e) { lock (this._timerlock) { if (this.processRunning) return; else this.processRunning = true; } if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString)) { try { // dbh E-Mail aus Postfach abrufen using (OpenPop.Pop3.Pop3Client client = new OpenPop.Pop3.Pop3Client()) { client.Connect(Properties.Settings.Default.Host, Properties.Settings.Default.Port, true); client.Authenticate(Properties.Settings.Default.User, Properties.Settings.Default.Password); int messageNum = client.GetMessageCount(); _log.DebugFormat("Mail Server has {0} messages", messageNum); if (messageNum > 0) { // We want to download all messages List allMessages = new List(messageNum); // Messages are numbered in the interval: [1, messageCount] // Ergo: message numbers are 1-based. // Most servers give the latest message the highest number for (int i = 1; i <= messageNum; i++) { allMessages.Add(client.GetMessage(i)); } for (int i = 0; i < allMessages.Count; i++) { // XML Anhang extrahieren und in einen string schreiben OpenPop.Mime.Message message = allMessages[i]; List messageParts = message.FindAllAttachments(); for (int partIndex = 0; partIndex < messageParts.Count; partIndex++) { OpenPop.Mime.MessagePart part = messageParts[partIndex]; _log.DebugFormat("{0} Encoding: {1}", partIndex, part.ContentTransferEncoding); switch (part.ContentTransferEncoding) { case OpenPop.Mime.Header.ContentTransferEncoding.Base64: // decode base64 body break; default: break; } } // E-Mail entfernen // client.DeleteMessage(i+1); } } } } catch (Exception ex) { _log.ErrorFormat("Exception occurred: {0}", ex.ToString()); } } else { this.EventLog.WriteEntry("FormService stopped: DB connection failed", EventLogEntryType.Error); this.Stop(); } lock (this._timerlock) { this.processRunning = false; } } } }