diff --git a/App/EmailProfiler.Common/ClassCurrent.vb b/App/EmailProfiler.Common/ClassCurrent.vb index a15e9c7..c055713 100644 --- a/App/EmailProfiler.Common/ClassCurrent.vb +++ b/App/EmailProfiler.Common/ClassCurrent.vb @@ -10,8 +10,6 @@ Public Class ClassCurrent Public Shared Property CURRENT_EMAIL_GUID As Integer Public Shared Property CURRENT_PROFILE_GUID As Integer - Public Shared Property DTCONFIG As DataTable - Public Shared Property DT_ECM_BASE_CONFIG As DataTable Public Shared Property DT_POLL_PROCESS As DataTable Public Shared Property DT_STEPS As DataTable @@ -35,7 +33,6 @@ Public Class ClassCurrent Public Shared Property CURRENT_POLL_TYPE As String Public Shared Property POLL_STEP_GUID As Integer - Public Shared Property POLL_STEP_PROCESS_ID As Integer Public Shared Property POLL_KEYWORDS As String Public Shared Property KEYWORDS_SPLIT As String() diff --git a/App/EmailProfiler.Common/MailContainer.vb b/App/EmailProfiler.Common/MailContainer.vb index 288cd70..78b9b7a 100644 --- a/App/EmailProfiler.Common/MailContainer.vb +++ b/App/EmailProfiler.Common/MailContainer.vb @@ -24,6 +24,7 @@ Public Class MailContainer ''' The subject, truncated to SUBJECT_MAX_LENGTH characters ''' Public ReadOnly Property Subject As String + Public ReadOnly Property SubjectOriginal As String Public ReadOnly Property SenderDomain As String Public ReadOnly Property SenderAddress As String @@ -34,9 +35,10 @@ Public Class MailContainer ImapId = pImapId MessageIdOriginal = pMail.MessageID - MessageId = StringEx.GetHash(pMail.MessageID) + MessageId = StringEx.GetShortHash(pMail.MessageID) Subject = ObjectEx.NotNull(pMail.Subject.Truncate(SUBJECT_MAX_LENGTH), String.Empty) + SubjectOriginal = ObjectEx.NotNull(pMail.Subject, String.Empty) SenderAddress = GetSenderAddress(pMail) SenderDomain = GetSenderDomain(pMail) diff --git a/App/EmailProfiler.Common/clsWorkEmail.vb b/App/EmailProfiler.Common/clsWorkEmail.vb index 29e5915..bfe7118 100644 --- a/App/EmailProfiler.Common/clsWorkEmail.vb +++ b/App/EmailProfiler.Common/clsWorkEmail.vb @@ -30,7 +30,7 @@ Public Class clsWorkEmail ''' Primary Mail Identifier. ''' Is a hash of the MessageId, used to be the MessageId itself. ''' - Private Property CURRENT_MAIL_MESSAGE_ID As String = "" + 'Private Property CURRENT_MAIL_MESSAGE_ID As String = "" Private CurrentMailProcessName As String @@ -69,11 +69,6 @@ Public Class clsWorkEmail _Logger.Error(ex) End Try End Sub - Public Shared Function RemoveIllegalFileNameChars(input As String, Optional replacement As String = "") As String - Dim regexSearch = New String(Path.GetInvalidFileNameChars()) & New String(Path.GetInvalidPathChars()) - Dim r = New Regex(String.Format("[{0}]", Regex.Escape(regexSearch))) - Return r.Replace(input, replacement) - End Function Public Function WorkEmailMessage(pMailMessage As IMail, poUID As Long) As Boolean Try For Each m As MailBox In pMailMessage.From @@ -99,7 +94,7 @@ Public Class clsWorkEmail ' 28.07.23 ' The SHA256 Hash is now truncated to half the size ' which should be a good balance between uniqueness and length - CURRENT_MAIL_MESSAGE_ID = StringEx.GetShortHash(pMailMessage.MessageID) + 'CURRENT_MAIL_MESSAGE_ID = StringEx.GetShortHash(pMailMessage.MessageID) 'If String.IsNullOrEmpty(CURRENT_MAIL_MESSAGE_ID) Then ' CURRENT_MAIL_MESSAGE_ID = Guid.NewGuid.ToString() @@ -125,7 +120,7 @@ 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 = '{CURRENT_MAIL_MESSAGE_ID}'" + 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 @@ -157,7 +152,7 @@ Public Class clsWorkEmail If CURRENT_ATTMT_COUNT = 0 Then _Logger.Info("### Mail contained no Attachments!! ###") Dim oBody = EmailStrings.EMAIL_NO_FERDS - If AddToEmailQueueMSSQL(CURRENT_MAIL_MESSAGE_ID, oBody, "No Attachments", _EmailAccountID) = True Then + If AddToEmailQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then CURRENT_ImapObject.DeleteMessageByUID(poUID) End If End If @@ -419,6 +414,7 @@ Public Class clsWorkEmail Private Function SAVE2TEMP(pCurrentMail As MailContainer) As Boolean Dim oTempFilename As String + Try Dim oTempPath As String = Path.Combine(Path.GetTempPath, "DD_EmailProfiler") _Logger.Debug($"oTempPath is: {oTempPath} ...") @@ -439,24 +435,12 @@ Public Class clsWorkEmail ' Subject can be SUBJECT_MAX_LENGTH chars at most, ' otherwise we run into errors with the path being too long 'Dim oSubjectFilename = CURRENT_MAIL_MESSAGE.Subject.Truncate(SUBJECT_MAX_LENGTH) & ".eml" - Dim oSubjectFilename = CURRENT_MAIL_MESSAGE_ID & ".eml" + Dim oSubjectFilename = pCurrentMail.MessageId & ".eml" - _Logger.Debug($"Filename (beforeclean) is: {oSubjectFilename}") - oSubjectFilename = RemoveIllegalFileNameChars(oSubjectFilename) + oSubjectFilename = StringEx.RemoveInvalidCharacters(oSubjectFilename) oTempFilename = Path.Combine(oTempPath, oSubjectFilename) - _Logger.Debug($"Filepath (afterclean) is: {oTempFilename}") + _Logger.Debug($"Filepath is: {oTempFilename}") - - 'If File.Exists(oTempFilename) = True Then - ' Do While File.Exists(oTempFilename) - ' oCounter += 1 - - ' oTempFilename = Path.Combine(oTempPath, oCounter & "_" & CURRENT_MAIL_MESSAGE.Subject.Replace(" ", "") & ".eml") - ' oTempFilename = String.Join("", oTempFilename.Split(Path.GetInvalidPathChars())) - ' oTempFilename = oTempFilename.Replace("/", "") - ' oTempFilename = oTempFilename.Replace("\", "") - ' Loop - 'End If pCurrentMail.Mail.Save(oTempFilename) CurrentTempMailPath = oTempFilename @@ -513,7 +497,7 @@ Public Class clsWorkEmail ' 'oTempFilename &= "\" & CURRENT_MAIL_MESSAGE.Subject.Replace(" ", "") & ".eml" 'End If - oTempFilename = Path.Combine(oTempFilename, $"{CURRENT_MAIL_MESSAGE_ID}.eml") + oTempFilename = Path.Combine(oTempFilename, $"{pCurrentMail.MessageId}.eml") 'Dim cleanPath As String = String.Join("", oTempFilename.Split(Path.GetInvalidPathChars())) If File.Exists(oTempFilename) = False Then @@ -557,7 +541,7 @@ Public Class clsWorkEmail End Try End Function Private Function EXTRACT_BODY(pCurrentMail As MailContainer) - Dim oDTFunctionRegex As DataTable = _DB_MSSQL.GetDatatable("SELECT * FROM TBDD_FUNCTION_REGEX WHERE UPPER(FUNCTION_NAME) IN (UPPER('EMAIL_PROFILER - RemoveHTMLText'),UPPER('EMAIL_PROFILER - RemoveHTMLText1'))") + Dim oTable As DataTable = _DB_MSSQL.GetDatatable("SELECT * FROM TBDD_FUNCTION_REGEX WHERE UPPER(FUNCTION_NAME) IN (UPPER('EMAIL_PROFILER - RemoveHTMLText'),UPPER('EMAIL_PROFILER - RemoveHTMLText1'))") Dim oBodyText As String = "" If Not IsNothing(pCurrentMail.Mail.Text) Then @@ -565,16 +549,18 @@ Public Class clsWorkEmail End If If Not IsNothing(CURRENT_MAIL_BODY_ALL) Then + Dim oRow = oTable.Rows.Item(0) + ' CURRENT_MAIL_BODY_ALL = oMsg_email.Body Dim oPattern1 As String Dim oPattern2 As String Try - oPattern1 = oDTFunctionRegex.Rows(0).Item("REGEX") + oPattern1 = oTable.Rows(0).Item("REGEX") Catch ex As Exception oPattern1 = "" End Try Try - oPattern2 = oDTFunctionRegex.Rows(1).Item("REGEX") + oPattern2 = oTable.Rows(1).Item("REGEX") Catch ex As Exception oPattern2 = "" End Try @@ -641,13 +627,14 @@ Public Class clsWorkEmail End If Next - _Logger.Debug(String.Format("MailBody-ANSWER1:...[{0}]", CURRENT_MAIL_BODY_ANSWER1)) _Logger.Debug(String.Format("MailBody-ANSWER2:...[{0}]", CURRENT_MAIL_BODY_Substr2)) If CURRENT_MAIL_BODY_ANSWER1 = String.Empty Then _Logger.Warn("CURRENT_MAIL_BODY_ANSWER1 is String.Empty: So the answer will interpreted as empty!") End If + + Return True Catch ex As Exception _Logger.Error(ex) 'clsLogger.Add("Unexpected Error in COPY2HDD: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID, True) @@ -701,7 +688,7 @@ Public Class clsWorkEmail Dim oFilename = StringEx.ConvertTextToSlug(oFilenameWithoutExtension) & oFileInfo.Extension 'Dim oAttachmentFileName = $"{CURRENT_MAIL_MESSAGE_ID}~{pCurrentMail.SenderDomain}~{oFilename}" - Dim oAttachmentFileName = $"{CURRENT_MAIL_MESSAGE_ID}~Attachment{oAttachmentCount}{oFileInfo.Extension}" + Dim oAttachmentFileName = $"{pCurrentMail.MessageId}~Attm{oAttachmentCount}{oFileInfo.Extension}" _Logger.Debug("Final Filename for Attachment: [{0}]", oAttachmentFileName) @@ -718,7 +705,7 @@ Public Class clsWorkEmail If oFileLenth > 2 Then _Logger.Info(String.Format("Attachment saved to [{0}]", oAttachmentFilePath)) 'INSERT_HISTORY_FB(CURRENT_MAIL_MESSAGE_ID, oAttachment.SafeFileName) - InsertAttachmentHistoryEntry(pCurrentMail, CURRENT_MAIL_MESSAGE_ID, oAttachment.SafeFileName) + InsertAttachmentHistoryEntry(pCurrentMail, pCurrentMail.MessageId, oAttachment.SafeFileName) oAttachmentCount += 1 Else _Logger.Warn($"##!! oFileLenth for AttachmentObjects is <2 !!##") @@ -767,16 +754,27 @@ Public Class clsWorkEmail Private Function InsertHistoryEntry(pCurrentMail As MailContainer) As Boolean If MessageError = False Then - Dim ins = $"INSERT INTO TBEMLP_HISTORY (WORK_PROCESS,EMAIL_MSGID,EMAIL_SUBJECT,EMAIL_DATE,EMAIL_BODY,EMAIL_SUBSTRING1,EMAIL_SUBSTRING2,EMAIL_FROM,PROFILE_ID) VALUES " & - $"('{CurrentMailProcessName}'," & - $"'{CURRENT_MAIL_MESSAGE_ID}'," & - $"'{CURRENT_MAIL_SUBJECT}'," & - $"'{pCurrentMail.Mail.Date}'," & - $"'{CURRENT_MAIL_BODY_ALL}'," & - $"'{CURRENT_MAIL_BODY_ANSWER1}'," & - $"'{CURRENT_MAIL_BODY_Substr2}'," & - $"'{CURRENT_MAIL_FROM}'," & - $"{CURRENT_PROFILE_GUID})" + Dim ins = $"INSERT INTO TBEMLP_HISTORY ( + WORK_PROCESS, + EMAIL_MSGID, + EMAIL_SUBJECT, + EMAIL_DATE, + EMAIL_BODY, + EMAIL_SUBSTRING1, + EMAIL_SUBSTRING2, + EMAIL_FROM, + PROFILE_ID + ) VALUES ( + '{CurrentMailProcessName}', + '{pCurrentMail.MessageId}', + '{pCurrentMail.SubjectOriginal}', + '{pCurrentMail.Mail.Date}', + '{CURRENT_MAIL_BODY_ALL}', + '{CURRENT_MAIL_BODY_ANSWER1}', + '{CURRENT_MAIL_BODY_Substr2}', + '{pCurrentMail.SenderAddress}', + {CURRENT_PROFILE_GUID} + )" Return _DB_MSSQL.ExecuteNonQuery(ins) Else _Logger.Info("! No INSERT_HISTORY as MessageError = True") @@ -794,8 +792,8 @@ Public Class clsWorkEmail Dim ins = $"INSERT INTO TBEMLP_HISTORY_ATTACHMENT (WORK_PROCESS,EMAIL_MSGID,EMAIL_FROM,EMAIL_SUBJECT,EMAIL_DATETIME,EMAIL_BODY,EMAIL_ATTMT) VALUES " & $"('{CurrentMailProcessName}'," & $"'{pMessageId}'," & - $"'{CURRENT_MAIL_FROM}'," & - $"'{CURRENT_MAIL_SUBJECT}'," & + $"'{pCurrentMail.SenderAddress}'," & + $"'{pCurrentMail.SubjectOriginal}'," & $"'{pCurrentMail.Mail.Date}'," & $"'{CURRENT_MAIL_BODY_ALL}'," & $"'{pFileName}')" @@ -815,7 +813,6 @@ Public Class clsWorkEmail _worked_email = False For Each row As DataRow In DT_STEPS.Rows POLL_STEP_GUID = row.Item("GUID") - POLL_STEP_PROCESS_ID = row.Item("PROCESS_ID") POLL_KEYWORDS = row.Item("KEYWORDS_BODY") KEYWORDS_SPLIT = POLL_KEYWORDS.Split(";") diff --git a/App/EmailProfiler.Service/MyService.vb b/App/EmailProfiler.Service/MyService.vb index 8d0e175..3501a9e 100644 --- a/App/EmailProfiler.Service/MyService.vb +++ b/App/EmailProfiler.Service/MyService.vb @@ -109,10 +109,9 @@ Public Class MyService End Try End Sub Protected Overrides Sub OnStop() - ' Hier Code zum Ausführen erforderlicher Löschvorgänge zum Beenden des Dienstes einfügen. Logger.Info("## Service was stopped manually. ##") End Sub - Private Sub Worker_Completed(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) 'Handles threadDateiimport.RunWorkerCompleted + Private Sub Worker_Completed(sender As Object, e As RunWorkerCompletedEventArgs) 'This event fires when the DoWork event completes Try Dim result As String = ""