// // Class: BSMDPopClient // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 6/15/2015 8:43:41 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Collections.Generic; using System.IO; using OpenPop.Pop3; using OpenPop.Mime; using log4net; using System.Security.Cryptography.X509Certificates; using System.Net.Security; namespace bsmd.email { public class BSMDPopClient : IDisposable { private Pop3Client pop3Client; private bool _connected = false; private ILog _log = LogManager.GetLogger(typeof(BSMDPopClient)); private int currentMail = 1; public BSMDPopClient() { try { this.pop3Client = new Pop3Client(); this.pop3Client.Connect(Properties.Settings.Default.POP3Server, Properties.Settings.Default.POP3Port, true, 60000, 60000, new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate)); this.pop3Client.Authenticate(Properties.Settings.Default.POP3User, Properties.Settings.Default.POP3Password); this._connected = true; } catch (Exception ex) { _log.ErrorFormat("Error connecting to POP3 Server: {0}", ex.Message); _log.DebugFormat("User:{0} Pw:{1}", Properties.Settings.Default.POP3User, Properties.Settings.Default.POP3Password); } } /// /// Diese Funktion validiert *jedes* Zertifikat und ist eigentlich scheiße. Das geht nur so im abgeschlossenen BSMD Netz! /// public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } public bool IsConnected { get { return this._connected; } } public bool ReceiveSingleMail(out string attachmentLocalFile, out string messageId, out string sender) { attachmentLocalFile = null; messageId = null; sender = null; if (!_connected) return false; int messageCount = this.pop3Client.GetMessageCount(); if (messageCount > (this.currentMail - 1)) { Message mailMessage = this.pop3Client.GetMessage(this.currentMail); if ((mailMessage.Headers.Sender != null) && mailMessage.Headers.Sender.HasValidMailAddress) sender = mailMessage.Headers.Sender.MailAddress.Address; if ((sender == null) && (mailMessage.Headers.From != null) && mailMessage.Headers.From.HasValidMailAddress) sender = mailMessage.Headers.From.MailAddress.Address; messageId = mailMessage.Headers.MessageId; foreach(MessagePart part in mailMessage.FindAllAttachments()) { _log.DebugFormat("found attachment named {0}, ContentType {1}", part.FileName, part.ContentType); if (part.FileName.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase)) { attachmentLocalFile = Path.Combine(Properties.Settings.Default.ArchiveFolder, part.FileName); part.Save(new FileInfo(attachmentLocalFile)); } } this.currentMail++; // advance message pointer return true; } else { _log.Debug("no new mail on server"); return false; } } /// /// deletes message referenced by messageId /// /// true if successful public bool DeleteMessageByMessageId(string messageId) { if (!_connected) return false; // Get the number of messages on the POP3 server int messageCount = this.pop3Client.GetMessageCount(); // Run trough each of these messages and download the headers for (int messageItem = messageCount; messageItem > 0; messageItem--) { // If the Message ID of the current message is the same as the parameter given, delete that message if (this.pop3Client.GetMessageHeaders(messageItem).MessageId.Equals(messageId)) { // Delete this.pop3Client.DeleteMessage(messageItem); return true; } } // We did not find any message with the given messageId, report this back return false; } public void Dispose() { if (this.pop3Client != null) { if(this.pop3Client.Connected) this.pop3Client.Disconnect(); this.pop3Client.Dispose(); } } } }