Messaging: Add functions to clear temp files, get sender/receiver addresses, always convert msg to eml

This commit is contained in:
Jonathan Jenne 2021-08-20 13:46:00 +02:00
parent 9ae92b75c2
commit 3f724dac44

View File

@ -14,6 +14,7 @@ Public Class Email2
Private ReadOnly LogConfig As LogConfig
Private ReadOnly FileEx As Filesystem.File
Private ReadOnly MailBuilder As New MailBuilder()
Private ReadOnly TempFiles As New List(Of String)
Private DisableExcessiveLogging As Boolean = False
@ -271,6 +272,16 @@ Public Class Email2
End Try
End Function
''' <summary>
''' Loads an eml file from disk and returns the IMail representation of it
''' </summary>
''' <param name="pFileName">Path to the eml file</param>
''' <returns>The IMail object</returns>
Public Function Load_Email(pFileName As String) As IMail
Dim oFileName As String = MaybeConvert_MsgToEml(pFileName)
Return MailBuilder.CreateFromEmlFile(oFileName)
End Function
''' <summary>
''' Removes all attachments from an EML file and saves it to a temporary file.
''' </summary>
@ -282,7 +293,8 @@ Public Class Email2
Dim oTempPath As String = Path.Combine(Path.GetTempPath(), Add_FilenameSuffix(pFileName, pSuffix))
Dim oCleanedPath As String = FileEx.GetCleanFilename(oTempPath)
Dim oVersionedPath As String = FileEx.GetVersionedFilename(oCleanedPath)
Dim oMail = MailBuilder.CreateFromEmlFile(pFileName)
Dim oEmlPath As String = MaybeConvert_MsgToEml(pFileName)
Dim oMail = MailBuilder.CreateFromEmlFile(oEmlPath)
oMail.RemoveAttachments()
oMail.Save(oVersionedPath)
@ -295,14 +307,11 @@ Public Class Email2
End Try
End Function
Public Function Load_Email(pFileName As String) As IMail
Return MailBuilder.CreateFromEmlFile(pFileName)
End Function
Public Function Save_AttachmentsToDisk(pFileName As String) As List(Of String)
Try
Dim oAttachmentPaths As New List(Of String)
Dim oMail = MailBuilder.CreateFromEmlFile(pFileName)
Dim oEmlFile As String = MaybeConvert_MsgToEml(pFileName)
Dim oMail = MailBuilder.CreateFromEmlFile(oEmlFile)
Dim oTempPath As String = IO.Path.GetTempPath()
If oMail.Attachments.Count = 0 Then
@ -315,6 +324,7 @@ Public Class Email2
oAttachment.Save(oVersionedPath)
oAttachmentPaths.Add(oVersionedPath)
TempFiles.Add(oVersionedPath)
Next
Return oAttachmentPaths
@ -325,7 +335,63 @@ Public Class Email2
End Try
End Function
Public Function Convert_MsgToEml(pEmailFileName As String) As String
Public Sub Clear_TempFiles()
Logger.Info("Cleaning [{0}] email temp files", TempFiles.Count)
For Each oFile In TempFiles
Try
IO.File.Delete(oFile)
Catch ex As Exception
Logger.Warn("Could not clean temp file [{0}]", oFile)
Logger.Error(ex)
End Try
Next
End Sub
Public Function Get_MessageSender(Mail As IMail) As String
Try
Dim oAddress = Mail.From.First()
If oAddress Is Nothing Then
Logger.Warn("Could not get MessageSender from Mail [{0}]", Mail.MessageID)
Return Nothing
End If
Return oAddress.Address
Catch ex As Exception
Logger.Warn("Could not get MessageSender from Mail [{0}]", Mail.MessageID)
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function Get_MessageReceiver(Mail As IMail) As String
Try
Dim oAddress = Mail.To.FirstOrDefault()
If oAddress Is Nothing Then
Logger.Warn("Could not get MessageReceiver from Mail [{0}]", Mail.MessageID)
Return Nothing
End If
Dim oMailBox = oAddress.GetMailboxes().First()
If oMailBox Is Nothing Then
Logger.Warn("Could not get MessageReceiver from Mail [{0}]", Mail.MessageID)
Return Nothing
End If
Return oMailBox.Address
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function MaybeConvert_MsgToEml(pEmailFileName As String) As String
Dim oInfo As New FileInfo(pEmailFileName)
If oInfo.Extension.ToUpper = ".EML" Then
@ -356,6 +422,7 @@ Public Class Email2
End Using
TempFiles.Add(oVersionedPath)
Return oVersionedPath
Catch ex As Exception
@ -374,4 +441,8 @@ Public Class Email2
Return $"{oFileNameWithoutExtension}{pSuffix}{oExtension}"
End Function
End Class