Convert MSG to EML in local

Hi Everyone,

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.

Kindly help me out to proceed for the same.

Thanks in advance

Hi @Binod_Kumar_Biswal

Try this,

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.

Hi @Binod_Kumar_Biswal

Make sure to import the namespaces

  • Microsoft.Office.Interop.Outlook
  • System.IO
  • System.Runtime.InteropServices

First use assign statement to get the files

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

1 Like

@Binod_Kumar_Biswal

Please refer below thread you may get any solution by following below thread

Happy Automation!!

Hi @prashant1603765 ,

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.

Hi @Sanjay_Bhat ,

Thanks for your time.

I didn’t get ’ Save as EML format (Use 3 instead of olRFC822)’ - what is the the meaning if the last phase.

Could you please simplify?

Thank you

@Binod_Kumar_Biswal

Its just that
olRFC822 = 3 corresponds to the EML (RFC822) format used for saving emails as .eml files.
You can just ignore the comments

@Binod_Kumar_Biswal
Ok then try this,

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.