Imports System.IO Imports System.Text.RegularExpressions Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports Independentsoft.Email.Mime Public Class Form1 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() MyLogger.Debug = True Dim dbResult As Boolean If My.Settings.DD_ECM_CONSTRING = String.Empty Then MsgBox("No Databaseconnection configured. (First Start or Appdata not accessible)" & vbNewLine & "Basic-Config will be loaded.", MsgBoxStyle.Information) Exit Sub End If MyDatabase = New MSSQLServer(MyLogger, My.Settings.DD_ECM_CONSTRING) If MyDatabase.DBInitialized = True Then dbResult = True Else dbResult = False End If bsitemppath.Caption = My.Settings.Temppath bsifolderbrowser.Caption = My.Settings.CURR_CHECK_PATH Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in FormLoad") End Try End Sub Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick With FolderBrowserDialog1 .SelectedPath = My.Settings.CURR_CHECK_PATH End With If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then bsifolderbrowser.Caption = FolderBrowserDialog1.SelectedPath My.Settings.CURR_CHECK_PATH = bsifolderbrowser.Caption My.Settings.Save() End If End Sub Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick With FolderBrowserDialog1 .SelectedPath = My.Settings.Temppath End With If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then bsitemppath.Caption = FolderBrowserDialog1.SelectedPath My.Settings.Temppath = FolderBrowserDialog1.SelectedPath My.Settings.Save() End If End Sub Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick ListBox1.Items.Clear() Dim di As New DirectoryInfo(bsifolderbrowser.Caption) ' Get a reference to each file in that directory. Dim fiArr As FileInfo() = di.GetFiles() ' Display the names of the files. Dim fri As FileInfo For Each fri In fiArr Try If Not fri.Name.EndsWith("eml") Then Continue For End If Dim oList As New List(Of String) oList.Add(fri.FullName) Dim oMessage As New Message(fri.FullName) Dim oMSGID = oMessage.MessageID oMSGID = oMSGID.Replace(">", "").Replace("<", "") ListBox1.Items.Add($"Working on email from: {oMessage.From.EmailAddress}...Subject: {oMessage.Subject}") Dim oCount As Integer = 0 For Each oAttachment As Attachment In oMessage.GetAttachments Dim oString As String Dim oATTFilename = oAttachment.GetFileName.ToString.ToLower Dim oValidExt As Boolean = False If oATTFilename.EndsWith("pdf") Then oValidExt = True ElseIf oATTFilename.EndsWith("xls") Then oValidExt = True ElseIf oATTFilename.EndsWith("xlsx") Then oValidExt = True ElseIf oATTFilename.EndsWith("doc") Then oValidExt = True ElseIf oATTFilename.EndsWith("docx") Then oValidExt = True ElseIf oATTFilename.EndsWith("ppt") Then oValidExt = True ElseIf oATTFilename.EndsWith("pptx") Then oValidExt = True End If If oValidExt = False Then Continue For End If oCount += 1 If oCount = 1 Then Continue For End If Dim oAttachmentFileString oString = String.Format(" Working on Attachment [{0}]", oAttachment.GetFileName) Logger.Info(oString) ListBox1.Items.Add(oString) Try Dim oFilename = oAttachment.GetFileName oFilename = CleanInput(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}]") 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) ListBox1.Items.Add(oString) If System.IO.File.Exists(oAttachmentFileString) = False Then 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)) Else Logger.Warn($"##!! oFileLenth for AttachmentObjects is 0 !!##") Try File.Delete(oAttachmentFileString) Catch ex As Exception Logger.Error(ex) End Try End If Catch ex As Exception 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") End If Else oString = " ALL OK! EXISTING" 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}") End Try oMessage = Nothing Next Catch ex As Exception Logger.Error(ex) End Try Next fri End Sub Private Function CleanInput(strIn As String) As String ' Replace invalid characters with empty strings. Try Return Regex.Replace(strIn, "[^\w\.@-]", "") ' If we timeout when replacing invalid characters, ' we should return String.Empty. Catch ex As Exception Logger.Error(ex) Return String.Empty End Try End Function End Class