Saved Outlook MailItem does not yet have EntryId - c#

This is a weird one.
I have a saved Outlook MailItem (.msg file) opened from outside of Outlook, that for some reason does not yet have its EntryID set:
Some context:
This MailItem is a saved .msg file opened from Windows Explorer, not from Outlook.
In my code, I originally start at the attachment and then get the MailItem as its parent.
If I inspect the MailItem while debugging, the EntryID is either null or an empty string...
... BUT if (for example) I expand m_ObjectToDataMap until I get to _rcw and expand that object's Dynamic View - that is when the EntryID gets set.
It's as if the MailItem isn't fully loaded yet, and some inspection of the values in the debugger somehow completes the initialization of the MailItem.
I have tried:
Waiting a few seconds with System.Threading.Thread.Sleep().
parent.Save() even though I know this is not a new MailItem being composed.
refreshing the active inspector.
attempting to get the MailItem via other methods instead of getting it off the attachment.
None of these fix the problem. Why does this happen? How would I fix or get around this issue? Any help would be much appreciated.

Standalone MSG files do not have entry ids. This is to be expected.

Related

How manage mails and attachments with a c# application for windows

I should create a C# application to manage the mail and attachments that arrive to me on Office 365 (Outlook).
In the app I would like to select from which domains you need to download the mail, based on this the app downloads only the related emails with attachments and shows them to me. This way I can decide what to print or not.
I need this app because I have to record all the projects that come to me from clients, architecture projects and therefore I need to divide everything according to the client.
Can you tell me what is the best way to develop this?
I mean if it is better to create VSTO for Outlook or something else or if there is other way. I would like to start with the right method.
I had thought about installing Outlook on the client, synchronized with Office 365, creating a VSTO that takes care of copying the interested emails (selecting just the domains of interest) and putting attachments in various folders, showing the attachments in an orderly manner and grouped.
Can you suggest me the best method?
I mean at the structural level (how to design system), and not at the code level (which I think I know it).
Thanks so much
You are right, you can develop a VSTO based add-in where you may handle the NewMailEx event of the Application class. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance.
To save the attached files you can use the MailItem.Attachments property which returns an Attachments object that represents all the attachments for the specified item. Use the Attachment.SaveAsFile method which saves the attachment to the specified path.
If TypeName(myItem) = "MailItem" Then
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _
myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End Sub
See Walkthrough: Create your first VSTO Add-in for Outlook to get started quickly.

Is there a way to override this message in my outlook plugin?

I am developing an outlook plugin that will be distributed among several users and I keep getting a message saying that "A program is trying to access e-mail address information stored in outlook."
Is there a way for me to disable this on every machine that downloads my plugin or disable it for my specific plugin? The program is written in c#
Many thanks
the message
Firstly, you should never get that prompt in an Outlook addin as long as you use the Outlook.Application object passed to your addin. Never create a new instance of that object.
To work around that prompt (not that you need to) see http://www.outlookcode.com/article.aspx?id=52 for the detailed list of your options.

How to obtain the ID of the origin inbox by the mail item?

Is there a way to obtain a (unique) inbox id by the mail item, maybe over one of the mailitem properties:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem_properties.aspx
I have in my Outlook 2010 a mail that was moved in the deleted items folder. Now I am looking for the source where did it come from (my private inbox or the shared inbox).
When an items is moved to a different folder, it retains no knowledge about its previous parent. Foo all practical purposes, it is a completely new item.
I want to provide you my solution for solve this problem. In this case you can subscribe to event "BeforeItemMove" of folder and when event occured you need check a folder name. If folder name is "Delted Items" you can write information about your inbox (private or shared) in "UserProperties" of mail item. In addition to that you need subscribe to event "ItemAdd" of delete folder. As a result you will receive information about your inbox.

C# Outlook Addin - Open msg Files

Ok i try to explain the Problem.
I've wrote an Outlook Extension to match Mails, Task and so on to an ERP System.
It works just fine but i have 1 Problem:
I save all Outlook Elements whitch could be mapped to ERP Customers, Suplliers and so on in the Document Database of the ERP System. So Every Project/Team member could open the latest Version of the Outlook Item.
The first time a user Opens the Item every Thing is fine, the second time they couldn't open the File because it is already open in Outlook.
see social.msdn.microsoft.com
I know that Outlook "saves" a temporay item for every msg File but i could not release the object.
And i don't want to Quit Outlook i just want to release the msg File after closing it so the User can reopen it WITHOUT CLOSING Outlook.
But i haven't found a way to release it without calling Quit.
Or is there an Option to redisplay the the currently opend Item? At the Moment i have no idea how to reference those already opened msg files.
Building an Dictionary of those Item is quite heavy i think.
#SuperBiasedMan - Sure, i tried 2 ways so far:
// 1. Just open File in corresponding Viewer
System.Diagnostics.Process.Start([PathToMsgFile]);
// 2. Using SharedItem Method to get an item Object
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
var item = app.Session.OpenSharedItem([PathToMsgFile]) as Microsoft.Office.Interop.Outlook.MailItem;
item.Display();
With both the Item is Displayed as expected, when the User Closes this File and try to reopen it (without restarting Outlook)the user get an error Message:
"Cannot open file: [PathToMsgFile] .msg. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click Properties to check your permissions for the folder."
Outlook keeps the Item open.
The only way i found to release it is to call:
app.Quit();
The Discussions (see Link above) conclusion is that Outlook "saves" an temporary Outlook Element when opening Msg files. I search an possibility to close/delete/release or whatever this temporary Object or to reopen it without restarting Outlook
#Dmitry: The file comes from a remote machine

"Best" way to get an Outlook MailItem from .msg file

To pass from a .msg file to its related Outlook MailItem I found and tried these two ways:
Outlook.Application oApp; // --> Outlook Application
Outlook.MailItem oItem; // --> Outlook MailItem
string file= #"C:\PWS\myMail.msg";
oApp= (Outlook.Application)new Outlook.Application();
// way #1
oItem= (Outlook.MailItem)oApp.CreateItemFromTemplate(file);
// or way #2
oItem= (Outlook.MailItem)oApp.Session.OpenSharedItem(file);
What is the difference between these two ways? I need to open the .msg and then use the resulting MailItem (to get some properties as 'SenderEmailAddress' or the email attachments)... what should I use? At the moment they are the same to me...
The third way is to use run .msg file programmatically. A default application (outlook) should be opened in that case. For example:
string file= #"C:\PWS\myMail.msg";
Process.Run(file);
Be aware, you can't run multiple instance of Outlook. So, the message will be opened in the existing Outlook instance (if any).
Both methods (#1 and #2) allow to open the saved message in Outlook. But they have minor differences:
The CreateItemFromTemplate method of the Application class creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item. I'd also like to draw your attention to the fact that new items will always open in compose mode, as opposed to read mode, regardless of the mode in which the items were saved to disk.
The OpenSharedItem method of the Namespace class opens a shared item from a specified path or URL. See How to: Import Saved Items using OpenSharedItem for more information.
It is up to you which way to choose based on the information listed above...

Categories