I want to convert the msg files stored in one folder to eml format and save the eml file in other folder and download the attachment present in the msg file to another folder.
Get all .msg files from the source folder using the “For Each File in Folder” activity. Read each .msg file using the “Read Outlook Mail Message” activity or a third-party package like Aspose.Email. Save the mail as an .eml file using the “Save Mail Message” activity, specifying the destination folder. Extract attachments by iterating through MailMessage.Attachments using a “For Each” loop. Save each attachment to the target folder using the “Save Attachment” activity. Ensure all necessary folders exist before execution.
msgFiles = Directory.GetFiles("C:\SourceFolder", "*.msg") were msgFiles is array of Strings
index = 0
I just tried with this invoke code (LLM Generated with some changes) and it works, pass the msgFiles and index as arguments
Dim outlookApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application
Dim msg As Microsoft.Office.Interop.Outlook.MailItem = DirectCast(outlookApp.Session.OpenSharedItem(msgFiles(index)), Microsoft.Office.Interop.Outlook.MailItem)
' Save as EML format (Use 3 instead of olRFC822)
Dim emlPath As String = "C:\Users\ eml folder path" & System.IO.Path.GetFileNameWithoutExtension(msgFiles(index)) & ".eml"
msg.SaveAs(emlPath, 3) ' 3 corresponds to olRFC822
' Save Attachments
If msg.Attachments.Count > 0 Then
Dim attachmentPath As String = "C:\Attachment path\"
For i As Integer = 1 To msg.Attachments.Count
msg.Attachments.Item(i).SaveAsFile(System.IO.Path.Combine(attachmentPath, msg.Attachments.Item(i).FileName))
Next
End If
msg.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)
outlookApp.Quit()
Please mark it as a solution if it helps !
Happy Automation
Thanks for your time and support but I can’t use any third party packages, can you please suggest me any turnaround without using any custom package.
You can use the “For Each File in Folder” activity to loop through the .msg files in the source folder. The “Read Outlook Mail Message” activity can be used to read each .msg file directly from the disk without the need for external packages. After reading the file, the “Save Mail Message” activity will allow us to save the message as an .eml file in the destination folder. You can then use a “For Each” loop to iterate through the attachments in the MailMessage.Attachments collection and save each attachment using the “Save Attachment” activity.