EMailProfiler.Common: Neue Extract Attachments-Logik & InfoMail Logik

This commit is contained in:
2024-09-25 16:08:51 +02:00
parent b18a6a3045
commit c91a7b82c7
9 changed files with 549 additions and 81 deletions

View File

@@ -1,17 +1,23 @@
Imports System.IO
Imports System.ComponentModel
Imports System.IO
Imports System.Net.Mail
Imports System.Text.RegularExpressions
Imports DigitalData.Modules
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports GdPicture14
Imports Independentsoft.Email.Mime
Imports Attachment = Independentsoft.Email.Mime.Attachment
Imports LicenseManager = GdPicture14.LicenseManager
Public Class Form1
Private Logger As DigitalData.Modules.Logging.Logger
Private _logger As DigitalData.Modules.Logging.Logger
Private Shared MyLogger As LogConfig
Private _Worklist As List(Of String)
Private MyDatabase As MSSQLServer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"), Nothing, My.Application.Info.CompanyName, My.Application.Info.ProductName)
Logger = MyLogger.GetLogger()
_logger = MyLogger.GetLogger()
MyLogger.Debug = True
Dim dbResult As Boolean
@@ -108,66 +114,66 @@ Public Class Form1
End If
Dim oAttachmentFileString
oString = String.Format(" Working on Attachment [{0}]", oAttachment.GetFileName)
Logger.Info(oString)
_logger.Info(oString)
ListBox1.Items.Add(oString)
Try
Dim oFilename = oAttachment.GetFileName
oFilename = CleanInput(oFilename)
Logger.Debug($"oFilename [{oFilename}]")
_logger.Debug($"oFilename [{oFilename}]")
If oFilename = String.Empty Then
oFilename = oAttachment.GetFileName
End If
Dim oAttFilenameonly = $"{oMSGID}~{oFilename}"
oAttachmentFileString = Path.Combine(bsitemppath.Caption, oAttFilenameonly)
Logger.Debug($"oAttachmentFileString [{oAttachmentFileString}]")
_logger.Debug($"oAttachmentFileString [{oAttachmentFileString}]")
Dim oSQL = $"SELECT * FROM IDB.DBO.TBIDB_DOC_INFO WHERE FILENAME_EXT = '{oAttFilenameonly}'"
Dim oDT As DataTable = MyDatabase.GetDatatable(oSQL)
If Not IsNothing(oDT) Then
If oDT.Rows.Count = 0 Then
oString = " ## Attachment NOT EXISTING ##"
Logger.Info(oString)
_logger.Info(oString)
ListBox1.Items.Add(oString)
If System.IO.File.Exists(oAttachmentFileString) = False Then
Logger.Debug(String.Format("Trying to save attachment [{0}]", oAttachmentFileString))
_logger.Debug(String.Format("Trying to save attachment [{0}]", oAttachmentFileString))
Try
oAttachment.Save(oAttachmentFileString)
Dim oFileInfo As New FileInfo(oAttachmentFileString)
Dim oFileLenth As Long = oFileInfo.Length
If oFileLenth > 0 Then
Logger.Info(String.Format(" Attachment saved to [{0}]", oAttachmentFileString))
_logger.Info(String.Format(" Attachment saved to [{0}]", oAttachmentFileString))
Else
Logger.Warn($"##!! oFileLenth for AttachmentObjects is 0 !!##")
_logger.Warn($"##!! oFileLenth for AttachmentObjects is 0 !!##")
Try
File.Delete(oAttachmentFileString)
Catch ex As Exception
Logger.Error(ex)
_logger.Error(ex)
End Try
End If
Catch ex As Exception
Logger.Warn($"Error while saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFileString}")
_logger.Warn($"Error while saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFileString}")
End Try
Else
ListBox1.Items.Add("EXATTMNT - Attachment (" & oAttachmentFileString & ") already existing!")
Logger.Info("EXATTMNT - Attachment (" & oAttachmentFileString & ") already existing!", False, "EXTRACT_ATTACHMENTS")
_logger.Info("EXATTMNT - Attachment (" & oAttachmentFileString & ") already existing!", False, "EXTRACT_ATTACHMENTS")
End If
Else
oString = " ALL OK! EXISTING"
Logger.Info(oString)
_logger.Info(oString)
ListBox1.Items.Add(oString)
End If
End If
Catch ex As Exception
Logger.Warn($"Error while creating and saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFileString}")
_logger.Warn($"Error while creating and saving attachment-name: {ex.Message} - AttachmentName: {oAttachmentFileString}")
End Try
oMessage = Nothing
Next
Catch ex As Exception
Logger.Error(ex)
_logger.Error(ex)
End Try
@@ -180,10 +186,86 @@ Public Class Form1
' If we timeout when replacing invalid characters,
' we should return String.Empty.
Catch ex As Exception
Logger.Error(ex)
_logger.Error(ex)
Return String.Empty
End Try
End Function
Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
' FileOpen Dialog
Dim fileOpenDlg = OpenFileDialog1
If fileOpenDlg.ShowDialog() = DialogResult.OK Then
Dim oFilename = fileOpenDlg.FileName
' Call Extract Method
ExtractEmbeddedPDFAttachments(oFilename)
End If
End Sub
Private Sub ExtractEmbeddedPDFAttachments(pFilename As String)
Try
Dim oSQLGDPicture = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE ACTIVE = 1"
Dim GDPictureLicense As String = "21182889975216572111813147150675976632"
Dim oValidExtensions = New List(Of String) From {"pdf", "xls", "xlsx", "doc", "docx", "ppt", "pptx"}
Dim oGraphicExtensions = New List(Of String) From {"jpg", "bmp", "jpeg", "gif", "png", "xml"}
Dim oFileInfo As New FileInfo(pFilename)
If oFileInfo.Extension.ToLower = ".pdf" Then
Dim oPDFConsistent As Boolean = True
Dim oLicenseManager As New LicenseManager
oLicenseManager.RegisterKEY(GDPictureLicense)
Dim oGdPicturePDF As New GdPicturePDF()
' Lic
Dim oStatus As GdPictureStatus = oGdPicturePDF.LoadFromFile(pFilename, True)
If oStatus <> GdPictureStatus.OK Then
oPDFConsistent = False
Dim oResult = $"PDF-Consistency of attached file {pFilename} is not ok. ({oStatus.ToString})"
Else
Dim embeddedFileCount As Integer = oGdPicturePDF.GetEmbeddedFileCount()
If embeddedFileCount > 0 Then
For i As Integer = 0 To embeddedFileCount - 1
Dim oEmbAttName As String = oGdPicturePDF.GetEmbeddedFileName(i)
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(oEmbAttName)
If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
Dim FileSize As Integer = oGdPicturePDF.GetEmbeddedFileSize(i)
Dim oValidExt = oValidExtensions.Any(Function(ext) oEmbAttName.EndsWith(ext))
If oValidExt = False Then
_Logger.Info("Invalid FileExtension of embedded file [{0}]", oEmbAttName)
Dim GraphicExt = oGraphicExtensions.Any(Function(ext) oEmbAttName.EndsWith(ext))
If GraphicExt = False Then
Dim oInfo = $"Consistency or extension of attached file [{oEmbAttName}] is not ok."
End If
Continue For
End If
If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
Dim FileData As Byte() = New Byte(FileSize) {}
Dim status As GdPictureStatus = oGdPicturePDF.ExtractEmbeddedFile(0, FileData)
If status <> GdPictureStatus.OK Or FileSize = 0 Then
oPDFConsistent = False
Dim oResult = $"Consistency or PDF-State of embedded file [{oEmbAttName}] is not ok."
Continue For
End If
End If
End If
Next
End If
End If
End If
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
End Class