3 Commits

Author SHA1 Message Date
Jonathan Jenne
ddffb9c4f8 Messaging: Rework mail fetcher to build client adhoc 2023-09-15 09:24:48 +02:00
Jonathan Jenne
b16050cfc6 Database: fix missing message on exception 2023-09-14 15:40:45 +02:00
Jonathan Jenne
a415f90906 Messaging: Fix Mail Session 2023-09-14 15:40:23 +02:00
3 changed files with 73 additions and 17 deletions

View File

@@ -111,7 +111,7 @@ Public Class MSSQLServer
Private Function MaybeGetTransaction(Connection As SqlConnection, Mode As TransactionMode, Transaction As SqlTransaction) As SqlTransaction Private Function MaybeGetTransaction(Connection As SqlConnection, Mode As TransactionMode, Transaction As SqlTransaction) As SqlTransaction
If Connection Is Nothing Then If Connection Is Nothing Then
Throw New ArgumentNullException("Connection") Throw New ArgumentNullException("Connection", "Could not get transaction because connection is null!")
End If End If
If Mode = TransactionMode.NoTransaction Then If Mode = TransactionMode.NoTransaction Then
@@ -269,7 +269,7 @@ Public Class MSSQLServer
Try Try
If pConnectionString Is Nothing OrElse pConnectionString.Length = 0 Then If pConnectionString Is Nothing OrElse pConnectionString.Length = 0 Then
Logger.Warn("Connection String is empty!") Logger.Warn("Connection String is empty!")
Throw New ArgumentNullException("pConnectionString") Throw New ArgumentNullException("pConnectionString", "Could not mask connection string because connectiong string is empty!")
End If End If
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = pConnectionString} Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = pConnectionString}

View File

@@ -7,7 +7,7 @@ Imports Limilabs.Mail
Namespace Mail Namespace Mail
Public Class MailFetcher Public Class MailFetcher
Inherits BaseClass Inherits BaseClass
Private ReadOnly MailSession As MailSession Private MailSession As MailSession
Private ReadOnly MailBuilder As New MailBuilder Private ReadOnly MailBuilder As New MailBuilder
Public ReadOnly Property Connected2Server As Boolean Public ReadOnly Property Connected2Server As Boolean
@@ -24,35 +24,50 @@ Namespace Mail
Public Sub New(pLogConfig As LogConfig) Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
MailSession = New MailSession(pLogConfig, New Imap)
Log.Enabled = pLogConfig.Debug Log.Enabled = pLogConfig.Debug
End Sub End Sub
Public Function Connect(pServer As String, pPort As Integer, pUser As String, pPassword As String, pAuthType As String) As MailSession.SessionInfo Public Function Connect(pServer As String, pPort As Integer, pUser As String, pPassword As String, pAuthType As String) As MailSession.SessionInfo
MailSession = New MailSession(LogConfig, New Imap)
Return MailSession.ConnectToServerWithBasicAuth(pServer, pPort, pUser, pPassword, pAuthType, New MailSession.MailSessionOptions) Return MailSession.ConnectToServerWithBasicAuth(pServer, pPort, pUser, pPassword, pAuthType, New MailSession.MailSessionOptions)
End Function End Function
Public Function ConnectToO365(pUser As String, pClientId As String, pTenantId As String, pClientSecret As String) Public Function ConnectToO365(pUser As String, pClientId As String, pTenantId As String, pClientSecret As String)
MailSession = New MailSession(LogConfig, New Imap)
Dim oOptions = New MailSession.MailSessionOptions With {.EnableTls1_2 = True} Dim oOptions = New MailSession.MailSessionOptions With {.EnableTls1_2 = True}
Return MailSession.ConnectToServerWithO365OAuth(pUser, pClientId, pTenantId, pClientSecret, oOptions) Return MailSession.ConnectToServerWithO365OAuth(pUser, pClientId, pTenantId, pClientSecret, oOptions)
End Function End Function
Public Function Connect(pServer As String, pPort As Integer, pUser As String, pPassword As String, pAuthType As String, pOptions As MailSession.MailSessionOptions) As MailSession.SessionInfo Public Function Connect(pServer As String, pPort As Integer, pUser As String, pPassword As String, pAuthType As String, pOptions As MailSession.MailSessionOptions) As MailSession.SessionInfo
MailSession = New MailSession(LogConfig, New Imap)
Return MailSession.ConnectToServerWithBasicAuth(pServer, pPort, pUser, pPassword, pAuthType, pOptions) Return MailSession.ConnectToServerWithBasicAuth(pServer, pPort, pUser, pPassword, pAuthType, pOptions)
End Function End Function
Public Function Disconnect() As Boolean Public Function Disconnect() As Boolean
If (MailSession.Session.Connected = False) Then
Return True
End If
Return MailSession.DisconnectFromServer() Return MailSession.DisconnectFromServer()
End Function End Function
Public Async Function FetchAllMails() As Task(Of List(Of IMail)) Public Async Function FetchAllMailsAsync() As Task(Of List(Of IMail))
Return Await FetchMailsAsync(Flag.All) Return Await FetchMailsAsync(Flag.All)
End Function End Function
Public Async Function FetchUnreadMails() As Task(Of List(Of IMail)) Public Async Function FetchUnreadMailsAsync() As Task(Of List(Of IMail))
Return Await FetchMailsAsync(Flag.Unseen) Return Await FetchMailsAsync(Flag.Unseen)
End Function End Function
Public Function FetchAllMails() As List(Of IMail)
Return FetchMails(Flag.All)
End Function
Public Function FetchUnreadMails() As List(Of IMail)
Return FetchMails(Flag.Unseen)
End Function
Public Function ListAllMails() As List(Of Long) Public Function ListAllMails() As List(Of Long)
Return ListMails(Flag.All) Return ListMails(Flag.All)
@@ -62,18 +77,54 @@ Namespace Mail
Return ListMails(Flag.Unseen) Return ListMails(Flag.Unseen)
End Function End Function
Public Function FetchMails(pMailFlag As Flag) As List(Of IMail)
Try
If MailSession.Session.Connected Then
Dim oMailIds = ListMails(pMailFlag)
Dim oMails As New List(Of IMail)
For Each oId In oMailIds
Dim oMail As IMail = FetchMail(oId)
oMails.Add(oMail)
Next
Return oMails
Else
Logger.Error("No connection to Server. Exiting.")
Return Nothing
End If
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Async Function FetchMailsAsync(pMailFlag As Flag) As Task(Of List(Of IMail)) Public Async Function FetchMailsAsync(pMailFlag As Flag) As Task(Of List(Of IMail))
Dim oMailIds = ListMails(pMailFlag) Try
Dim oMails As New List(Of IMail) If MailSession.Session.Connected Then
Dim oMailIds = ListMails(pMailFlag)
Dim oMails As New List(Of IMail)
For Each oId In oMailIds For Each oId In oMailIds
Dim oMail As IMail = Await FetchMailAsync(oId) Dim oMail As IMail = Await FetchMailAsync(oId)
oMails.Add(oMail) oMails.Add(oMail)
Next Next
Return oMails
Else
Logger.Error("No connection to Server. Exiting.")
Return Nothing
End If
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
Return oMails
End Function End Function
Public Function ListMails(pMailFlag As Flag) As List(Of Long) Public Function ListMails(pMailFlag As Flag) As List(Of Long)

