Is there a solution to get a .msg File from the MS Graph API? - outlook

I have the task to get several Mails with the Graph API and archive those as .msg files to a server.
Right now, i'm only able to get mails as MIME. Is there a way to transform those into .msg files?

MSG file format is specific to the Windows version of Outlook. Neither Graph nor EWS expose that file format in any way. You can create an MSG file (see How to create ".msg" file without using outlook in mfc?) and import EML into it.
Also note that Outlook will be just as happy to open and display an EML file as an MSG file, so it might make sense to work with EML files, even if they don't expose all the information and MAPI specific properties that an MSG file can store.

Related

Is it possible to embed an Outlook .msg into an application using OLE?

If you have some application with an ordinary HWND, is it possible to embed an Outlook email .msg file into the window using OLE?
It appears that, after opening an Outlook message using OpenSharedItem, you can successfully QueryInterface for IOleObject. But after that, I can't seem to successfully call any IOleObject methods.
Thanks for any input.
MSG file format is well-described in MSDN. So, you can read the content without any third-party components or interfaces. To display the content (I guess the message body) you can use any web browser (for example, WebView2). The message body (HTMLBody) is represented by a regular web page. Most probably you will also need attached files that are used in the body.
Another possible alternative to use Extended MAPI for getting item properties. For example, you can use any third-party components for simplifying the work - Redemption.

Running Excel Scripts on Outlook Attachments then sending processed files

TL;DR: I want to extract an Excel attachment from an Outlook message, automate an Excel script on the unprocessed reports, and then send the output in a new email message.
I have reports from Amazon that I have sent to a specific email on my Outlook folder each month.
I set up Power Automate to trigger whenever an email with an attachment is received in this folder.
I want to take the attachment in this Outlook email and run an Excel script that I build on Excel Desktop (not an Excel application macro) to format the data.
And then I want to send the output/result (scripted email attachment) as the body of an email out to clients.
I've gotten the first and last parts to run, but I can't get the script to process the file, that's the phase that always breaks that automation.
Things that I think might be causing the issue:
Do I need to extract the attachment from the email and save it as a file before I can run the Excel script on it?
Does the attachment need to be saved through Share Point to be later referenced by the Excel script?
Does the script need to be on the same or different cards as previous steps?
I've seen people run the Excel script through a Scope.
I've also looked at every available YouTube video to troubleshoot this: some go over certain steps in the process, but I'm having a hard time threading everything together.
Other Things I've Tried
I also tried to build a macro that does the same thing as my Excel desktop automation, and then wrote VBA code so each time a new sheet is opened the same macro is run, processing the data. But I couldn't get it to run on any new future sheet that I open, even when writing the VBA auto-open code in Module as well as This Worksheet.
I'm open to any method that will make this work.
Get email (V2) - Message ID
#{triggerOutputs()?['body/id']}
Apply to Each - Select an output from previous steps
#{outputs('Get_email_(V2)')?['body/attachments']}
Create File - File Name
#{items('Apply_to_each')?['name']}
Create File - File Content
#{items('Apply_to_each')?['contentBytes']}
Move or rename a file - File (make sure you add .xlsx for Destination File Path)
#outputs('Create_file')?['body/Id']
Run Script - File
#outputs('Move_or_rename_a_file')?['body/Id']
Get file content - File
#outputs('Move_or_rename_a_file')?['body/Id']
Send an email (V2) - Attachments Name-1
#outputs('Move_or_rename_a_file')?['body/DisplayName']
Send an email (V2) - Attachments Content - 1
#body('Get_file_content')?['body']

Which OlSaveAsType can I add to the MailItem SaveAs method?

I am getting the currently selected mailItem from the explorer and saving it to the local file system. The file is then being uploaded to a server. My problem is sometimes the server (which I have no control over) returns that the file format is invalid.
I save the mailItem with the following code:
oMailItem.SaveAs("C:\path\savedEmail.msg")
This creates a file which is 174kb in size. If I add the OlSaveAsType of olMSG then I get the same file size.
If I save the exact same email with the Outlook UI then the chosen/suggested Message format is Unicode. This produces a file of about 251kb ie. much bigger. If I save it with the above code and olMSGUnicode then I also get the same file size.
I am therefore assuming that leaving out the OlSaveAsType option saves the mailItem as an olMSG.
I am currently also assuming that this may be causing some issues with the upload to the server.
What property of a mailItem tells me which OlSaveAsType to use? Can I safely use olMSGUnicode for all saves?
Yes, you can safely use olMSGUnicode format on all modern versions of Outlook.
But the real problem is why the server (?!) returns an error that the format is invalid? Was it corrupted? Can you successfully open the same file you sent to the server? You need to figure out why that error is returned.

Outlook attachment source file path

I am using Outlook.Attachment attachment object in my C# Outlook AddIn. But the attachment object doesn't have any field which suggest what is the full source path of the file being attached.
The attachment object has the following fields/methods, but they are not helpful.
attachment.DisplayName
attachment.FileName
attachment.GetTemporaryFilePath()
There is no such thing - attachment does not have to come from the file system even it is created. After its is inserted, it is stored outside of the file system alongside messages in the store.

Parsing Outlook Emails (.msg) with Mimekit

Im attempting to parse .msg files from outlook to get the MIME content in order to save the file as an eml. I'm using Exchange Web Services to save emls as they come through a journaled inbox on the exchange server, but some emails need to be saved after the fact through an outlook add in, though getting them into an eml/MIME format seems more difficult.
The example on the mimekit github page shows it can parse a msg file, though when I attempt it, a System.FormatException is thrown. Does mimekit support msg parsing?
This is what I am doing right now:
var stream = File.OpenRead(#"C:\example.msg");
var parser = new MimeParser(stream);
var msg = parser.ParseMessage();
Any amount of insight would be incredibly helpful.
MSG format is not MIME - try to open it in Notepad. It is an OLE storage file.
To open IStorage file, use StgOpenStorage.
See Difference between a .msg file and a .eml file

Resources