2021-07-22 16:45:08 +02:00

242 lines
8.3 KiB
VB.net

Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Filesystem
Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Imports Limilabs.Mail.MIME
Public Class Email2
Private ReadOnly Logger As Logger
Private ReadOnly LogConfig As LogConfig
Private ReadOnly FileEx As Filesystem.File
Public Sub New(pLogConfig As LogConfig)
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger()
FileEx = New Filesystem.File(pLogConfig)
End Sub
Public Enum EmailSecurity
SSL
STARTTLS
End Enum
Private Function Get_PortForSecurityOption(pSecurity As EmailSecurity) As Integer
Select Case pSecurity
Case EmailSecurity.SSL
Return 993
Case EmailSecurity.STARTTLS
Return 143
Case Else
Return 0
End Select
End Function
Public Function Test_Login(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As Boolean
Logger.Debug("Testing Login to Server [{0}:{1}] with user [{2}]", pServer, pPort, pUsername)
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Inbox")
Dim oStatus As FolderStatus = oClient.SelectInbox()
Logger.Debug("Test finished!")
oClient.Close()
End Using
Return True
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return False
End Try
End Function
Public Function Get_Messages(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As List(Of IMail)
Logger.Debug("Fetching Messages from Server [{0}:{1}] with user [{2}]", pServer, pPort, pUsername)
Dim oMails As New List(Of IMail)
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Folder [{0}]", pFolder)
Dim oStatus As FolderStatus = oClient.Select(pFolder)
Logger.Debug("Fetching Unseen UUIDs")
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.Unseen)
Dim oMailBuilder As New MailBuilder()
Logger.Debug("Fetching Unseen Mails")
For Each oUUID As Long In oUUIDs
Dim oEmlFile As Byte() = oClient.GetMessageByUID(oUUID)
Dim oEmail As IMail = oMailBuilder.CreateFromEml(oEmlFile)
oMails.Add(oEmail)
Next
Logger.Debug("Emails fetched!")
oClient.Close()
End Using
Return oMails
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return New List(Of IMail)
End Try
End Function
Public Function Get_Message(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, pMessageId As String, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As IMail
Logger.Debug("Fetching Message [{0}] from Server [{1}:{2}] with user [{3}]", pMessageId, pServer, pPort, pUsername)
Dim oMail As IMail = Nothing
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Inbox")
Dim oStatus As FolderStatus = oClient.Select(pFolder)
Logger.Debug("Fetching UUIDs")
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.All)
Dim oMailBuilder As New MailBuilder()
Dim oInfos As List(Of MessageInfo) = oClient.GetMessageInfoByUID(oUUIDs)
Logger.Debug("Fetching Unseen Mails")
For Each oInfo As MessageInfo In oInfos
If oInfo.Envelope.MessageID = pMessageId Then
Dim oMailData As Byte() = oClient.GetMessageByUID(oInfo.UID)
oMail = oMailBuilder.CreateFromEml(oMailData)
Exit For
End If
Next
Logger.Debug("Emails fetched!")
oClient.Close()
End Using
Return oMail
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return New List(Of IMail)
End Try
End Function
Public Function Remove_AttachmentsFromEmail(pFileName As String) As String
Try
Dim oTempFileName As String = IO.Path.GetTempFileName()
Dim oMailBuilder As New MailBuilder()
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
oMail.RemoveAttachments()
oMail.Save(oTempFileName)
Return oTempFileName
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function Save_AttachmentsToDisk(pFileName As String) As List(Of String)
Try
Dim oAttachmentPaths As New List(Of String)
Dim oMailBuilder As New MailBuilder()
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
Dim oTempPath As String = IO.Path.GetTempPath()
If oMail.Attachments.Count = 0 Then
Return New List(Of String)
End If
For Each oAttachment As MimeData In oMail.Attachments
Dim oPath As String = IO.Path.Combine(oTempPath, oAttachment.SafeFileName)
Dim oVersionedPath As String = FileEx.GetVersionedFilename(oPath)
oAttachment.Save(oVersionedPath)
oAttachmentPaths.Add(oVersionedPath)
Next
Return oAttachmentPaths
Catch ex As Exception
Logger.Error(ex)
Return New List(Of String)
End Try
End Function
End Class