This commit is contained in:
Digital Data - Marlon Schreiber 2019-04-11 18:06:30 +02:00
commit e575a7068e
6 changed files with 194 additions and 74 deletions

View File

@ -57,10 +57,6 @@
<Reference Include="DevExpress.XtraGrid.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" /> <Reference Include="DevExpress.XtraGrid.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraLayout.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" /> <Reference Include="DevExpress.XtraLayout.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
<Reference Include="DevExpress.XtraPrinting.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" /> <Reference Include="DevExpress.XtraPrinting.v18.1, Version=18.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
<Reference Include="DigitalData.EMLProfiler, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\DigitalData.EMLProfiler\bin\Debug\DigitalData.EMLProfiler.dll</HintPath>
</Reference>
<Reference Include="DigitalData.Modules.Logging"> <Reference Include="DigitalData.Modules.Logging">
<HintPath>..\..\..\DDMonorepo\Modules.Logging\bin\Debug\DigitalData.Modules.Logging.dll</HintPath> <HintPath>..\..\..\DDMonorepo\Modules.Logging\bin\Debug\DigitalData.Modules.Logging.dll</HintPath>
</Reference> </Reference>
@ -72,6 +68,9 @@
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.5.9\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.5.9\lib\net45\NLog.dll</HintPath>
</Reference> </Reference>
<Reference Include="S22.Imap, Version=3.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\S22.Imap.3.6.0.0\lib\net40\S22.Imap.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
@ -191,5 +190,11 @@
<None Include="Resources\Encrypt.png" /> <None Include="Resources\Encrypt.png" />
<None Include="Resources\folder_Closed_16xLG.png" /> <None Include="Resources\folder_Closed_16xLG.png" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DigitalData.EMLProfiler\DigitalData.EMLProfiler.vbproj">
<Project>{9f748dcd-952e-40a0-9dad-65bf8a39b231}</Project>
<Name>DigitalData.EMLProfiler</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
</Project> </Project>

View File

@ -664,9 +664,13 @@ Public Class frmMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.Save() My.Settings.Save()
Dim PWPlain = _Encryption.DecryptData(EMAIL_PWTextBox.Text) Dim PWPlain = _Encryption.DecryptData(EMAIL_PWTextBox.Text)
If _emailIMAP.TEST_IMAP_COLLECT(txtInboxname.Text, EMAIL_SMTPTextBox.Text, PORT_INTextBox.Text, EMAIL_USERTextBox.Text, PWPlain) = False Then
MsgBox("Access Imap NOT successfull", MsgBoxStyle.Critical)
Dim oResult = _emailIMAP.TestIMAPLogin(EMAIL_SMTPTextBox.Text, PORT_INTextBox.Text, EMAIL_USERTextBox.Text, PWPlain, txtInboxname.Text)
If oResult = False Then
MsgBox("Access Imap NOT successfull", MsgBoxStyle.Critical)
Else
MsgBox("Test Successful!", MsgBoxStyle.Information)
End If End If
End Sub End Sub

View File

@ -72,6 +72,9 @@
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.5.8\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.5.8\lib\net45\NLog.dll</HintPath>
</Reference> </Reference>
<Reference Include="S22.Imap, Version=3.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\S22.Imap.3.6.0.0\lib\net40\S22.Imap.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />

View File

