Mailkit mit IMAP Implementierung (6.4.1) funktioniert leider nicht mit dem neuen Mailserver

This commit is contained in:
Daniel Schick 2021-06-23 19:19:48 +00:00
parent 8151ceaa5b
commit a3083680e6
11 changed files with 227 additions and 81 deletions

Binary file not shown.

View File

@ -35,9 +35,6 @@
</startup>
<applicationSettings>
<bsmd.ExcelReadService.Properties.Settings>
<setting name="ConnectionString" serializeAs="String">
<value>Data Source=(localdb)\Projects;Initial Catalog=nsw;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False</value>
</setting>
<setting name="SleepSeconds" serializeAs="String">
<value>300</value>
</setting>
@ -50,7 +47,7 @@
<setting name="ValidSender" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>report@bsmd.de</string>
<string>hsok@gmx.de</string>
<string>bald@puls200.de</string>
@ -65,7 +62,7 @@
<setting name="ConfirmationDE" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>E:\svnlager\BSMD\nsw\Deutschland\BSMD-Formblatt.xlsx</string>
</ArrayOfString>
</value>
@ -73,12 +70,15 @@
<setting name="ConfirmationDK" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>E:\svnlager\BSMD\nsw\Dänemark\NSW-DK-Excel-Arrival.xlsx</string>
<string>E:\svnlager\BSMD\nsw\Dänemark\NSW-DK-Excel-Departure.xlsx</string>
</ArrayOfString>
</value>
</setting>
<setting name="ConnectionString" serializeAs="String">
<value>Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=E:\DATA\DB\NSW.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False</value>
</setting>
</bsmd.ExcelReadService.Properties.Settings>
<bsmd.email.Properties.Settings>
<setting name="POP3User" serializeAs="String">

View File

@ -71,6 +71,9 @@ namespace bsmd.ExcelReadService
Dictionary<Guid, ReportingParty> reportingPartyDict = DBManager.Instance.GetReportingPartyDict();
// BSMDPopClient.PrintCapabilities();
// BSMDPopClient.PrintSslConnectionInfo();
using (BSMDPopClient bsmdPopClient = new BSMDPopClient())
{
if(bsmdPopClient.IsConnected)

View File

@ -12,7 +12,7 @@ namespace bsmd.ExcelReadService.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
@ -23,15 +23,6 @@ namespace bsmd.ExcelReadService.Properties {
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("replace me!")]
public string ConnectionString {
get {
return ((string)(this["ConnectionString"]));
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("300")]
@ -108,5 +99,16 @@ namespace bsmd.ExcelReadService.Properties {
return ((global::System.Collections.Specialized.StringCollection)(this["ConfirmationDK"]));
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=E:\\DATA\\DB\\NSW.MDF;Integrated " +
"Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Appl" +
"icationIntent=ReadWrite;MultiSubnetFailover=False")]
public string ConnectionString {
get {
return ((string)(this["ConnectionString"]));
}
}
}
}

View File

@ -2,9 +2,6 @@
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="bsmd.ExcelReadService.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ConnectionString" Type="System.String" Scope="Application">
<Value Profile="(Default)">replace me!</Value>
</Setting>
<Setting Name="SleepSeconds" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">300</Value>
</Setting>
@ -40,5 +37,8 @@
&lt;string&gt;E:\svnlager\BSMD\nsw\Dänemark\NSW-DK-Excel-Departure.xlsx&lt;/string&gt;
&lt;/ArrayOfString&gt;</Value>
</Setting>
<Setting Name="ConnectionString" Type="System.String" Scope="Application">
<Value Profile="(Default)">Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=E:\DATA\DB\NSW.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -2,6 +2,6 @@
[assembly: AssemblyCompany("schick Informatik")]
[assembly: AssemblyProduct("BSMD NSW interface")]
[assembly: AssemblyInformationalVersion("6.4.0")]
[assembly: AssemblyInformationalVersion("6.4.1")]
[assembly: AssemblyCopyright("Copyright © 2014-2021 schick Informatik")]
[assembly: AssemblyTrademark("")]

View File

