WIP Email Validation
This commit is contained in:
@@ -45,11 +45,11 @@ Public Class ClassCurrent
|
||||
Public Shared Property CURRENT_WM_DOC As WMObject
|
||||
Public Shared Property CURRENT_DOC_ID As Integer
|
||||
Public Shared Property CURRENT_DOC_PATH As String
|
||||
Public Shared Property MessageError As Boolean = False
|
||||
Public Shared Property MESSAGE_ERROR As Boolean = False
|
||||
|
||||
Public Shared Property TEMP_FILES As List(Of String) = New List(Of String)
|
||||
|
||||
Public Shared Property CURRENT_DEBUG_LOCAL_EMAIL As String
|
||||
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
@@ -62,6 +62,9 @@
|
||||
<Reference Include="DigitalData.Modules.Messaging">
|
||||
<HintPath>..\..\..\DDModules\Messaging\bin\Debug\DigitalData.Modules.Messaging.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="DigitalData.Modules.Patterns">
|
||||
<HintPath>..\..\..\DDModules\Patterns\bin\Debug\DigitalData.Modules.Patterns.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Interop.WINDREAMLib">
|
||||
<HintPath>P:\Visual Studio Projekte\Bibliotheken\windream\Interop.WINDREAMLib.dll</HintPath>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
Public Const EMAIL_MISSINGPROPERTIES_1 = "<p>Die angehängte Datei entspricht nicht dem ZUGFeRD-Format: {0}</p>"
|
||||
Public Const EMAIL_MISSINGPROPERTIES_2 = "<p>Die folgenden Eigenschaften wurden als ERFORDERLICH eingestuft, wurden aber nicht gefunden:<p/>"
|
||||
|
||||
|
||||
|
||||
Public Const EMAIL_MD5_ERROR = "<p>Die von Ihnen gesendete Rechnung wurde bereits von unserem System verarbeitet.</p>"
|
||||
|
||||
Public Const EMAIL_TOO_MUCH_FERDS = "<p>Ihre Email enthielt mehr als ein ZUGFeRD-Dokument.</p>"
|
||||
|
||||
@@ -737,7 +737,7 @@ Public Class clsWindream_Index
|
||||
Return ValueArray
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
ClassCurrent.MessageError = True
|
||||
ClassCurrent.MESSAGE_ERROR = True
|
||||
'clsLogger.AddError(ex.Message, "Return_VektorArray")
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
@@ -10,6 +10,7 @@ Imports Limilabs.Mail
|
||||
Imports Limilabs.Mail.MIME
|
||||
Imports Limilabs.Mail.Headers
|
||||
Imports MailBox = Limilabs.Mail.Headers.MailBox
|
||||
Imports DigitalData.Modules.Patterns
|
||||
|
||||
Public Class clsWorkEmail
|
||||
Private Const SUBJECT_MAX_LENGTH = 25
|
||||
@@ -25,13 +26,6 @@ Public Class clsWorkEmail
|
||||
Private CURRENT_MAIL_SUBJECT As String = ""
|
||||
Private CURRENT_MAIL_FROM As String = ""
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' Primary Mail Identifier.
|
||||
''' Is a hash of the MessageId, used to be the MessageId itself.
|
||||
''' </summary>
|
||||
'Private Property CURRENT_MAIL_MESSAGE_ID As String = ""
|
||||
|
||||
Private CurrentMailProcessName As String
|
||||
|
||||
Private ReadOnly _Logger As Logger
|
||||
@@ -43,6 +37,7 @@ Public Class clsWorkEmail
|
||||
Private ReadOnly _windream As clsWindream_allgemein
|
||||
Private ReadOnly _windream_index As clsWindream_Index
|
||||
Private ReadOnly _windreamConnectionString As String
|
||||
Private ReadOnly _Patterns As Patterns2
|
||||
|
||||
Private ReadOnly _EmailAccountID As Integer = 1
|
||||
|
||||
@@ -52,10 +47,10 @@ Public Class clsWorkEmail
|
||||
Try
|
||||
_Logger = LogConf.GetLogger
|
||||
_LogConfig = LogConf
|
||||
'_DB_MSSQL = New clsDatabase(LogConf, ConStr)
|
||||
_DB_MSSQL = New MSSQLServer(LogConf, ConStr)
|
||||
_Logger.Debug("clsWorkmail _email initialized")
|
||||
_UseWindream = pUseWindream
|
||||
_Patterns = New Patterns2(LogConf)
|
||||
|
||||
If pUseWindream Then
|
||||
_windream = New clsWindream_allgemein(LogConf)
|
||||
@@ -69,7 +64,7 @@ Public Class clsWorkEmail
|
||||
_Logger.Error(ex)
|
||||
End Try
|
||||
End Sub
|
||||
Public Function WorkEmailMessage(pMailMessage As IMail, poUID As Long) As Boolean
|
||||
Public Function WorkEmailMessage(pMailMessage As IMail, poUID As Long, pValidationSQL As String) As Boolean
|
||||
Try
|
||||
For Each m As MailBox In pMailMessage.From
|
||||
CURRENT_MAIL_FROM = m.Address
|
||||
@@ -82,7 +77,6 @@ Public Class clsWorkEmail
|
||||
CURRENT_MAIL_BODY_ALL = ""
|
||||
CURRENT_MAIL_BODY_ANSWER1 = ""
|
||||
CURRENT_MAIL_BODY_Substr2 = ""
|
||||
'CURRENT_MAIL_MESSAGE = pMailMessage
|
||||
CURRENT_MAIL_SUBJECT = pMailMessage.Subject.ToUpper.EscapeForSQL()
|
||||
|
||||
CURRENT_MAIL_UID = poUID
|
||||
@@ -120,8 +114,8 @@ Public Class clsWorkEmail
|
||||
|
||||
_Logger.Info($"Working on email from : {CURRENT_MAIL_FROM}...")
|
||||
|
||||
Dim osql = $"Select COALESCE(MAX(GUID),0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{CurrentMail.MessageId}'"
|
||||
Dim oHistoryID = _DB_MSSQL.GetScalarValue(osql)
|
||||
Dim oSql = $"Select COALESCE(MAX(GUID),0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{CurrentMail.MessageId}'"
|
||||
Dim oHistoryID = _DB_MSSQL.GetScalarValue(oSql)
|
||||
|
||||
If oHistoryID > 0 Then
|
||||
_Logger.Info($"Messsage with subject [{CURRENT_MAIL_SUBJECT}] from [{CURRENT_MAIL_FROM}] has already been worked!")
|
||||
@@ -129,48 +123,80 @@ Public Class clsWorkEmail
|
||||
End If
|
||||
|
||||
Dim oTempMailExists As Boolean = SAVE2TEMP(CurrentMail)
|
||||
|
||||
'Checking wether Mail can be opened
|
||||
Dim oTempMailAccessible As Boolean = False
|
||||
If oTempMailExists = True Then
|
||||
Try
|
||||
Dim oFS As FileStream = File.OpenRead(CurrentTempMailPath)
|
||||
oTempMailAccessible = True
|
||||
oFS.Close()
|
||||
Catch ex As Exception
|
||||
_Logger.Warn($"Could not read the Temp-Mail. Insufficient rights? Message: {ex.Message}")
|
||||
End Try
|
||||
If oTempMailAccessible = True Then
|
||||
MessageError = False
|
||||
If CURRENT_MAIL_SUBJECT.Contains("[PROCESSMANAGER]") Then
|
||||
PROCESS_MANAGER_IN(CurrentMail)
|
||||
ElseIf pMailMessage.Subject.Contains("[ADDI]") Then
|
||||
|
||||
Else
|
||||
_Logger.Debug("CommonEmail-Process-Sniffer")
|
||||
If COMMON_EMAIL_IN(CurrentMail) = True Then
|
||||
InsertHistoryEntry(CurrentMail)
|
||||
If CURRENT_ATTMT_COUNT = 0 Then
|
||||
_Logger.Info("### Mail contained no Attachments!! ###")
|
||||
Dim oBody = EmailStrings.EMAIL_NO_FERDS
|
||||
If AddToEmailQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then
|
||||
CURRENT_ImapObject.DeleteMessageByUID(poUID)
|
||||
End If
|
||||
End If
|
||||
Return True
|
||||
Else
|
||||
Return False
|
||||
If oTempMailExists = False Then
|
||||
_Logger.Warn("Could not process email [{0}], file does not exist!", CurrentMail.MessageId)
|
||||
Return False
|
||||
End If
|
||||
|
||||
Try
|
||||
Dim oFS As FileStream = File.OpenRead(CurrentTempMailPath)
|
||||
oTempMailAccessible = True
|
||||
oFS.Close()
|
||||
Catch ex As Exception
|
||||
_Logger.Warn($"Could not read the Temp-Mail. Insufficient rights? Message: {ex.Message}")
|
||||
End Try
|
||||
|
||||
If oTempMailAccessible = False Then
|
||||
_Logger.Warn("Could not process email [{0}], file does is not accessible!", CurrentMail.MessageId)
|
||||
Return False
|
||||
End If
|
||||
|
||||
MESSAGE_ERROR = False
|
||||
|
||||
If pValidationSQL <> "" Then
|
||||
|
||||
Dim oReplaceValues = New Dictionary(Of String, String) From {
|
||||
{"EMAIL", CurrentMail.SenderAddress},
|
||||
{"DOMAIN", CurrentMail.SenderDomain}
|
||||
}
|
||||
|
||||
Dim pValidationSQLWithPlaceholders = _Patterns.ReplaceCustomValues(pValidationSQL, oReplaceValues)
|
||||
Dim oResult As String = ObjectEx.NotNull(_DB_MSSQL.GetScalarValue(pValidationSQLWithPlaceholders), "")
|
||||
|
||||
If oResult <> "" Then
|
||||
'insert history und exit
|
||||
InsertHistoryEntryWithStatus(pMailMessage, "REJECTED", oResult)
|
||||
|
||||
AddToEmailQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID)
|
||||
|
||||
' Return early from processing eml
|
||||
Return True
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
If CURRENT_MAIL_SUBJECT.Contains("[PROCESSMANAGER]") Then
|
||||
Return PROCESS_MANAGER_IN(CurrentMail)
|
||||
|
||||
Else
|
||||
_Logger.Debug("CommonEmail-Process-Sniffer")
|
||||
If COMMON_EMAIL_IN(CurrentMail) = True Then
|
||||
InsertHistoryEntry(CurrentMail)
|
||||
If CURRENT_ATTMT_COUNT = 0 Then
|
||||
_Logger.Info("### Mail contained no Attachments!! ###")
|
||||
Dim oBody = EmailStrings.EMAIL_NO_FERDS
|
||||
If AddToEmailQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then
|
||||
CURRENT_ImapObject.DeleteMessageByUID(poUID)
|
||||
End If
|
||||
End If
|
||||
|
||||
Return True
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function AddToEmailQueueMSSQL(MessageId As String, BodyText As String, SourceProcedure As String, pEmailAccountId As Integer) As Boolean
|
||||
Public Function AddToEmailQueueMSSQL(MessageId As String, BodyText As String, Comment As String, pEmailAccountId As Integer) As Boolean
|
||||
|
||||
Try
|
||||
Dim oReference = MessageId
|
||||
@@ -222,7 +248,7 @@ Public Class clsWorkEmail
|
||||
,'{oEmailTo}'
|
||||
,'{oSubject}'
|
||||
,'{oFinalBodyText}'
|
||||
,'{SourceProcedure}'
|
||||
,'{Comment}'
|
||||
,'{oCreatedWho}')"
|
||||
Return _DB_MSSQL.ExecuteNonQuery(oInsert)
|
||||
Else
|
||||
@@ -478,37 +504,19 @@ Public Class clsWorkEmail
|
||||
|
||||
End Try
|
||||
End Function
|
||||
Private Function COPY2HDD(pCurrentMail As MailContainer, pShouldCopyToDisk As Boolean, pPathOriginal As String, pPathErrors As String, pUseMessageIdAsFilename As Boolean) As Boolean
|
||||
Private Function COPY2HDD(pCurrentMail As MailContainer, pShouldCopyToDisk As Boolean, pDestination As String, pPathErrors As String, pUseMessageIdAsFilename As Boolean) As Boolean
|
||||
Try
|
||||
If pShouldCopyToDisk = True Then
|
||||
_Logger.Debug("COPY_2_HDD is ACTIVE!")
|
||||
'PATH_ERROR = pPathErrors
|
||||
If Directory.Exists(pPathOriginal) Then
|
||||
Dim oTempFilename = pPathOriginal
|
||||
If Directory.Exists(pDestination) Then
|
||||
Dim oTempFilename = Path.Combine(pDestination, $"{pCurrentMail.MessageId}.eml")
|
||||
|
||||
'If pUseMessageIdAsFilename = True Then
|
||||
' Dim oFileName = CURRENT_MAIL_MESSAGE_ID & ".eml"
|
||||
' oTempFilename = Path.Combine(oTempFilename, oFileName)
|
||||
|
||||
' 'oTempFilename &= "\" & CURRENT_MAIL_MESSAGE_ID & ".eml"
|
||||
'Else
|
||||
' Dim oFileName = CURRENT_MAIL_MESSAGE.Subject.Truncate(SUBJECT_MAX_LENGTH).Replace(" ", "") & ".eml"
|
||||
' oTempFilename = Path.Combine(oTempFilename, oFileName)
|
||||
|
||||
' 'oTempFilename &= "\" & CURRENT_MAIL_MESSAGE.Subject.Replace(" ", "") & ".eml"
|
||||
'End If
|
||||
oTempFilename = Path.Combine(oTempFilename, $"{pCurrentMail.MessageId}.eml")
|
||||
|
||||
'Dim cleanPath As String = String.Join("", oTempFilename.Split(Path.GetInvalidPathChars()))
|
||||
If File.Exists(oTempFilename) = False Then
|
||||
Try
|
||||
File.Delete(oTempFilename)
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
|
||||
pCurrentMail.Mail.Save(oTempFilename)
|
||||
|
||||
|
||||
Dim oFileInfo As New FileInfo(oTempFilename)
|
||||
Dim oFileLenth As Long = oFileInfo.Length
|
||||
If oFileLenth > 0 Then
|
||||
@@ -529,7 +537,7 @@ Public Class clsWorkEmail
|
||||
Return True
|
||||
End If
|
||||
Else
|
||||
_Logger.Error("Destination directory [{0}] does not exist!", pPathOriginal)
|
||||
_Logger.Error("Destination directory [{0}] does not exist!", pDestination)
|
||||
Return False
|
||||
End If
|
||||
Else
|
||||
@@ -595,7 +603,7 @@ Public Class clsWorkEmail
|
||||
If CURRENT_MAIL_BODY_ALL = String.Empty Then
|
||||
_Logger.Warn("Mailbody is empty. Email can not be processed! - Please check the html-structure")
|
||||
_Logger.Info("EXCEPTION - Mailbody is empty.Email can not be processed! - Please check the html-structure")
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
Return False
|
||||
Else
|
||||
_Logger.Debug($"Length of Body is [{CURRENT_MAIL_BODY_ALL.Length}] - Body Text is [{CURRENT_MAIL_BODY_ALL}]")
|
||||
@@ -638,7 +646,7 @@ Public Class clsWorkEmail
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
'clsLogger.Add("Unexpected Error in COPY2HDD: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID, True)
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
@@ -714,12 +722,12 @@ Public Class clsWorkEmail
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
End Try
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Warn($"Error while saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFilePath}")
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
|
||||
End Try
|
||||
Else
|
||||
@@ -729,7 +737,7 @@ Public Class clsWorkEmail
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Warn($"Error while creating and saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFilePath}")
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
|
||||
End Try
|
||||
Next
|
||||
@@ -740,21 +748,29 @@ Public Class clsWorkEmail
|
||||
_Logger.Warn($"EXTRACT_ATTACHMENTSIf cause 1: CURRENT_TEMP_MAIL_PATH is NOTHING")
|
||||
End If
|
||||
CURRENT_ATTMT_COUNT = oAttachmentCount
|
||||
If MessageError = True Then
|
||||
If MESSAGE_ERROR = True Then
|
||||
Return False
|
||||
Else
|
||||
Return True
|
||||
End If
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function InsertHistoryEntry(pCurrentMail As MailContainer) As Boolean
|
||||
If MessageError = False Then
|
||||
Dim ins = $"INSERT INTO TBEMLP_HISTORY (
|
||||
If MESSAGE_ERROR = False Then
|
||||
Return InsertHistoryEntryWithStatus(pCurrentMail, String.Empty, String.Empty)
|
||||
Else
|
||||
_Logger.Info("! No INSERT_HISTORY as MessageError = True")
|
||||
Return False
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Function InsertHistoryEntryWithStatus(pCurrentMail As MailContainer, pStatus As String, pComment As String) As Boolean
|
||||
Dim ins = $"INSERT INTO TBEMLP_HISTORY (
|
||||
WORK_PROCESS,
|
||||
EMAIL_MSGID,
|
||||
EMAIL_SUBJECT,
|
||||
@@ -763,7 +779,9 @@ Public Class clsWorkEmail
|
||||
EMAIL_SUBSTRING1,
|
||||
EMAIL_SUBSTRING2,
|
||||
EMAIL_FROM,
|
||||
PROFILE_ID
|
||||
PROFILE_ID,
|
||||
STATUS,
|
||||
COMMENT
|
||||
) VALUES (
|
||||
'{CurrentMailProcessName}',
|
||||
'{pCurrentMail.MessageId}',
|
||||
@@ -773,13 +791,11 @@ Public Class clsWorkEmail
|
||||
'{CURRENT_MAIL_BODY_ANSWER1}',
|
||||
'{CURRENT_MAIL_BODY_Substr2}',
|
||||
'{pCurrentMail.SenderAddress}',
|
||||
{CURRENT_PROFILE_GUID}
|
||||
{CURRENT_PROFILE_GUID},
|
||||
'{pStatus}',
|
||||
'{pComment.Substring(0, 500)}'
|
||||
)"
|
||||
Return _DB_MSSQL.ExecuteNonQuery(ins)
|
||||
Else
|
||||
_Logger.Info("! No INSERT_HISTORY as MessageError = True")
|
||||
Return False
|
||||
End If
|
||||
Return _DB_MSSQL.ExecuteNonQuery(ins)
|
||||
End Function
|
||||
|
||||
Private Function InsertAttachmentHistoryEntry(pCurrentMail As MailContainer, pMessageId As String, pFileName As String) As Boolean
|
||||
@@ -788,8 +804,16 @@ Public Class clsWorkEmail
|
||||
Return False
|
||||
End If
|
||||
Try
|
||||
If MessageError = False Then
|
||||
Dim ins = $"INSERT INTO TBEMLP_HISTORY_ATTACHMENT (WORK_PROCESS,EMAIL_MSGID,EMAIL_FROM,EMAIL_SUBJECT,EMAIL_DATETIME,EMAIL_BODY,EMAIL_ATTMT) VALUES " &
|
||||
If MESSAGE_ERROR = False Then
|
||||
Dim ins = $"INSERT INTO TBEMLP_HISTORY_ATTACHMENT (
|
||||
WORK_PROCESS,
|
||||
EMAIL_MSGID,
|
||||
EMAIL_FROM,
|
||||
EMAIL_SUBJECT,
|
||||
EMAIL_DATETIME,
|
||||
EMAIL_BODY,
|
||||
EMAIL_ATTMT
|
||||
) VALUES " &
|
||||
$"('{CurrentMailProcessName}'," &
|
||||
$"'{pMessageId}'," &
|
||||
$"'{pCurrentMail.SenderAddress}'," &
|
||||
@@ -860,7 +884,7 @@ Public Class clsWorkEmail
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
_Logger.Error(ex)
|
||||
'clsLogger.Add("Unexpected Error in WORK_POLL_STEPS: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID, True)
|
||||
Return False
|
||||
@@ -881,7 +905,7 @@ Public Class clsWorkEmail
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
_Logger.Error(ex)
|
||||
'clsLogger.Add("Unexpected Error in WORK_INDEXING_STEPS: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID, True)
|
||||
Return False
|
||||
@@ -981,7 +1005,7 @@ Public Class clsWorkEmail
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
'clsLogger.Add("Unexpected Error in GET_DOC_INFO: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID)
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
@@ -999,7 +1023,7 @@ Public Class clsWorkEmail
|
||||
Next
|
||||
Return result
|
||||
Catch ex As Exception
|
||||
MessageError = True
|
||||
MESSAGE_ERROR = True
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
Imports DigitalData.EMLProfiler.ClassCurrent
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Messaging
|
||||
Imports Limilabs.Mail
|
||||
Imports Limilabs.Mail.MIME
|
||||
Imports Limilabs.Mail.Headers
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Base
|
||||
Imports DigitalData.Modules.Messaging.Mail
|
||||
Imports Limilabs.Mail
|
||||
Imports Limilabs.Mail.Headers
|
||||
|
||||
Public Class clsWorker
|
||||
Private ReadOnly Logger As Logger
|
||||
@@ -18,6 +17,9 @@ Public Class clsWorker
|
||||
Private ReadOnly Encryption As clsEncryption
|
||||
Private ReadOnly ProfileId As Integer = 0D
|
||||
Private ReadOnly EmailLimitationSender As String = ""
|
||||
Private ReadOnly EmailLimitationEnabled As Boolean = False
|
||||
|
||||
Private ReadOnly LocalEmlFile As String = ""
|
||||
|
||||
Sub New(pLogConfig As LogConfig, pConnectionString As String, pWindreamConnectionString As String, pPollProfileId As Integer, pUseWindream As Boolean, pEmailAccountID As Integer, pEmailPrefix As String, pEmailLimitationSender As String, Optional pLocalEML As String = "")
|
||||
Logger = pLogConfig.GetLogger
|
||||
@@ -27,6 +29,10 @@ Public Class clsWorker
|
||||
|
||||
UseWindream = pUseWindream
|
||||
EmailLimitationSender = pEmailLimitationSender
|
||||
If EmailLimitationSender.Contains("@") Then
|
||||
Logger.Info("Email Sender Limitation active for address: [{0}]", EmailLimitationSender)
|
||||
EmailLimitationEnabled = True
|
||||
End If
|
||||
|
||||
If UseWindream Then
|
||||
ClassWindreamAllgemein = New clsWindream_allgemein(pLogConfig)
|
||||
@@ -38,7 +44,7 @@ Public Class clsWorker
|
||||
|
||||
Encryption = New clsEncryption("!35452didalog=", pLogConfig)
|
||||
ProfileId = pPollProfileId
|
||||
CURRENT_DEBUG_LOCAL_EMAIL = pLocalEML
|
||||
LocalEmlFile = pLocalEML
|
||||
End Sub
|
||||
|
||||
Private Sub DeleteTempFiles()
|
||||
@@ -57,184 +63,196 @@ Public Class clsWorker
|
||||
TEMP_FILES.Clear()
|
||||
End Sub
|
||||
|
||||
Private Function LoadEmailAccounts() As DataTable
|
||||
Return Database.GetDatatable("SELECT * FROM TBDD_EMAIL_ACCOUNT WHERE ACTIVE = 1")
|
||||
End Function
|
||||
|
||||
Private Function LoadPollingProfiles(pProfileId As Integer) As DataTable
|
||||
Dim oSQL = "SELECT * FROM TBEMLP_POLL_PROFILES WHERE ACTIVE = 1"
|
||||
|
||||
If ProfileId = 0 Then
|
||||
oSQL &= " ORDER BY SEQUENCE"
|
||||
Else
|
||||
oSQL &= $" WHERE GUID = {pProfileId}"
|
||||
End If
|
||||
|
||||
Return Database.GetDatatable(oSQL)
|
||||
End Function
|
||||
|
||||
Public Sub Start_WorkingProfiles(Optional LocalEmail As Boolean = False)
|
||||
Try
|
||||
DeleteTempFiles()
|
||||
|
||||
If Database.DBInitialized = True Then
|
||||
Logger.Debug("now windream_init... ")
|
||||
If UseWindream Then
|
||||
If ClassWindreamAllgemein.Init = False Then
|
||||
Logger.Info("windream could not be initialized!!")
|
||||
Exit Sub
|
||||
Else
|
||||
Logger.Debug("windream_initialized!")
|
||||
End If
|
||||
End If
|
||||
Dim osql As String = "SELECT * FROM TBEMLP_POLL_PROFILES"
|
||||
If ProfileId = 0 Then
|
||||
osql &= " WHERE ACTIVE = 1 order by SEQUENCE"
|
||||
Else
|
||||
osql &= " WHERE GUID = " & ProfileId
|
||||
End If
|
||||
Dim DT_TBDD_EMAIL As DataTable = Database.GetDatatable("SELECT * FROM TBDD_EMAIL_ACCOUNT WHERE ACTIVE = 1")
|
||||
Dim DT_PROFILES = Database.GetDatatable(osql)
|
||||
If Not IsNothing(DT_PROFILES) Then
|
||||
If DT_PROFILES.Rows.Count > 0 Then
|
||||
Logger.Debug("count of active profiles: " & DT_PROFILES.Rows.Count.ToString)
|
||||
For Each oDR_Profile As DataRow In DT_PROFILES.Rows
|
||||
CURRENT_PROFILE_GUID = oDR_Profile.Item("GUID")
|
||||
DT_POLL_PROCESS = Nothing
|
||||
Dim sql = String.Format("SELECT * FROM TBEMLP_POLL_PROCESS WHERE PROFILE_ID = {0} AND ACTIVE = 1", CURRENT_PROFILE_GUID)
|
||||
DT_POLL_PROCESS = Database.GetDatatable(sql)
|
||||
If Not IsNothing(DT_POLL_PROCESS) Then
|
||||
If DT_POLL_PROCESS.Rows.Count = 0 Then
|
||||
Logger.Info("No processes configured for this Email-Profile - " & sql)
|
||||
Continue For
|
||||
Else
|
||||
DT_STEPS = Nothing
|
||||
DT_STEPS = Database.GetDatatable(String.Format("SELECT T.* FROM TBEMLP_POLL_STEPS T,TBEMLP_POLL_PROCESS T1 WHERE T.PROCESS_ID = T1.GUID AND T1.PROFILE_ID = {0} AND T1.ACTIVE = 1", CURRENT_PROFILE_GUID))
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("DT_POLL_PROCESS is nothing")
|
||||
Continue For
|
||||
End If
|
||||
|
||||
CURRENT_EMAIL_GUID = 0
|
||||
|
||||
CURRENT_POLL_TYPE = oDR_Profile.Item("POLL_TYPE")
|
||||
Logger.Debug(String.Format("Working on profile: ({0}-{1}-{2}) ", oDR_Profile.Item("GUID"), oDR_Profile.Item("PROFILE_NAME"), CURRENT_POLL_TYPE))
|
||||
CURRENT_EMAIL_GUID = oDR_Profile.Item("EMAIL_CONF_ID")
|
||||
|
||||
Dim EMAIL_PROF_TABLE As DataTable = DT_TBDD_EMAIL
|
||||
'Presuming the DataTable has a column named Date.
|
||||
Dim expression As String
|
||||
expression = "GUID = " & CURRENT_EMAIL_GUID
|
||||
Logger.Debug("Expression Filter email: " & expression)
|
||||
Logger.Debug("DT_TBDD_EMAIL.rowCount: " & DT_TBDD_EMAIL.Rows.Count)
|
||||
'Filter the rows using Select() method of DataTable
|
||||
Dim FilteredRows As List(Of DataRow) = EMAIL_PROF_TABLE.Select(expression).ToList()
|
||||
If FilteredRows.Count = 1 Then
|
||||
Dim oRow = FilteredRows(0)
|
||||
Dim MAIL_FROM = oRow("EMAIL_FROM")
|
||||
Dim MAIL_SERVER = oRow("EMAIL_SMTP")
|
||||
Dim MAIL_USER = oRow("EMAIL_USER")
|
||||
Dim MAIL_USER_PW = oRow("EMAIL_PW")
|
||||
Dim MAIL_PORT = oRow("PORT_IN")
|
||||
Dim MAIL_INBOX_NAME = "Inbox"
|
||||
Dim MAIL_ARCHIVE_FOLDER = oRow("ARCHIVE_FOLDER")
|
||||
Dim MAIL_AUTHTYPE = oRow("AUTH_TYPE")
|
||||
Logger.Debug(String.Format("{0}-{1}", MAIL_FROM, MAIL_SERVER))
|
||||
|
||||
Dim PWPlain = Encryption.DecryptData(MAIL_USER_PW)
|
||||
If Not IsNothing(PWPlain) Then
|
||||
If PWPlain <> "" Then
|
||||
MAIL_USER_PW = PWPlain
|
||||
Else
|
||||
Logger.Warn("PWPlain is string.empty - Could not decrypt passwort")
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("PWPlain is nothing - Could not decrypt passwort")
|
||||
End If
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST.Clear()
|
||||
|
||||
If MAIL_SERVER <> "" Then
|
||||
Dim oPollResult As Boolean = False
|
||||
If LocalEmail = True Then
|
||||
oPollResult = True
|
||||
Else
|
||||
Select Case CURRENT_POLL_TYPE
|
||||
Case "IMAP"
|
||||
' We are using 'Archive Folder' as an additional field to save the Tenant-ID for O365-OAuth2
|
||||
oPollResult = FetchMessages(MAIL_SERVER, MAIL_PORT, MAIL_USER, MAIL_USER_PW, MAIL_AUTHTYPE, MAIL_ARCHIVE_FOLDER)
|
||||
|
||||
Case Else
|
||||
Logger.Error("Poll Type [{0}] is not supported!", CURRENT_POLL_TYPE)
|
||||
oPollResult = False
|
||||
End Select
|
||||
End If
|
||||
|
||||
If CURRENT_WORKMAIL_UID_LIST.Count() > 0 Or LocalEmail = True Then
|
||||
If LocalEmail Then
|
||||
Logger.Info("Working with local Mail")
|
||||
Dim oEmail As IMail = New MailBuilder().CreateFromEmlFile(CURRENT_DEBUG_LOCAL_EMAIL)
|
||||
ClassWorkMail.WorkEmailMessage(oEmail, 123456789)
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Else
|
||||
Try
|
||||
Logger.Info(String.Format("Pulled: [{0}] E-Mails", CURRENT_WORKMAIL_UID_LIST.Count()))
|
||||
Dim LIMIT_EMAIL_FROM As Boolean = False
|
||||
If EmailLimitationSender.Contains("@") Then
|
||||
LIMIT_EMAIL_FROM = True
|
||||
Logger.Info("####################################")
|
||||
Logger.Info($"LIMIT_EMAIL_FROM is ACTIVE - EMAIL_FROM = [{EmailLimitationSender}]")
|
||||
Logger.Info("####################################")
|
||||
Logger.Debug("####################################")
|
||||
Logger.Debug($"LIMIT_EMAIL_FROM is ACTIVE - EMAIL_FROM = [{EmailLimitationSender}]")
|
||||
Logger.Debug("####################################")
|
||||
End If
|
||||
For Each oUID In CURRENT_WORKMAIL_UID_LIST
|
||||
Dim oEmail As IMail = Fetcher.FetchMail(oUID)
|
||||
|
||||
If Not IsNothing(oEmail) Then
|
||||
If LIMIT_EMAIL_FROM Then
|
||||
Dim oEmailFrom As String = ""
|
||||
For Each m As MailBox In oEmail.From
|
||||
oEmailFrom = m.Address
|
||||
Next
|
||||
If oEmailFrom <> EmailLimitationSender Then
|
||||
Logger.Debug($"Skipping email {oEmailFrom} ...Subject [{oEmail.Subject}]")
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
If ClassWorkMail.WorkEmailMessage(oEmail, oUID) = True Then
|
||||
If CURRENT_DEBUG_LOCAL_EMAIL = "" Then
|
||||
EMAIL_DELETE()
|
||||
End If
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Info("### oEmail was nothing ###")
|
||||
End If
|
||||
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Next
|
||||
If CURRENT_POLL_TYPE = "IMAP" And CURRENT_WORKMAIL_UID_LIST.Count > 0 Then
|
||||
If Not IsNothing(Fetcher.Client) Then
|
||||
Fetcher.Client.Close()
|
||||
End If
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Logger.Warn($"Unexpected Error working CURRENT_WORKMAIL_UID_LIST: {ex.Message} ")
|
||||
End Try
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Debug(String.Format("No emails for profile!"))
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found!")
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_POLL_PROFILES SET LAST_TICK = GETDATE() WHERE GUID = " & oDR_Profile.Item("GUID").ToString)
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found! Check wether Email-Profile is active!")
|
||||
End If
|
||||
Next
|
||||
DeleteTempFiles()
|
||||
|
||||
Else
|
||||
Logger.Info("No active profiles")
|
||||
End If
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_CONFIG SET LAST_TICK = GETDATE() WHERE GUID = 1")
|
||||
If Database.DBInitialized = False Then
|
||||
Logger.Warn("Database is not initialized. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Logger.Debug("now windream_init... ")
|
||||
If UseWindream Then
|
||||
If ClassWindreamAllgemein.Init = False Then
|
||||
Logger.Info("windream could not be initialized!!")
|
||||
Exit Sub
|
||||
Else
|
||||
Logger.Debug("windream_initialized!")
|
||||
End If
|
||||
End If
|
||||
|
||||
Dim EmailAccountTable As DataTable = LoadEmailAccounts()
|
||||
Dim PollingProfileTable = LoadPollingProfiles(ProfileId)
|
||||
|
||||
If IsNothing(PollingProfileTable) Then
|
||||
Logger.Warn("Error while fetching Polling Profiles. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
If PollingProfileTable.Rows.Count = 0 Then
|
||||
Logger.Warn("No active Polling Profiles found. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Logger.Debug("Count of active profiles: " & PollingProfileTable.Rows.Count.ToString)
|
||||
|
||||
For Each oProfile As DataRow In PollingProfileTable.Rows
|
||||
|
||||
Dim oValidationSql = oProfile.ItemEx("VALIDATION_SQL", "")
|
||||
|
||||
CURRENT_PROFILE_GUID = oProfile.Item("GUID")
|
||||
DT_POLL_PROCESS = Nothing
|
||||
Dim sql = String.Format("SELECT * FROM TBEMLP_POLL_PROCESS WHERE PROFILE_ID = {0} AND ACTIVE = 1", CURRENT_PROFILE_GUID)
|
||||
DT_POLL_PROCESS = Database.GetDatatable(sql)
|
||||
If Not IsNothing(DT_POLL_PROCESS) Then
|
||||
If DT_POLL_PROCESS.Rows.Count = 0 Then
|
||||
Logger.Info("No processes configured for this Email-Profile - " & sql)
|
||||
Continue For
|
||||
Else
|
||||
DT_STEPS = Nothing
|
||||
DT_STEPS = Database.GetDatatable($"SELECT T.* FROM TBEMLP_POLL_STEPS T,TBEMLP_POLL_PROCESS T1 WHERE T.PROCESS_ID = T1.GUID AND T1.PROFILE_ID = {CURRENT_PROFILE_GUID} AND T1.ACTIVE = 1")
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("DT_POLL_PROCESS is nothing")
|
||||
Continue For
|
||||
End If
|
||||
|
||||
CURRENT_EMAIL_GUID = 0
|
||||
|
||||
CURRENT_POLL_TYPE = oProfile.Item("POLL_TYPE")
|
||||
Logger.Debug(String.Format("Working on profile: ({0}-{1}-{2}) ", oProfile.Item("GUID"), oProfile.Item("PROFILE_NAME"), CURRENT_POLL_TYPE))
|
||||
CURRENT_EMAIL_GUID = oProfile.Item("EMAIL_CONF_ID")
|
||||
|
||||
Dim FilteredRows As List(Of DataRow) = EmailAccountTable.
|
||||
Select($"GUID = {CURRENT_EMAIL_GUID}").
|
||||
ToList()
|
||||
Logger.Debug("FilteredRows: " & FilteredRows.Count)
|
||||
|
||||
If FilteredRows.Count = 1 Then
|
||||
Dim oRow = FilteredRows(0)
|
||||
Dim oMailFrom = oRow("EMAIL_FROM")
|
||||
Dim oMailServer = oRow("EMAIL_SMTP")
|
||||
Dim oMailUser = oRow("EMAIL_USER")
|
||||
Dim oMailPassword = oRow("EMAIL_PW")
|
||||
Dim oMailPortIn = oRow("PORT_IN")
|
||||
Dim oMailboxName = "Inbox"
|
||||
Dim oMailArchiveFolder = oRow("ARCHIVE_FOLDER")
|
||||
Dim oMailAuthType = oRow("AUTH_TYPE")
|
||||
|
||||
Logger.Debug("Mail Server: {0}", oMailServer)
|
||||
Logger.Debug("Mail From: {0}", oMailFrom)
|
||||
|
||||
Dim PWPlain = Encryption.DecryptData(oMailPassword)
|
||||
If Not IsNothing(PWPlain) Then
|
||||
If PWPlain <> "" Then
|
||||
oMailPassword = PWPlain
|
||||
Else
|
||||
Logger.Warn("PWPlain is string.empty - Could not decrypt passwort")
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("PWPlain is nothing - Could not decrypt passwort")
|
||||
End If
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST.Clear()
|
||||
|
||||
If oMailServer <> "" Then
|
||||
Dim oPollResult As Boolean = False
|
||||
If LocalEmail = True Then
|
||||
oPollResult = True
|
||||
Else
|
||||
Select Case CURRENT_POLL_TYPE
|
||||
Case "IMAP"
|
||||
' We are using 'Archive Folder' as an additional field to save the Tenant-ID for O365-OAuth2
|
||||
oPollResult = FetchMessages(oMailServer, oMailPortIn, oMailUser, oMailPassword, oMailAuthType, oMailArchiveFolder)
|
||||
|
||||
Case Else
|
||||
Logger.Error("Poll Type [{0}] is not supported!", CURRENT_POLL_TYPE)
|
||||
oPollResult = False
|
||||
End Select
|
||||
End If
|
||||
|
||||
If CURRENT_WORKMAIL_UID_LIST.Count() > 0 Or LocalEmail = True Then
|
||||
If LocalEmail Then
|
||||
Logger.Info("Working with local Mail")
|
||||
Dim oEmail As IMail = New MailBuilder().CreateFromEmlFile(LocalEmlFile)
|
||||
ClassWorkMail.WorkEmailMessage(oEmail, 123456789, oValidationSql)
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Else
|
||||
Try
|
||||
Logger.Info(String.Format("Pulled: [{0}] E-Mails", CURRENT_WORKMAIL_UID_LIST.Count()))
|
||||
|
||||
For Each oMailId In CURRENT_WORKMAIL_UID_LIST
|
||||
Dim oEmail As IMail = Fetcher.FetchMail(oMailId)
|
||||
|
||||
If Not IsNothing(oEmail) Then
|
||||
If EmailLimitationEnabled Then
|
||||
Dim oEmailFrom As String = ""
|
||||
For Each m As MailBox In oEmail.From
|
||||
oEmailFrom = m.Address
|
||||
Next
|
||||
If oEmailFrom <> EmailLimitationSender Then
|
||||
Logger.Debug($"Skipping email {oEmailFrom} ...Subject [{oEmail.Subject}]")
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
If ClassWorkMail.WorkEmailMessage(oEmail, oMailId, oValidationSql) = True Then
|
||||
If LocalEmlFile = "" Then
|
||||
DeleteEmailFile(oMailId)
|
||||
End If
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Info("### oEmail was nothing ###")
|
||||
End If
|
||||
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Next
|
||||
|
||||
If CURRENT_POLL_TYPE = "IMAP" And CURRENT_WORKMAIL_UID_LIST.Count > 0 Then
|
||||
Fetcher.Disconnect()
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Logger.Warn($"Unexpected Error working CURRENT_WORKMAIL_UID_LIST: {ex.Message} ")
|
||||
End Try
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Debug(String.Format("No emails for profile!"))
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found!")
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_POLL_PROFILES SET LAST_TICK = GETDATE() WHERE GUID = " & oProfile.Item("GUID").ToString)
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found! Check wether Email-Profile is active!")
|
||||
End If
|
||||
Next
|
||||
|
||||
DeleteTempFiles()
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_CONFIG SET LAST_TICK = GETDATE() WHERE GUID = 1")
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
End Try
|
||||
@@ -280,27 +298,26 @@ Public Class clsWorker
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST = oMailIds
|
||||
|
||||
Fetcher.Disconnect()
|
||||
|
||||
Return True
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
Private Sub EMAIL_DELETE()
|
||||
Private Sub DeleteEmailFile(pMailId As Integer)
|
||||
Try
|
||||
If DeleteMail = True And MessageError = False Then
|
||||
If DeleteMail = True And MESSAGE_ERROR = False Then
|
||||
If IsNothing(Fetcher.Client) Then
|
||||
Logger.Warn("EMAIL_DELETE - CURRENT_ImapObject is nothing")
|
||||
End If
|
||||
|
||||
Fetcher.Client.DeleteMessageByUID(CURRENT_MAIL_UID)
|
||||
Logger.Info("Email with Id [{0}] was deleted.", CURRENT_MAIL_UID)
|
||||
Fetcher.Client.DeleteMessageByUID(pMailId)
|
||||
Logger.Info("Email with Id [{0}] was deleted.", pMailId)
|
||||
Else
|
||||
If MessageError = True Then
|
||||
Logger.Warn("Did not delete Message with UID [{0}] as there was an MessageError!", CURRENT_MAIL_UID)
|
||||
If MESSAGE_ERROR = True Then
|
||||
Logger.Warn("Did not delete Message with UID [{0}] as there was an MessageError!", pMailId)
|
||||
End If
|
||||
End If
|
||||
Catch ex As Exception
|
||||
|
||||
Reference in New Issue
Block a user