@ -4,6 +4,10 @@ Imports Independentsoft.Email.Mime
Imports DigitalData.EMLProfiler.ClassCurrent Imports DigitalData.EMLProfiler.ClassCurrent
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports AE Imports AE
Imports System.Net
Imports System.Reflection
Imports System.IO
Public Class clsEmailIMAP Public Class clsEmailIMAP
Private Shared Logger As DigitalData.Modules.Logging.Logger Private Shared Logger As DigitalData.Modules.Logging.Logger
@ -13,87 +17,188 @@ Public Class clsEmailIMAP
'Private Shared Sub OnWriteLog(ByVal sender As Object, ByVal e As WriteLogEventArgs) 'Private Shared Sub OnWriteLog(ByVal sender As Object, ByVal e As WriteLogEventArgs)
' Logger.Info(e.Log) ' Logger.Info(e.Log)
'End Sub 'End Sub
Public Function IMAP_COLLECT()
Try
Logger.Info(String.Format("Working on IMAP_COLLECT....."))
Logger.Debug(String.Format("Working on IMAP_COLLECT....."))
Dim oClient As New Independentsoft.Email.Imap.ImapClient(MAIL_SERVER, MAIL_PORT)
oClient.ValidateRemoteCertificate = False Public Function TestIMAPLogin(Server As String, Port As Integer, Username As String, Password As String, Inbox As String) As Boolean
oClient.Connect() Logger.Debug("Testing Login to Server {0}:{1} with user {2}", Server, Port, Username)
oClient.Login(MAIL_USER, MAIL_USER_PW)
oClient.SelectFolder("Inbox") Try
Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oClient.ListMessages() Logger.Debug("Connecting...")
Dim oCount As Integer = 0 Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
For i As Integer = 0 To oEnvelopes.Length - 1 If Not oClient.Authed Then
If Not IsNothing(oEnvelopes(i).Subject) Then Logger.Warn("Connected to server but authentication failed.")
'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then Return False
Logger.Info($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
Dim oMessage As Mime.Message = oClient.GetMessage(oEnvelopes(i).UniqueID)
If Not IsNothing(oMessage) Then
oCount += 1
MAIL_LIST.Add(oMessage)
End If
'End If
End If End If
Next Logger.Debug("Connection successful")
oClient.Disconnect()
Logger.Debug($"{oCount.ToString} messages will be worked..") Logger.Debug("Fetching MessageIds..")
Logger.Debug("IMAP_COLLECT finished!") Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Inbox)
Return True
Logger.Debug("Found {0} messages", oMessageIds.Count)
Logger.Debug("Fetching messages...")
Dim oMessages As IEnumerable(Of Mail.MailMessage) = oClient.GetMessages(oMessageIds, False, Inbox)
Logger.Debug("Messages fetched")
oClient.Dispose()
Return True
End Using
Catch ex As Exception Catch ex As Exception
Logger.Error(ex, "Unexpected Error in IMAP COLLECT:") Logger.Error(ex)
Return False Return False
End Try End Try
End Function End Function
Public Function TEST_IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
Public Function FetchIMAPMessages(Server As String, Port As Integer, Username As String, Password As String, Inbox As String) As Boolean
Logger.Debug("Connecting to Server {0}:{1} with user {2}", Server, Port, Username)
Try Try
Logger.Info(String.Format("Working on TEST_IMAP_COLLECT.....")) Logger.Debug("Connecting...")
Dim oLogPath = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Log\logindependentSoft.txt") Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
Logger.Debug($"IsoftLog: {oLogPath}...") If Not oClient.Authed Then
Dim oindependentLogger As New Independentsoft.Email.Logger(oLogPath) Logger.Warn("Connected to server but authentication failed.")
Return False
Dim oImapClient As New Independentsoft.Email.Imap.ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
oImapClient.Logger = oindependentLogger
oImapClient.ValidateRemoteCertificate = False
oImapClient.Connect()
Logger.Debug($"oImapClient connected...")
Try
oImapClient.Login(MAIL_USER, MAIL_USER_PW, AuthenticationType.Login)
Catch ex As Exception
MsgBox($"Unexpected error in (oImapClient.Login): {ex.Message}")
Logger.Info($"Unexpected error in oImapClient.Login - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
Logger.Warn(ex.StackTrace.ToString)
Logger.Warn(ex.Message)
oImapClient.Logger.Close()
Logger.Error(ex)
Return False
End Try
oImapClient.SelectFolder(INBOXNAME)
Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oImapClient.ListMessages()
For i As Integer = 0 To oEnvelopes.Length - 1
If Not IsNothing(oEnvelopes(i).Subject) Then
'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then
MsgBox($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
Dim message As Mime.Message = oImapClient.GetMessage(oEnvelopes(i).UniqueID)
End If End If
Next Logger.Debug("Connection successful")
oImapClient.Disconnect() Logger.Debug("Fetching MessageIds..")
oImapClient.Logger.Close() Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Inbox)
Logger.Info("TEST_IMAP_COLLECT finished!")
Logger.Debug("Found {0} messages", oMessageIds.Count)
Logger.Debug("Fetching messages...")
For Each oMessageId As UInteger In oMessageIds
Dim oMessage = oClient.GetMessage(oMessageId, False, Inbox)
Dim oTempPath = Path.GetTempFileName()
Dim oResult = WriteMessageToFile(oMessage, oTempPath)
Dim oMsg As New Message(oTempPath)
MAIL_LIST.Add(oMsg)
Try
File.Delete(oTempPath)
Catch ex As Exception
Logger.Error(ex)
Logger.Warn("Temp file could not be deleted")
End Try
Next
Logger.Debug("Messages fetched")
End Using
Return True Return True
Catch ex As Exception Catch ex As Exception
Logger.Info($"Unexpected error in TEST_IMAP_COLLECT - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]") Logger.Error(ex)
MsgBox($"Unexpected error in TEST_IMAP_COLLECT: {ex.Message}")
Logger.Error(ex, "Unexpected Error in TEST_IMAP_COLLECT:")
Return False Return False
End Try End Try
End Function End Function
''' <summary>
''' Uses a private API from MailWriter to write a MailMessage to disk.
''' May break in future versions of .NET
''' </summary>
Public Function WriteMessageToFile(Message As Mail.MailMessage, Filename As String) As Boolean
Dim oAssembly As Assembly = GetType(Mail.SmtpClient).Assembly
Dim oMailWriterType As Type = oAssembly.[GetType]("System.Net.Mail.MailWriter")
Try
Using oStream As New FileStream(Filename, FileMode.Create)
Dim oMailWriterConstructor As ConstructorInfo = oMailWriterType.GetConstructor(
BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing
)
Dim oMailWriter As Object = oMailWriterConstructor.Invoke(New Object() {oStream})
Dim sendMethod As MethodInfo = GetType(Mail.MailMessage).GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic)
sendMethod.Invoke(Message, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, {oMailWriter, True, True}, Nothing)
End Using
Return True
Catch ex As Exception
Return Nothing
End Try
End Function
'Public Function IMAP_COLLECT()
' Try
' Logger.Info(String.Format("Working on IMAP_COLLECT....."))
' Logger.Debug(String.Format("Working on IMAP_COLLECT....."))
' Dim oClient As New Independentsoft.Email.Imap.ImapClient(MAIL_SERVER, MAIL_PORT)
' oClient.ValidateRemoteCertificate = False
' oClient.Connect()
' oClient.Login(MAIL_USER, MAIL_USER_PW)
' oClient.SelectFolder("Inbox")
' Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oClient.ListMessages()
' Dim oCount As Integer = 0
' For i As Integer = 0 To oEnvelopes.Length - 1
' If Not IsNothing(oEnvelopes(i).Subject) Then
' 'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then
' Logger.Info($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
' Dim oMessage As Mime.Message = oClient.GetMessage(oEnvelopes(i).UniqueID)
' If Not IsNothing(oMessage) Then
' oCount += 1
' MAIL_LIST.Add(oMessage)
' End If
' 'End If
' End If
' Next
' oClient.Disconnect()
' Logger.Debug($"{oCount.ToString} messages will be worked..")
' Logger.Debug("IMAP_COLLECT finished!")
' Return True
' Catch ex As Exception
' Logger.Error(ex, "Unexpected Error in IMAP COLLECT:")
' Return False
' End Try
'End Function
'Public Function TEST_IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
' Try
' Logger.Info(String.Format("Working on TEST_IMAP_COLLECT....."))
' Dim oLogPath = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Log\logindependentSoft.txt")
' Logger.Debug($"IsoftLog: {oLogPath}...")
' Dim oindependentLogger As New Independentsoft.Email.Logger(oLogPath)
' Dim oImapClient As New Independentsoft.Email.Imap.ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
' oImapClient.Logger = oindependentLogger
' oImapClient.ValidateRemoteCertificate = False
' oImapClient.Connect()
' Logger.Debug($"oImapClient connected...")
' Try
' oImapClient.Login(MAIL_USER, MAIL_USER_PW, AuthenticationType.Login)
' Catch ex As Exception
' MsgBox($"Unexpected error in (oImapClient.Login): {ex.Message}")
' Logger.Info($"Unexpected error in oImapClient.Login - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
' Logger.Warn(ex.StackTrace.ToString)
' Logger.Warn(ex.Message)
' oImapClient.Logger.Close()
' Logger.Error(ex)
' Return False
' End Try
' oImapClient.SelectFolder(INBOXNAME)
' Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oImapClient.ListMessages()
' For i As Integer = 0 To oEnvelopes.Length - 1
' If Not IsNothing(oEnvelopes(i).Subject) Then
' 'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then
' MsgBox($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
' Dim message As Mime.Message = oImapClient.GetMessage(oEnvelopes(i).UniqueID)
' End If
' Next
' oImapClient.Disconnect()
' oImapClient.Logger.Close()
' Logger.Info("TEST_IMAP_COLLECT finished!")
' Return True
' Catch ex As Exception
' Logger.Info($"Unexpected error in TEST_IMAP_COLLECT - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
' MsgBox($"Unexpected error in TEST_IMAP_COLLECT: {ex.Message}")
' Logger.Error(ex, "Unexpected Error in TEST_IMAP_COLLECT:")
' Return False
' End Try
'End Function
Public Function TEST_AEIMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String) Public Function TEST_AEIMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
Try Try
Logger.Info(String.Format("Working on TEST_AEIMAP_COLLECT.....")) Logger.Info(String.Format("Working on TEST_AEIMAP_COLLECT....."))

View File

@ -127,7 +127,9 @@ Public Class clsWorker
Case "POP" Case "POP"
pollresult = _email.POP3_COLLECT() pollresult = _email.POP3_COLLECT()
Case "IMAP" Case "IMAP"
pollresult = _emailIMAP.IMAP_COLLECT() 'pollresult = _emailIMAP.IMAP_COLLECT()
' 09.04.19: Use S22.Imap instead of IndependentSoft ImapClient
pollresult = _emailIMAP.FetchIMAPMessages(MAIL_SERVER, MAIL_PORT, MAIL_USER, MAIL_USER_PW, "Inbox")
End Select End Select
End If End If

View File

@ -2,4 +2,5 @@
<packages> <packages>
<package id="AE.Net.Mail" version="1.7.10.0" targetFramework="net461" /> <package id="AE.Net.Mail" version="1.7.10.0" targetFramework="net461" />
<package id="NLog" version="4.5.8" targetFramework="net461" /> <package id="NLog" version="4.5.8" targetFramework="net461" />
<package id="S22.Imap" version="3.6.0.0" targetFramework="net461" />
</packages> </packages>