@ -1,4 +1,4 @@
using System.Reflection;
[assembly: AssemblyVersion("6.4.0.*")]
[assembly: AssemblyVersion("6.4.1.*")]

View File

@ -21,8 +21,8 @@ namespace bsmd.email
{
public class BSMDMail : IDisposable
{
private SmtpClient client;
private static ILog log = LogManager.GetLogger(typeof(BSMDMail));
private readonly SmtpClient client;
private static readonly ILog log = LogManager.GetLogger(typeof(BSMDMail));
internal BSMDMail()
{

View File

@ -9,12 +9,19 @@
using System;
using System.IO;
using OpenPop.Pop3;
using OpenPop.Mime;
using log4net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using MimeKit;
using MailKit;
using MailKit.Security;
using MailKit.Net.Pop3;
using MailKit.Net.Imap;
using MailKit.Net.Smtp;
using EmailValidation;
namespace bsmd.email
{
public class BSMDPopClient : IDisposable
@ -24,7 +31,7 @@ namespace bsmd.email
private readonly Pop3Client pop3Client;
private readonly ILog _log = LogManager.GetLogger(typeof(BSMDPopClient));
private int currentMail = 1;
private int currentMail = 0;
#endregion
@ -35,21 +42,15 @@ namespace bsmd.email
try
{
this.pop3Client = new Pop3Client();
//bool useSSL = false; // abhängig vom PORT setzen!!!
//if (Properties.Settings.Default.POP3Port != 110)
bool useSSL = true;
this.pop3Client.Connect(Properties.Settings.Default.POP3Server, Properties.Settings.Default.POP3Port, useSSL,
60000, 60000, new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate));
// this.pop3Client.Connect(Properties.Settings.Default.POP3Server, Properties.Settings.Default.POP3Port, useSSL);
this.pop3Client.ServerCertificateValidationCallback = ValidateServerCertificate;
bool useSSL = true;
this.pop3Client.Connect(Properties.Settings.Default.POP3Server, Properties.Settings.Default.POP3Port, useSSL);
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.ErrorFormat("Error connecting to POP3 Server {0}: {1}", Properties.Settings.Default.POP3Server, ex.Message);
_log.DebugFormat("User:{0} Pw:{1}", Properties.Settings.Default.POP3User, Properties.Settings.Default.POP3Password);
}
}
@ -58,8 +59,8 @@ namespace bsmd.email
{
if (this.pop3Client != null)
{
if (this.pop3Client.Connected)
this.pop3Client.Disconnect();
if (this.pop3Client.IsConnected)
this.pop3Client.Disconnect(true);
this.pop3Client.Dispose();
}
}
@ -96,19 +97,26 @@ namespace bsmd.email
if (!IsConnected) return false;
int messageCount = this.pop3Client.GetMessageCount();
if (messageCount > (this.currentMail - 1))
if (messageCount > this.currentMail)
{
Message mailMessage = this.pop3Client.GetMessage(this.currentMail);
MimeMessage mailMessage = this.pop3Client.GetMessage(this.currentMail);
if((mailMessage.Sender != null) && EmailValidator.Validate(mailMessage.Sender.Address))
sender = mailMessage.Sender.Address;
if ((sender == null) && mailMessage.From.Count > 0)
{
foreach (MailboxAddress ma in mailMessage.From.Mailboxes)
{
if(EmailValidator.Validate(ma.Address))
{
sender = ma.Address;
break;
}
}
}
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;
body = mailMessage.TextBody;
messageId = mailMessage.MessageId;
subject = mailMessage.Subject;
this.currentMail++; // advance message pointer
return true;
@ -129,32 +137,63 @@ namespace bsmd.email
if (!IsConnected) return false;
int messageCount = this.pop3Client.GetMessageCount();
if (messageCount > (this.currentMail - 1))
if (messageCount > this.currentMail)
{
Message mailMessage = this.pop3Client.GetMessage(this.currentMail);
MimeMessage 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())
if((mailMessage.Sender != null) && EmailValidator.Validate(mailMessage.Sender.Address))
sender = mailMessage.Sender.Address;
if ((sender == null) && (mailMessage.From.Count > 0))
{
_log.InfoFormat("found attachment named {0}, ContentType {1}", part.FileName, part.ContentType);
if (part.FileName.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase))
foreach (MailboxAddress ma in mailMessage.From.Mailboxes)
{
attachmentLocalFile = Path.Combine(Properties.Settings.Default.ArchiveFolder, part.FileName);
part.Save(new FileInfo(attachmentLocalFile));
if (EmailValidator.Validate(ma.Address))
{
sender = ma.Address;
break;
}
}
else if (part.FileName.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase))
}
messageId = mailMessage.MessageId;
subject = mailMessage.Subject;
foreach (MimeEntity attachment in mailMessage.Attachments)
{
if (attachment is MessagePart rfc822)
{
attachmentLocalFile = Path.Combine(Properties.Settings.Default.ArchiveFolder, part.FileName);
part.Save(new FileInfo(attachmentLocalFile));
string filename = attachment.ContentDisposition?.FileName;
if(filename == null)
{
_log.InfoFormat("attachment {0} has no filename", attachment.ContentId);
continue;
}
_log.InfoFormat("found attachment named {0}, ContentType {1}", filename, attachment.ContentType);
if (filename.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase) ||
filename.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase))
{
attachmentLocalFile = Path.Combine(Properties.Settings.Default.ArchiveFolder, filename);
using (var stream = File.Create(attachmentLocalFile))
rfc822.Message.WriteTo(stream);
}
}
else
{
var part = (MimePart)attachment;
var filename = part.FileName;
if (filename != null)
{
if (filename.EndsWith(".xls", StringComparison.InvariantCultureIgnoreCase) ||
filename.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase))
{
attachmentLocalFile = Path.Combine(Properties.Settings.Default.ArchiveFolder, filename);
using (var stream = File.Create(attachmentLocalFile))
part.Content.DecodeTo(stream);
}
}
}
}
}
this.currentMail++; // advance message pointer
@ -176,15 +215,19 @@ namespace bsmd.email
// 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))
// If the Message ID of the current message is the same as the parameter given, delete that message
if (this.pop3Client.GetMessage(messageIndex).MessageId.Equals(messageId))
{
// gefunden, Nachricht laden
Message mailMessage = this.pop3Client.GetMessage(messageIndex);
foreach (MessagePart part in mailMessage.FindAllAttachments())
MimeMessage mailMessage = this.pop3Client.GetMessage(messageIndex);
foreach (MimeEntity attachment in mailMessage.Attachments)
{
if (part.FileName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase))
return part.FileName;
if(attachment is MessagePart)
{
var filename = attachment.ContentDisposition?.FileName;
if ((filename != null) && filename.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
return attachment.ContentDisposition?.FileName;
}
}
}
}
@ -208,10 +251,10 @@ namespace bsmd.email
int messageCount = this.pop3Client.GetMessageCount();
// Run trough each of these messages and download the headers
for (int messageItem = messageCount; messageItem > 0; messageItem--)
for (int messageItem = (messageCount - 1); 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))
if (this.pop3Client.GetMessage(messageItem).MessageId.Equals(messageId))
{
// Delete
this.pop3Client.DeleteMessage(messageItem);
@ -224,6 +267,87 @@ namespace bsmd.email
}
#endregion
#region MailKit helper / info / logging funcs
#region SslConnectionInformation
public static void PrintSslConnectionInfo()
{
using (var client = new SmtpClient())
{
string host = Properties.Settings.Default.SMTPServer;
client.Connect(host, 587, SecureSocketOptions.Auto);
Console.WriteLine($"Negotiated the following SSL options with {host}:");
Console.WriteLine($" Protocol Version: {client.SslProtocol}");
Console.WriteLine($" Cipher Algorithm: {client.SslCipherAlgorithm}");
Console.WriteLine($" Cipher Strength: {client.SslCipherStrength}");
Console.WriteLine($" Hash Algorithm: {client.SslHashAlgorithm}");
Console.WriteLine($" Hash Strength: {client.SslHashStrength}");
Console.WriteLine($" Key-Exchange Algorithm: {client.SslKeyExchangeAlgorithm}");
Console.WriteLine($" Key-Exchange Strength: {client.SslKeyExchangeStrength}");
// Example Log:
//
// Negotiated the following SSL options with pop.gmail.com:
// Protocol Version: Tls12
// Cipher Algorithm: Aes128
// Cipher Strength: 128
// Hash Algorithm: Sha256
// Hash Strength: 0
// Key-Exchange Algorithm: 44550
// Key-Exchange Strength: 255
client.Disconnect(true);
}
}
public static void PrintCapabilities()
{
using (var client = new Pop3Client())
{
client.Connect(Properties.Settings.Default.POP3Server, Properties.Settings.Default.POP3Port, SecureSocketOptions.SslOnConnect);
if (client.Capabilities.HasFlag(Pop3Capabilities.Sasl))
{
var mechanisms = string.Join(", ", client.AuthenticationMechanisms);
Console.WriteLine("The POP3 server supports the following SASL mechanisms: {0}", mechanisms);
}
client.Authenticate(Properties.Settings.Default.POP3User, Properties.Settings.Default.POP3Password);
if (client.Capabilities.HasFlag(Pop3Capabilities.Apop))
Console.WriteLine("The server supports APOP authentication.");
if (client.Capabilities.HasFlag(Pop3Capabilities.Expire))
{
if (client.ExpirePolicy > 0)
Console.WriteLine("The POP3 server automatically expires messages after {0} days", client.ExpirePolicy);
else
Console.WriteLine("The POP3 server will never expire messages.");
}
if (client.Capabilities.HasFlag(Pop3Capabilities.LoginDelay))
Console.WriteLine("The minimum number of seconds between login attempts is {0}.", client.LoginDelay);
if (client.Capabilities.HasFlag(Pop3Capabilities.Pipelining))
Console.WriteLine("The POP3 server can pipeline commands, so using client.GetMessages() will be faster.");
if (client.Capabilities.HasFlag(Pop3Capabilities.Top))
Console.WriteLine("The POP3 server supports the TOP command, so it's possible to download message headers.");
if (client.Capabilities.HasFlag(Pop3Capabilities.UIDL))
Console.WriteLine("The POP3 server supports the UIDL command which means we can track messages by UID.");
client.Disconnect(true);
}
}
#endregion
#endregion
}
}

