// // 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.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 { #region Fields private readonly Pop3Client pop3Client; private readonly ILog _log = LogManager.GetLogger(typeof(BSMDPopClient)); private int currentMail = 1; #endregion #region Construction / Destruction 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.IsConnected = 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); } } public void Dispose() { if (this.pop3Client != null) { if (this.pop3Client.Connected) this.pop3Client.Disconnect(); this.pop3Client.Dispose(); } } #endregion #region Hack /// /// 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; } #endregion #region Properties public bool IsConnected { get; } = false; #endregion #region E-Mail receive helper / convenience funcs public bool ReceiveSingleMailText(out string messageId, out string sender, out string subject, out string body) { messageId = null; sender = null; subject = null; body = null; if (!IsConnected) return false; int messageCount = this.pop3Client.GetMessageCount(); if (messageCount > (this.currentMail - 1)) { Message mailMessage = this.pop3Client.GetMessage(this.currentMail); if (mailMessage.Headers.Sender?.HasValidMailAddress == true) sender = mailMessage.Headers.Sender.MailAddress.Address; if ((sender == null) && (mailMessage.Headers.From?.HasValidMailAddress == true)) sender = mailMessage.Headers.From.MailAddress.Address; MessagePart aPart = mailMessage.FindFirstPlainTextVersion(); body = aPart?.GetBodyAsText(); messageId = mailMessage.Headers.MessageId; subject = mailMessage.Headers.Subject; this.currentMail++; // advance message pointer return true; } else { return false; } } public bool ReceiveSingleMail(out string attachmentLocalFile, out string messageId, out string sender, out string subject) { attachmentLocalFile = null; messageId = null; sender = null; subject = null; if (!IsConnected) return false; int messageCount = this.pop3Client.GetMessageCount(); if (messageCount > (this.currentMail - 1)) { Message mailMessage = this.pop3Client.GetMessage(this.currentMail); if (mailMessage.Headers.Sender?.HasValidMailAddress == true) sender = mailMessage.Headers.Sender.MailAddress.Address; if ((sender == null) && (mailMessage.Headers.From?.HasValidMailAddress == true)) sender = mailMessage.Headers.From.MailAddress.Address; messageId = mailMessage.Headers.MessageId; subject = mailMessage.Headers.Subject; foreach(MessagePart part in mailMessage.FindAllAttachments()) { _log.InfoFormat("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)); } else if (part.FileName.EndsWith(".xlsx", 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.Info("no new mail on server"); return false; } } public string GetNameOfFirstMailEmailPDFAttachment(string messageId) { if (IsConnected) { int messageCount = this.pop3Client.GetMessageCount(); // Run through each of these messages and download the headers for (int messageIndex = messageCount; messageIndex > 0; messageIndex--) { // If the Message ID of the current message is the same as the parameter given, delete that message if (this.pop3Client.GetMessageHeaders(messageIndex).MessageId.Equals(messageId)) { // gefunden, Nachricht laden Message mailMessage = this.pop3Client.GetMessage(messageIndex); foreach (MessagePart part in mailMessage.FindAllAttachments()) { if (part.FileName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)) return part.FileName; } } } } return null; } #endregion #region delete designated email /// /// deletes message referenced by messageId /// /// true if successful public bool DeleteMessageByMessageId(string messageId) { if (!IsConnected) 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; } #endregion } }