WIP Email Validation

This commit is contained in:
Jonathan Jenne
2023-09-19 11:39:27 +02:00
parent e457b8586b
commit 8b5f6f0635
17 changed files with 760 additions and 619 deletions

View File

@@ -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