View File

@ -40,16 +40,29 @@
<AssemblyOriginatorKeyFile>..\bsmdKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.8.10.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
<HintPath>..\packages\Portable.BouncyCastle.1.8.10\lib\net40\BouncyCastle.Crypto.dll</HintPath>
</Reference>
<Reference Include="EmailValidation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=60f3df2f38e92638, processorArchitecture=MSIL">
<HintPath>..\packages\EmailValidation.1.0.4\lib\net45\EmailValidation.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="OpenPop, Version=2.0.6.1120, Culture=neutral, PublicKeyToken=6bdb97f144b7efc8, processorArchitecture=MSIL">
<HintPath>..\packages\OpenPop.NET.2.0.6.1120\lib\net40\OpenPop.dll</HintPath>
<Private>True</Private>
<Reference Include="MailKit, Version=2.13.0.0, Culture=neutral, PublicKeyToken=4e064fe7c44a8f1b, processorArchitecture=MSIL">
<HintPath>..\packages\MailKit.2.13.0\lib\net45\MailKit.dll</HintPath>
</Reference>
<Reference Include="MimeKit, Version=2.13.0.0, Culture=neutral, PublicKeyToken=bede1c8a46c66814, processorArchitecture=MSIL">
<HintPath>..\packages\MimeKit.2.13.0\lib\net45\MimeKit.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\netstandard1.1\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EmailValidation" version="1.0.4" targetFramework="net45" />
<package id="log4net" version="2.0.8" targetFramework="net45" />
<package id="OpenPop.NET" version="2.0.6.1120" targetFramework="net45" />
<package id="MailKit" version="2.13.0" targetFramework="net45" />
<package id="MimeKit" version="2.13.0" targetFramework="net45" />
<package id="Portable.BouncyCastle" version="1.8.10" targetFramework="net45" />
<package id="System.Buffers" version="4.5.1" targetFramework="net45" />
</packages>