MS Release CleanFileName
This commit is contained in:
@@ -8,155 +8,46 @@ Imports System.Net
|
||||
Imports System.Reflection
|
||||
Imports System.IO
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Messaging
|
||||
|
||||
Public Class clsEmailIMAP
|
||||
Private Shared Logger As DigitalData.Modules.Logging.Logger
|
||||
Private Shared LogConfig As DigitalData.Modules.Logging.LogConfig
|
||||
Private _DB_MSSQL As clsDatabase
|
||||
Private _limilab As DigitalData.Modules.Messaging.Limilab
|
||||
Sub New(LogConf As LogConfig, ECMConnectionString As String)
|
||||
LogConfig = LogConf
|
||||
Logger = LogConf.GetLogger
|
||||
_DB_MSSQL = New clsDatabase(LogConf, ECMConnectionString)
|
||||
_limilab = New Limilab(LogConf)
|
||||
End Sub
|
||||
'Private Shared Sub OnWriteLog(ByVal sender As Object, ByVal e As WriteLogEventArgs)
|
||||
' Logger.Info(e.Log)
|
||||
'End Sub
|
||||
|
||||
Public Function FetchIMAPMessagesS22(Server As String, Port As Integer, Username As String, Password As String, pInbox As String, Optional IsTest As Boolean = False, Optional DeleteinTest As Boolean = False, Optional MoveMailTo As String = "") As Boolean
|
||||
Logger.Debug("FetchIMAPMessagesS22 - Connecting to Server {0}:{1} with user {2}", Server, Port, Username)
|
||||
Public Function FetchIMAPMessagesLimilab(Server As String, Port As Integer, Username As String, Password As String, AuthType As String) As Boolean
|
||||
Try
|
||||
Logger.Debug("FetchIMAPMessagesS22 - Connecting...")
|
||||
Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
|
||||
If Not oClient.Authed Then
|
||||
Logger.Warn("FetchIMAPMessagesS22 - Connected to server but authentication failed.")
|
||||
Return False
|
||||
Logger.Debug("FetchIMAPMessagesLimilab - Fetching messages...")
|
||||
_limilab.InitIMAP(True, Server, Port, Username, Password, AuthType)
|
||||
Dim oListuids As New List(Of Long)
|
||||
oListuids = _limilab.IMAPGetMessageIDs_AllMails()
|
||||
If IsNothing(oListuids) Then
|
||||
Return False
|
||||
Else
|
||||
If oListuids.Count > 0 Then
|
||||
CURRENT_WORKMAIL_UID_LIST = oListuids
|
||||
Logger.Info("FetchIMAPMessagesLimilab - Found [{0}] messages", oListuids.Count)
|
||||
CURRENT_ImapObject = _limilab.CurrentImapObject
|
||||
Else
|
||||
Logger.Debug("FetchIMAPMessagesLimilab - No Emails found!")
|
||||
End If
|
||||
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - Fetching unseen MessageIds from Inbox: {pInbox}")
|
||||
Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, pInbox)
|
||||
If oMessageIds.Count > 0 Then
|
||||
Logger.Info("FetchIMAPMessagesS22 - Found [{0}] messages", oMessageIds.Count)
|
||||
Logger.Debug("FetchIMAPMessagesS22 - Fetching messages...")
|
||||
End If
|
||||
|
||||
Dim oMessageCountRegular As Integer = 0
|
||||
Dim oMessageCountWorked As Integer = 0
|
||||
Dim oLastLog As String
|
||||
For Each oMessageId As UInteger In oMessageIds
|
||||
Logger.Debug($"Checking message...")
|
||||
Dim oMessage = oClient.GetMessage(oMessageId, False, pInbox)
|
||||
oLastLog = $"Checking message with Subject [{oMessage.Subject}] From [{oMessage.From}]"
|
||||
Logger.Debug(oLastLog)
|
||||
Dim oTempPath = Path.GetTempFileName()
|
||||
Try
|
||||
Dim oResult = WriteMessageToFile(oMessage, oTempPath)
|
||||
oLastLog &= $" # Message written to TempPath [{oTempPath}]"
|
||||
Dim oMessageREFGUID
|
||||
Dim oMsg As Message
|
||||
Try
|
||||
oLastLog &= " # Creating the New Message(oTempPath)"
|
||||
oMsg = New Message(oTempPath)
|
||||
oLastLog &= " # Extracting the MessageID"
|
||||
oMessageREFGUID = oMsg.MessageID
|
||||
Catch ex As Exception
|
||||
Logger.Warn($"FetchIMAPMessagesS22 - Could not get a MessageID or create a MailObject - Error: {ex.Message} - Last Debug Log: [{oLastLog}]")
|
||||
Continue For
|
||||
End Try
|
||||
|
||||
oLastLog &= " # Got the MessageID"
|
||||
oMessageREFGUID = oMessageREFGUID.Replace(">", "").Replace("<", "")
|
||||
Dim oCHECKSQL = $"SELECT * FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{oMessageREFGUID}'"
|
||||
Dim oCHECKDT As DataTable = _DB_MSSQL.Return_Datatable(oCHECKSQL)
|
||||
If Not IsNothing(oCHECKDT) Then
|
||||
If oCHECKDT.Rows.Count = 0 Then
|
||||
oMessageCountRegular += 1
|
||||
CURRENT_WORKMAIL_LIST.Add(oMsg)
|
||||
oLastLog &= " # Added to CURRENT_WORKMAIL_LIST"
|
||||
Else
|
||||
Logger.Info("FetchIMAPMessagesS22 - Message has already been worked! Skipping!")
|
||||
Logger.Debug($"Message shall be deleted...")
|
||||
oLastLog &= " # Message shall be deleted..."
|
||||
oClient.DeleteMessage(oMessageId)
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - Message has been deleted!")
|
||||
oLastLog &= " # FetchIMAPMessagesS22 - Message has been deleted!"
|
||||
Dim oUpd = $"UPDATE TBEMLP_HISTORY SET DATE_DELETED_INBOX = GETDATE() WHERE EMAIL_MSGID = '{oMessageId}'"
|
||||
_DB_MSSQL.Execute_non_Query(oUpd)
|
||||
oMessageCountWorked += 1
|
||||
End If
|
||||
If IsTest = True Then
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - IMAP-Test Message#: {oMessageCountRegular} - Msgsubject is: {oMsg.Subject} - MsgMessageID is: {oMessageREFGUID}")
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - message correctly fetched. Mail has been downloaded to {oTempPath}")
|
||||
End If
|
||||
Try
|
||||
If DeleteinTest = True Then
|
||||
Logger.Debug($"Message shall be deleted...")
|
||||
oClient.DeleteMessage(oMessageId,)
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - Message has been deleted!")
|
||||
End If
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Message could not be deleted: " & ex.Message)
|
||||
End Try
|
||||
If MoveMailTo <> String.Empty Then
|
||||
Try
|
||||
Logger.Debug($"Moving to [{MoveMailTo}] is active...")
|
||||
oClient.MoveMessage(oMessageId, MoveMailTo)
|
||||
Logger.Debug($"FetchIMAPMessagesS22 - successfully moved!")
|
||||
Catch ex As Exception
|
||||
Logger.Warn($"FetchIMAPMessagesS22 - Could not move message to folder [{MoveMailTo}] - Error: {ex.Message}")
|
||||
End Try
|
||||
End If
|
||||
|
||||
|
||||
End If
|
||||
|
||||
Try
|
||||
If IsTest = False Then
|
||||
File.Delete(oTempPath)
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
|
||||
End Try
|
||||
Catch ex As Exception
|
||||
Logger.Warn($"FetchIMAPMessages - Unexpected Error while working on email: [{ex.Message}] - Last Debug Log: [{oLastLog}]")
|
||||
End Try
|
||||
|
||||
Next
|
||||
oClient.Expunge()
|
||||
Logger.Debug("FetchIMAPMessagesS22 - Finished Message-Fetch")
|
||||
If oMessageCountRegular > 0 Or oMessageCountWorked > 0 Then
|
||||
Logger.Info($"###############################################")
|
||||
If oMessageCountRegular > 0 Then
|
||||
Logger.Info($"Found [{oMessageCountRegular.ToString}] regular messages to work on!")
|
||||
End If
|
||||
If oMessageCountWorked > 0 Then
|
||||
Logger.Info($"Found [{oMessageCountWorked.ToString}] worked messages to work on!")
|
||||
End If
|
||||
If IsTest = True Then
|
||||
Dim omsgtext As String
|
||||
If oMessageCountRegular > 0 Then
|
||||
omsgtext = $"Found [{oMessageCountRegular.ToString}] regular Messages to work on!"
|
||||
End If
|
||||
If oMessageCountWorked > 0 Then
|
||||
If omsgtext = String.Empty Then
|
||||
Logger.Info($"Found [{oMessageCountWorked.ToString}] worked messages to work on!")
|
||||
Else
|
||||
omsgtext += vbNewLine & $"Found [{oMessageCountWorked.ToString}] worked messages to work on!"
|
||||
End If
|
||||
End If
|
||||
MsgBox(omsgtext)
|
||||
End If
|
||||
Logger.Info($"###############################################")
|
||||
End If
|
||||
End Using
|
||||
End If
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' Uses a private API from MailWriter to write a MailMessage to disk.
|
||||
''' May break in future versions of .NET
|
||||
@@ -181,6 +72,4 @@ Public Class clsEmailIMAP
|
||||
End Try
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
Reference in New Issue
Block a user