MS Mailfunctions und Monitoring
This commit is contained in:
149
Modules.Messaging/Limilab.vb
Normal file
149
Modules.Messaging/Limilab.vb
Normal file
@@ -0,0 +1,149 @@
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports Limilabs.Mail
|
||||
Imports Limilabs.Client.IMAP
|
||||
Imports Limilabs.Client
|
||||
Imports System.Net.Security
|
||||
Imports System
|
||||
Imports System.Security.Authentication
|
||||
|
||||
Public Class Limilab
|
||||
Private Initialized As Boolean = False
|
||||
Private LogConfig As LogConfig
|
||||
Private Logger As DigitalData.Modules.Logging.Logger
|
||||
Private IMAPServer As String
|
||||
Private IMAPPort As Integer
|
||||
Private User As String
|
||||
Private Password As String
|
||||
Private AuthType As String
|
||||
Private ImapObject As Imap
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
LogConfig = LogConfig
|
||||
Logger = LogConfig.GetLogger()
|
||||
Logger.Info("Limilab initialized")
|
||||
End Sub
|
||||
Public Sub InitIMAP(oImapServer As String, oPort As Integer, oUser As String, oPassword As String, oAuthType As String, Optional Folder As String = "Inbox")
|
||||
IMAPServer = oImapServer
|
||||
IMAPPort = oPort
|
||||
User = oUser
|
||||
Password = oPassword
|
||||
AuthType = oAuthType
|
||||
Initialized = True
|
||||
End Sub
|
||||
''' <summary>
|
||||
''' Tests connection to a given IMAP Server by connecting and doing a simple message query.
|
||||
''' </summary>
|
||||
''' <param name="Server">IP-Address or Domainname of Server</param>
|
||||
''' <param name="Port">IMAP-Port</param>
|
||||
''' <param name="Username">IMAP-Username</param>
|
||||
''' <param name="Password">IMAP-Password</param>
|
||||
''' <param name="Folder">The folder to fetch messages from. Defaults to `Inbox`</param>
|
||||
''' <returns>True if connection and query were successful. False otherwise.</returns>
|
||||
Public Function IMAPTestLogin() As Boolean
|
||||
Logger.Debug("Testing Login to Server {0}:{1} with user {2}", IMAPServer, IMAPPort, User)
|
||||
If Initialized = False Then
|
||||
Return False
|
||||
End If
|
||||
Try
|
||||
Logger.Debug("Connecting...")
|
||||
Dim oReturn As Boolean = ImapConnect()
|
||||
If oReturn = True Then
|
||||
ImapObject.Close()
|
||||
End If
|
||||
Return oReturn
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
Private Function ImapConnect() As Boolean
|
||||
Try
|
||||
If Initialized = False Then
|
||||
Return True
|
||||
End If
|
||||
Dim oReturnImap As New Imap()
|
||||
|
||||
If AuthType.EndsWith("TLS") Then
|
||||
oReturnImap.SSLConfiguration.EnabledSslProtocols = SslProtocols.Tls12
|
||||
' we will use custom validation
|
||||
AddHandler oReturnImap.ServerCertificateValidate, AddressOf Validate
|
||||
Logger.Debug("Connecting to IMAP-Server without port...")
|
||||
oReturnImap.Connect(IMAPServer)
|
||||
|
||||
ElseIf AuthType = "SSL" Then
|
||||
' we will use custom validation
|
||||
AddHandler oReturnImap.ServerCertificateValidate, AddressOf Validate
|
||||
Logger.Debug($"Connecting to IMAP-Server with port {IMAPPort}...")
|
||||
oReturnImap.ConnectSSL(IMAPServer, IMAPPort)
|
||||
ElseIf AuthType = "Simple" Then
|
||||
|
||||
End If
|
||||
Logger.Debug(">> Connected to IMAP-Server!")
|
||||
Dim oSupportsStartTLS As Boolean = oReturnImap.SupportedExtensions() _
|
||||
.Contains(ImapExtension.StartTLS)
|
||||
If oSupportsStartTLS And AuthType.EndsWith("TLS") Then
|
||||
Logger.Debug("Server supports StartTLS, so starting...")
|
||||
oReturnImap.StartTLS()
|
||||
End If
|
||||
Logger.Debug("Login with User and password...")
|
||||
oReturnImap.UseBestLogin(User, Password)
|
||||
Logger.Debug(">> Logged on!")
|
||||
ImapObject = oReturnImap
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
If Not IsNothing(ex.InnerException) Then
|
||||
Logger.Warn("Inner Exception ImapConnect: " + ex.InnerException.Message)
|
||||
End If
|
||||
|
||||
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
Private Sub Validate(
|
||||
ByVal sender As Object,
|
||||
ByVal e As ServerCertificateValidateEventArgs)
|
||||
|
||||
Const ignoredErrors As SslPolicyErrors =
|
||||
SslPolicyErrors.RemoteCertificateChainErrors Or _ ' self-signed
|
||||
SslPolicyErrors.RemoteCertificateNameMismatch ' name mismatch
|
||||
|
||||
Dim nameOnCertificate As String = e.Certificate.Subject
|
||||
|
||||
If (e.SslPolicyErrors And Not ignoredErrors) = SslPolicyErrors.None Then
|
||||
e.IsValid = True
|
||||
Return
|
||||
End If
|
||||
e.IsValid = False
|
||||
End Sub
|
||||
Public Function ImapGetMessageIDs() As List(Of Long)
|
||||
Dim oListuids As New List(Of Long)
|
||||
Try
|
||||
ImapObject.SelectInbox()
|
||||
|
||||
oListuids = ImapObject.Search(Flag.Unseen)
|
||||
|
||||
Return oListuids
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return oListuids
|
||||
End Try
|
||||
|
||||
End Function
|
||||
|
||||
Public Function GetMessageUids(oImapServer As String, oPort As Integer, oUser As String, oPassword As String, oSSL As Boolean)
|
||||
Try
|
||||
|
||||
Catch ex As Exception
|
||||
|
||||
End Try
|
||||
End Function
|
||||
Public Function DeleteMessageByUID(oUID As String) As Boolean
|
||||
Try
|
||||
ImapObject.DeleteMessageByUID(oUID)
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user