Messaging: Correctly save emails without attachments as eml

This commit is contained in:
Jonathan Jenne 2021-08-23 13:40:44 +02:00
parent 847c7575c4
commit 9899f6867d

View File

@ -290,7 +290,7 @@ Public Class Email2
''' <returns>The path of the new EML without attachments.</returns>
Public Function Remove_AttachmentsFromEmail(pFileName As String, Optional pSuffix As String = "") As String
Try
Dim oTempPath As String = Path.Combine(Path.GetTempPath(), Add_FilenameSuffix(pFileName, pSuffix))
Dim oTempPath As String = Path.Combine(Path.GetTempPath(), Add_FilenameSuffix(pFileName, pSuffix, ".eml"))
Dim oCleanedPath As String = FileEx.GetCleanPath(oTempPath)
Dim oVersionedPath As String = FileEx.GetVersionedFilename(oCleanedPath)
Dim oEmlPath As String = MaybeConvert_MsgToEml(pFileName)
@ -412,20 +412,28 @@ Public Class Email2
Private Function DoConvertMsgToEmlFile(pMsgFile As String) As String
Try
Logger.Debug("Converting Msg file to Eml: [{0}]", pMsgFile?.ToString)
Dim oTempPath As String = IO.Path.GetTempPath()
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(pMsgFile)
Dim oFileNameWithoutInvalidChars = Language.Utils.RemoveInvalidCharacters(oFileNameWithoutExtension)
Dim oEmlPath As String = IO.Path.Combine(oTempPath, $"{oFileNameWithoutInvalidChars}.eml")
Dim oVersionedPath As String = FileEx.GetVersionedFilename(oEmlPath)
Logger.Debug("New Path for Eml file: [{0}]", oVersionedPath)
Using oConverter As New MsgConverter(pMsgFile)
Logger.Debug("Converter created")
Dim oEmail As IMail = oConverter.CreateMessage()
Logger.Debug("Message created")
Dim oData As Byte() = oEmail.Render()
Logger.Debug("Message rendered")
oEmail.Save(oVersionedPath)
Logger.Debug("Message saved")
End Using
Logger.Info("Msg File successfully converted to Eml: [{0}]", oVersionedPath)
TempFiles.Add(oVersionedPath)
Return oVersionedPath
@ -437,16 +445,20 @@ Public Class Email2
End Try
End Function
Private Function Add_FilenameSuffix(pFilename As String, pSuffix As String)
Private Function Add_FilenameSuffix(pFilename As String, pSuffix As String, Optional pExtension As String = Nothing)
Dim oFileInfo As New FileInfo(pFilename)
Dim oExtension As String = oFileInfo.Extension
If pExtension IsNot Nothing Then
If pExtension.StartsWith(".") = False Then
oExtension = $".{pExtension}"
Else
oExtension = pExtension
End If
End If
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(pFilename)
Return $"{oFileNameWithoutExtension}{pSuffix}{oExtension}"
End Function
End Class