Messaging: WIP Email2
This commit is contained in:
parent
c22c3aa0a2
commit
829a13d37d
@ -13,7 +13,7 @@ Public Class frmEmail
|
|||||||
|
|
||||||
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
||||||
|
|
||||||
Dim oResult = Email.Test_Login(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL)
|
Dim oResult = Email.Test_Login(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSLTLS)
|
||||||
|
|
||||||
If oResult = True Then
|
If oResult = True Then
|
||||||
AddLog($"Connection to {txtServer.Text} successful.")
|
AddLog($"Connection to {txtServer.Text} successful.")
|
||||||
@ -23,7 +23,7 @@ Public Class frmEmail
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
|
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
|
||||||
Dim oMessages = Email.Get_Messages(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL, "Inbox")
|
Dim oMessages = Email.Get_Messages(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSLTLS, "Inbox")
|
||||||
AddLog($"Found {oMessages.Count} Messages!")
|
AddLog($"Found {oMessages.Count} Messages!")
|
||||||
|
|
||||||
For Each oMessage In oMessages
|
For Each oMessage In oMessages
|
||||||
@ -37,7 +37,7 @@ Public Class frmEmail
|
|||||||
|
|
||||||
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
|
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
|
||||||
Dim oMessageId As String = txtMessageID.Text
|
Dim oMessageId As String = txtMessageID.Text
|
||||||
Dim oMail As IMail = Email.Get_Message(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL, oMessageId, "Inbox")
|
Dim oMail As IMail = Email.Get_Message(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSLTLS, oMessageId, "Inbox")
|
||||||
Dim oFilename As String = IO.Path.GetTempFileName
|
Dim oFilename As String = IO.Path.GetTempFileName
|
||||||
oMail.Save(oFilename)
|
oMail.Save(oFilename)
|
||||||
|
|
||||||
|
|||||||
@ -3,11 +3,14 @@ Imports DigitalData.Modules.Filesystem
|
|||||||
Imports Limilabs.Mail
|
Imports Limilabs.Mail
|
||||||
Imports Limilabs.Client.IMAP
|
Imports Limilabs.Client.IMAP
|
||||||
Imports Limilabs.Mail.MIME
|
Imports Limilabs.Mail.MIME
|
||||||
|
Imports System.IO
|
||||||
|
Imports Limilabs.Mail.MSG
|
||||||
|
|
||||||
Public Class Email2
|
Public Class Email2
|
||||||
Private ReadOnly Logger As Logger
|
Private ReadOnly Logger As Logger
|
||||||
Private ReadOnly LogConfig As LogConfig
|
Private ReadOnly LogConfig As LogConfig
|
||||||
Private ReadOnly FileEx As Filesystem.File
|
Private ReadOnly FileEx As Filesystem.File
|
||||||
|
Private ReadOnly MailBuilder As New MailBuilder()
|
||||||
|
|
||||||
Public Sub New(pLogConfig As LogConfig)
|
Public Sub New(pLogConfig As LogConfig)
|
||||||
LogConfig = pLogConfig
|
LogConfig = pLogConfig
|
||||||
@ -16,21 +19,49 @@ Public Class Email2
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Public Enum EmailSecurity
|
Public Enum EmailSecurity
|
||||||
SSL
|
SSLTLS
|
||||||
STARTTLS
|
STARTTLS
|
||||||
End Enum
|
End Enum
|
||||||
|
|
||||||
Private Function Get_PortForSecurityOption(pSecurity As EmailSecurity) As Integer
|
Private Function Get_PortForSecurityOption(pSecurity As EmailSecurity) As Integer
|
||||||
Select Case pSecurity
|
If pSecurity = EmailSecurity.STARTTLS Then
|
||||||
Case EmailSecurity.SSL
|
Return 143
|
||||||
Return 993
|
Else ' EmailSecurity.SSL
|
||||||
|
Return 993
|
||||||
|
End If
|
||||||
|
End Function
|
||||||
|
|
||||||
Case EmailSecurity.STARTTLS
|
Private Function New_Connection(pIMAP As Imap, pServer As String, pUsername As String, pPassword As String, pPort As Integer, pSecurity As EmailSecurity) As Boolean
|
||||||
Return 143
|
Try
|
||||||
|
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
|
||||||
|
|
||||||
Case Else
|
Dim oPort As Integer = pPort
|
||||||
Return 0
|
If oPort = 0 Then
|
||||||
End Select
|
oPort = Get_PortForSecurityOption(pSecurity)
|
||||||
|
End If
|
||||||
|
Logger.Debug("Using Port [{0}] for connection", oPort)
|
||||||
|
|
||||||
|
If pSecurity = EmailSecurity.STARTTLS Then
|
||||||
|
Logger.Debug("Using STARTTLS as Security Option")
|
||||||
|
pIMAP.Connect(pServer, oPort)
|
||||||
|
pIMAP.StartTLS()
|
||||||
|
Else
|
||||||
|
Logger.Debug("Using SSL/TLS as Security Option")
|
||||||
|
pIMAP.ConnectSSL(pServer, oPort)
|
||||||
|
End If
|
||||||
|
Logger.Debug("Connection to IMAP Server [{0}] established!", pServer)
|
||||||
|
|
||||||
|
Logger.Debug("Logging in with user [{0}]", pUsername)
|
||||||
|
pIMAP.UseBestLogin(pUsername, pPassword)
|
||||||
|
|
||||||
|
Return True
|
||||||
|
|
||||||
|
Catch ex As Exception
|
||||||
|
Logger.Warn("Could not connect to server [{0}] with user [{1}]", pServer, pUsername)
|
||||||
|
Logger.Error(ex)
|
||||||
|
Return False
|
||||||
|
|
||||||
|
End Try
|
||||||
End Function
|
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
|
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
|
||||||
@ -38,30 +69,15 @@ Public Class Email2
|
|||||||
|
|
||||||
Try
|
Try
|
||||||
Using oClient As New Imap()
|
Using oClient As New Imap()
|
||||||
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
|
If New_Connection(oClient, pServer, pUsername, pPassword, pPort, pSecurity) Then
|
||||||
|
Logger.Debug("Fetching Inbox")
|
||||||
|
Dim oStatus As FolderStatus = oClient.SelectInbox()
|
||||||
|
|
||||||
|
Logger.Debug("Test finished!")
|
||||||
|
oClient.Close()
|
||||||
|
|
||||||
Dim oPort As Integer = pPort
|
|
||||||
If oPort = 0 Then
|
|
||||||
oPort = Get_PortForSecurityOption(pSecurity)
|
|
||||||
End If
|
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
|
End Using
|
||||||
|
|
||||||
Return True
|
Return True
|
||||||
@ -80,46 +96,27 @@ Public Class Email2
|
|||||||
|
|
||||||
Try
|
Try
|
||||||
Using oClient As New Imap()
|
Using oClient As New Imap()
|
||||||
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
|
If New_Connection(oClient, pServer, pUsername, pPassword, pPort, pSecurity) Then
|
||||||
|
Logger.Debug("Fetching Folder [{0}]", pFolder)
|
||||||
|
Dim oStatus As FolderStatus = oClient.Select(pFolder)
|
||||||
|
|
||||||
Dim oPort As Integer = pPort
|
Logger.Debug("Fetching Unseen UUIDs")
|
||||||
If oPort = 0 Then
|
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.Unseen)
|
||||||
oPort = Get_PortForSecurityOption(pSecurity)
|
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 If
|
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
|
End Using
|
||||||
|
|
||||||
Return oMails
|
Return oMails
|
||||||
@ -139,50 +136,27 @@ Public Class Email2
|
|||||||
|
|
||||||
Try
|
Try
|
||||||
Using oClient As New Imap()
|
Using oClient As New Imap()
|
||||||
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
|
If New_Connection(oClient, pServer, pUsername, pPassword, pPort, pSecurity) Then
|
||||||
|
Logger.Debug("Fetching Folder [{0}]", pFolder)
|
||||||
|
Dim oStatus As FolderStatus = oClient.Select(pFolder)
|
||||||
|
|
||||||
Dim oPort As Integer = pPort
|
Logger.Debug("Fetching UUIDs")
|
||||||
If oPort = 0 Then
|
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.All)
|
||||||
oPort = Get_PortForSecurityOption(pSecurity)
|
Logger.Debug("Fetching Mails")
|
||||||
|
Dim oInfos As List(Of MessageInfo) = oClient.GetMessageInfoByUID(oUUIDs)
|
||||||
|
|
||||||
End If
|
Dim oMailInfo = oInfos.Where(Function(i) i.Envelope.MessageID = pMessageId).FirstOrDefault()
|
||||||
|
|
||||||
If pSecurity = EmailSecurity.SSL Then
|
If oMailInfo IsNot Nothing Then
|
||||||
oClient.ConnectSSL(pServer, oPort)
|
Dim oMailData As Byte() = oClient.GetMessageByUID(oMailInfo.UID)
|
||||||
|
oMail = MailBuilder.CreateFromEml(oMailData)
|
||||||
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
|
End If
|
||||||
Next
|
|
||||||
|
|
||||||
Logger.Debug("Emails fetched!")
|
Logger.Debug("Emails fetched!")
|
||||||
oClient.Close()
|
oClient.Close()
|
||||||
|
|
||||||
|
End If
|
||||||
|
|
||||||
End Using
|
End Using
|
||||||
|
|
||||||
Return oMail
|
Return oMail
|
||||||
@ -190,16 +164,16 @@ Public Class Email2
|
|||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
|
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
|
||||||
Logger.Error(ex)
|
Logger.Error(ex)
|
||||||
Return New List(Of IMail)
|
|
||||||
|
Return Nothing
|
||||||
|
|
||||||
End Try
|
End Try
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
Public Function Remove_AttachmentsFromEmail(pFileName As String) As String
|
Public Function Remove_AttachmentsFromEmail(pFileName As String, Optional pSuffix As String = "") As String
|
||||||
Try
|
Try
|
||||||
Dim oTempFileName As String = IO.Path.GetTempFileName()
|
Dim oTempFileName As String = Path.Combine(Path.GetTempPath(), AddFilenameSuffix(pFileName, pSuffix))
|
||||||
Dim oMailBuilder As New MailBuilder()
|
Dim oMail = MailBuilder.CreateFromEmlFile(pFileName)
|
||||||
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
|
|
||||||
|
|
||||||
oMail.RemoveAttachments()
|
oMail.RemoveAttachments()
|
||||||
oMail.Save(oTempFileName)
|
oMail.Save(oTempFileName)
|
||||||
@ -215,8 +189,7 @@ Public Class Email2
|
|||||||
Public Function Save_AttachmentsToDisk(pFileName As String) As List(Of String)
|
Public Function Save_AttachmentsToDisk(pFileName As String) As List(Of String)
|
||||||
Try
|
Try
|
||||||
Dim oAttachmentPaths As New List(Of String)
|
Dim oAttachmentPaths As New List(Of String)
|
||||||
Dim oMailBuilder As New MailBuilder()
|
Dim oMail = MailBuilder.CreateFromEmlFile(pFileName)
|
||||||
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
|
|
||||||
Dim oTempPath As String = IO.Path.GetTempPath()
|
Dim oTempPath As String = IO.Path.GetTempPath()
|
||||||
|
|
||||||
If oMail.Attachments.Count = 0 Then
|
If oMail.Attachments.Count = 0 Then
|
||||||
@ -238,4 +211,56 @@ Public Class Email2
|
|||||||
Return New List(Of String)
|
Return New List(Of String)
|
||||||
End Try
|
End Try
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
|
Public Function ConvertMsgToEml(pEmailFileName As String) As String
|
||||||
|
Dim oInfo As New FileInfo(pEmailFileName)
|
||||||
|
|
||||||
|
If oInfo.Extension.ToUpper = ".EML" Then
|
||||||
|
Return pEmailFileName
|
||||||
|
|
||||||
|
ElseIf oInfo.Extension.ToUpper = ".MSG" Then
|
||||||
|
Return DoConvertMsgToEmlFile(pEmailFileName)
|
||||||
|
|
||||||
|
Else
|
||||||
|
Return Nothing
|
||||||
|
|
||||||
|
End If
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function DoConvertMsgToEmlFile(pMsgFile As String) As String
|
||||||
|
Try
|
||||||
|
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(pMsgFile)
|
||||||
|
Dim oEmlPath As String = $"{oFileNameWithoutExtension}.eml"
|
||||||
|
|
||||||
|
Using oConverter As New MsgConverter(pMsgFile)
|
||||||
|
Dim oEmail As IMail = oConverter.CreateMessage()
|
||||||
|
Dim oData As Byte() = oEmail.Render()
|
||||||
|
|
||||||
|
oEmail.Save(oEmlPath)
|
||||||
|
End Using
|
||||||
|
|
||||||
|
Return oEmlPath
|
||||||
|
|
||||||
|
Catch ex As Exception
|
||||||
|
Return Nothing
|
||||||
|
|
||||||
|
End Try
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function GetTempFileNameWithExtension(pExtension As String) As String
|
||||||
|
Dim oTempFileName As String = IO.Path.GetTempFileName()
|
||||||
|
Dim oInfo As New IO.FileInfo(oTempFileName)
|
||||||
|
Dim oExtension As String = IIf(pExtension.StartsWith("."), pExtension, $".{pExtension}")
|
||||||
|
Dim oNewFileName = IO.Path.Combine(oInfo.DirectoryName, oInfo.Name.Replace(oInfo.Extension, oExtension))
|
||||||
|
|
||||||
|
Return oNewFileName
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Function AddFilenameSuffix(pFilename As String, pSuffix As String)
|
||||||
|
Dim oFileInfo As New FileInfo(pFilename)
|
||||||
|
Dim oExtension As String = oFileInfo.Extension
|
||||||
|
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(pFilename)
|
||||||
|
|
||||||
|
Return $"{oFileNameWithoutExtension}{pSuffix}{oExtension}"
|
||||||
|
End Function
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user