View File

@@ -166,7 +166,7 @@ Namespace Mail
ElseIf pSession.AuthType = AUTH_SSL Then ElseIf pSession.AuthType = AUTH_SSL Then
Try Try
If pSession.Port = 465 Then If pSession.Port = 993 Then
Logger.Debug("Connecting with [ConnectSSL] on [{0}/{1}]", pSession.Server, pSession.Port) Logger.Debug("Connecting with [ConnectSSL] on [{0}/{1}]", pSession.Server, pSession.Port)
Client.ConnectSSL(pSession.Server, pSession.Port) Client.ConnectSSL(pSession.Server, pSession.Port)
Else Else
@@ -186,8 +186,13 @@ Namespace Mail
ElseIf Session.AuthType = AUTH_SSLTLS Or Session.AuthType = AUTH_STARTTLS Then ElseIf Session.AuthType = AUTH_SSLTLS Or Session.AuthType = AUTH_STARTTLS Then
Try Try
Logger.Debug("Connecting with [Connect] on [{0}/{1}]", pSession.Server, pSession.Port) If pSession.Port = 993 Then
Client.Connect(pSession.Server, pSession.Port) Logger.Debug("Connecting with [ConnectSSL] on [{0}/{1}]", pSession.Server, pSession.Port)
Client.ConnectSSL(pSession.Server, pSession.Port)
Else
Logger.Debug("Connecting with [Connect] on [{0}/{1}]", pSession.Server, pSession.Port)
Client.Connect(pSession.Server, pSession.Port)
End If
Logger.Info("Connection Successful!") Logger.Info("Connection Successful!")
Dim oSupportsSTARTTLS As Boolean = SupportsSTARTTLS(Client) Dim oSupportsSTARTTLS As Boolean = SupportsSTARTTLS(Client)