S22 TryCatch to continue

This commit is contained in:
SchreiberM 2020-08-19 11:27:19 +02:00
parent d49e627f5d
commit 34368ce760
14 changed files with 83 additions and 65 deletions

Binary file not shown.

View File

@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben: ' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("2.5.0.0")> <Assembly: AssemblyVersion("2.6.0.0")>
<Assembly: AssemblyFileVersion("2.5.0.0")> <Assembly: AssemblyFileVersion("2.6.0.0")>

View File

@ -23,32 +23,45 @@ Public Class clsEmailIMAP
'End Sub 'End Sub
Public Function FetchIMAPMessages(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 Public Function FetchIMAPMessages(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("S22 Connecting to Server {0}:{1} with user {2}", Server, Port, Username) Logger.Debug("S22FetchIMAPMessages - Connecting to Server {0}:{1} with user {2}", Server, Port, Username)
Try Try
Logger.Debug("S22 Connecting...") Logger.Debug("S22FetchIMAPMessages - Connecting...")
Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True) Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True)
If Not oClient.Authed Then If Not oClient.Authed Then
Logger.Warn("S22 Connected to server but authentication failed.") Logger.Warn("S22FetchIMAPMessages - Connected to server but authentication failed.")
Return False Return False
End If End If
Logger.Debug($"S22 Fetching unseen MessageIds from Inbox: {pInbox}") Logger.Debug($"S22FetchIMAPMessages - Fetching unseen MessageIds from Inbox: {pInbox}")
Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, pInbox) Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, pInbox)
Logger.Debug("S22 Found {0} messages", oMessageIds.Count) Logger.Debug("S22FetchIMAPMessages - Found {0} messages", oMessageIds.Count)
Logger.Debug("S22 Fetching messages...") Logger.Debug("S22FetchIMAPMessages - Fetching messages...")
Dim oMessageCountRegular As Integer = 0 Dim oMessageCountRegular As Integer = 0
Dim oMessageCountWorked As Integer = 0 Dim oMessageCountWorked As Integer = 0
Dim oLastLog As String
For Each oMessageId As UInteger In oMessageIds For Each oMessageId As UInteger In oMessageIds
Logger.Debug($"Checking message") Logger.Debug($"Checking message...")
Dim oMessage = oClient.GetMessage(oMessageId, False, pInbox) Dim oMessage = oClient.GetMessage(oMessageId, False, pInbox)
Logger.Debug($"Checking message Subject: {oMessage.Subject}, From: {oMessage.From}") oLastLog = $"Checking message with Subject [{oMessage.Subject}] From [{oMessage.From}]"
Logger.Debug(oLastLog)
Dim oTempPath = Path.GetTempFileName() Dim oTempPath = Path.GetTempFileName()
Try
Dim oResult = WriteMessageToFile(oMessage, oTempPath) 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($"S22FetchIMAPMessages - Could not get a MessageID or create a MailObject - Error: {ex.Message} - Last Debug Log: [{oLastLog}]")
Continue For
End Try
Dim oMsg As New Message(oTempPath) oLastLog &= " # Got the MessageID"
Dim oMessageREFGUID = oMsg.MessageID
oMessageREFGUID = oMessageREFGUID.Replace(">", "").Replace("<", "") oMessageREFGUID = oMessageREFGUID.Replace(">", "").Replace("<", "")
Dim oCHECKSQL = $"SELECT * FROM TBEMLP_HISTORY WHERE lower(EMAIL_MSGID) = lower('{oMessageREFGUID}')" Dim oCHECKSQL = $"SELECT * FROM TBEMLP_HISTORY WHERE lower(EMAIL_MSGID) = lower('{oMessageREFGUID}')"
Dim oCHECKDT As DataTable = _DB_MSSQL.Return_Datatable(oCHECKSQL) Dim oCHECKDT As DataTable = _DB_MSSQL.Return_Datatable(oCHECKSQL)
@ -56,25 +69,27 @@ Public Class clsEmailIMAP
If oCHECKDT.Rows.Count = 0 Then If oCHECKDT.Rows.Count = 0 Then
oMessageCountRegular += 1 oMessageCountRegular += 1
CURRENT_WORKMAIL_LIST.Add(oMsg) CURRENT_WORKMAIL_LIST.Add(oMsg)
oLastLog &= " # Added to CURRENT_WORKMAIL_LIST"
Else Else
Logger.Info("S22 Message has already been worked! Skipping!") Logger.Info("S22FetchIMAPMessages - Message has already been worked! Skipping!")
Logger.Debug($"Message shall be deleted...") Logger.Debug($"Message shall be deleted...")
oLastLog &= " # Message shall be deleted..."
oClient.DeleteMessage(oMessageId,) oClient.DeleteMessage(oMessageId,)
Logger.Debug($"S22 Message has been deleted!") Logger.Debug($"S22FetchIMAPMessages - Message has been deleted!")
oLastLog &= " # S22FetchIMAPMessages - Message has been deleted!"
Dim oUpd = $"UPDATE TBEMLP_HISTORY SET DATE_DELETED_INBOX = GETDATE() WHERE lower(EMAIL_MSGID) = lower('{oMessageId}')" Dim oUpd = $"UPDATE TBEMLP_HISTORY SET DATE_DELETED_INBOX = GETDATE() WHERE lower(EMAIL_MSGID) = lower('{oMessageId}')"
_DB_MSSQL.Execute_non_Query(oUpd) _DB_MSSQL.Execute_non_Query(oUpd)
oMessageCountWorked += 1 oMessageCountWorked += 1
End If End If
If IsTest = True Then If IsTest = True Then
Logger.Debug($"S22 IMAP-Test Message#: {oMessageCountRegular} - Msgsubject is: {oMsg.Subject} - MsgMessageID is: {oMessageREFGUID}") Logger.Debug($"S22FetchIMAPMessages - IMAP-Test Message#: {oMessageCountRegular} - Msgsubject is: {oMsg.Subject} - MsgMessageID is: {oMessageREFGUID}")
Logger.Debug($"S22 message correctly fetched. Mail has been downloaded to {oTempPath}") Logger.Debug($"S22FetchIMAPMessages - message correctly fetched. Mail has been downloaded to {oTempPath}")
End If End If
Try Try
If DeleteinTest = True Then If DeleteinTest = True Then
Logger.Debug($"Message shall be deleted...") Logger.Debug($"Message shall be deleted...")
oClient.DeleteMessage(oMessageId,) oClient.DeleteMessage(oMessageId,)
Logger.Debug($"S22 Message has been deleted!") Logger.Debug($"S22FetchIMAPMessages - Message has been deleted!")
End If End If
Catch ex As Exception Catch ex As Exception
Logger.Warn("Message could not be deleted: " & ex.Message) Logger.Warn("Message could not be deleted: " & ex.Message)
@ -83,9 +98,9 @@ Public Class clsEmailIMAP
Try Try
Logger.Debug($"Moving to [{MoveMailTo}] is active...") Logger.Debug($"Moving to [{MoveMailTo}] is active...")
oClient.MoveMessage(oMessageId, MoveMailTo) oClient.MoveMessage(oMessageId, MoveMailTo)
Logger.Debug($"S22 successfully moved!") Logger.Debug($"S22FetchIMAPMessages - successfully moved!")
Catch ex As Exception Catch ex As Exception
Logger.Warn($"S22 Could not move message to folder [{MoveMailTo}] - Error: {ex.Message}") Logger.Warn($"S22FetchIMAPMessages - Could not move message to folder [{MoveMailTo}] - Error: {ex.Message}")
End Try End Try
End If End If
@ -98,12 +113,15 @@ Public Class clsEmailIMAP
End If End If
Catch ex As Exception Catch ex As Exception
Logger.Error(ex)
Logger.Warn("S22 Temp file could not be deleted")
End Try End Try
Catch ex As Exception
Logger.Warn($"FetchIMAPMessages - Unexpected Error while working on email: [{ex.Message}] - Last Debug Log: [{oLastLog}]")
End Try
Next Next
oClient.Expunge() oClient.Expunge()
Logger.Debug("S22 Finished Message-Fetch") Logger.Debug("S22FetchIMAPMessages - Finished Message-Fetch")
If oMessageCountRegular > 0 Or oMessageCountWorked > 0 Then If oMessageCountRegular > 0 Or oMessageCountWorked > 0 Then
Logger.Info($"###############################################") Logger.Info($"###############################################")
If oMessageCountRegular > 0 Then If oMessageCountRegular > 0 Then

View File

@ -135,10 +135,10 @@ Public Class clsWorker
End If End If
If pollresult = False Then If pollresult = False Then
Logger.Warn(String.Format("Error while polling emails...Trying S22....")) Logger.Info(String.Format("Error while polling emails...Trying S22...."))
pollresult = _emailIMAP.FetchIMAPMessages(MAIL_SERVER, MAIL_PORT, MAIL_USER, MAIL_USER_PW, MAIL_INBOX_NAME) pollresult = _emailIMAP.FetchIMAPMessages(MAIL_SERVER, MAIL_PORT, MAIL_USER, MAIL_USER_PW, MAIL_INBOX_NAME)
If pollresult = False Then If pollresult = False Then
Logger.Warn(String.Format("S22 also returned error!")) Logger.Warn(String.Format("S22FetchIMAPMessages - also returned error!"))
End If End If
End If End If
If CURRENT_WORKMAIL_LIST.Count() > 0 Then If CURRENT_WORKMAIL_LIST.Count() > 0 Then