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...
Related
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.
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.
I want to take backups of all emails from SQL Server database and need to create it's backup file using C# code in outlook compatible format. So that emails can be restored in outlook software.
Please help
Till now we have created one desktop application and we have table containing emails which has some custom fields also as per our need.
We have done some exploration on it and found given below links -
Can I read an Outlook (2003/2007) PST file in C#?
http://www.c-sharpcorner.com/article/outlook-integration-in-C-Sharp/
https://www.add-in-express.com/creating-addins-blog/2013/12/20/create-outlook-files/
Can I read an Outlook (2003/2007) PST file in C#?
My problem is that we have some custom fields in database so how they will get stored in outlook data files
Email table structure is given below -
You can use Outlook Object Model and its Namespace.AddStore/AddStoreEx methods to add new or existing PST file to a profile and then (given the returned Store object) populate it with folders and emails. To store custom properties, use MailItem.UserProperties collection.
Note however that OOM will not work in a service - you'd need Extended MAPI (C++ or Delphi) or Redemption (I am its author - any language) for that. Creating items in the sent state can also be a challenge. If using Redemption is an option, it exposes RDOSession.LogonPstStore method that create (and deletes) a temporary profile configured to work with the specified PST file. It can be used i na service. No existing Outlook profiles are affected.
Redemption.RDOSession session = new Redemption.RDOSession();
Redemption.RDOPstStore store = session.LogonPstStore(PstFileName);
Redemption.RDOFolder folder = store.IPMRootFolder.Folders.Add("Backup folder");
RDOMail item = folder.Items.Add("IPM.Note");
item.Sent = true;
item.Subject = "test";
item.Body = "test body";
item.Recipients.AddEx("The User", "user#domain.demo", "SMTP");
item.UserProperties.Add("My custom prop", olText).Value = "custom prop value";
item.Save();
Your description is still not precise enough.
Do you want to store individual e-mails from outlook into database and eventually get them back as e-mails in Outlook?
Then it looks like you have all you need.
MailItem in Outlook has more-less properties like you in database.
Then conceptually it should look like this:
Enumarate MAPIFolder and for each e-mail store properties inside database and then delete e-mail.
In case of restoring read database record, create new MailItem and add it to folder.
BUT:
I see some problems with field types - i.e. EmailTo nvarchar(100) is far too small.
Additionally you don't have all fields to restore e-mail 1:1.
So i.e. storing msg file could be good option (maybe additionally to data you are retrieving).
Please specify more details then I could also answer in better way.
EDIT:
According to your clarification (still not sure if I understand correctly):
Simpliest way would be to use outlook for this process.
Create in outlook pst folder, create e-mails inside and then backup complete pst file (then you have all in one file) or export individually e-mails to .msg files (then 1 file per e-mail).
Trying to write directly from your application to pst file or to msg file could be very hard since format of those files are not described.
Please precise if you want to use Outlook for this process or not.
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
I am working on a WPF app (written in C#) that searches and serves data from the active directory. Searching for a user will fetch all phone numbers for the user and display on the app window.
How can I open the outlook contact card by the click of some icon or the username or something?
The Outlook Contact Card is part of Microsoft Outlook and (as far as I know) not a public control, so you can not simply 'open' it unless you are creating an Outlook add-in.
Using the Active Directory's information you could recreate it.
By Outlook Contact Card I assume you mean a vCard (*.VCF file). It is actually nothing more than a text file. You can easily recreate it. For example:
Create a StringBuilder instance and write the contents of the .VCF file to it.
var vcf = new StringBuilder();
vcf.Append("TITLE:" + contact.Title + System.Environment.NewLine);
//...
Afterwards you can save it to a file.
var filename = #"C:\mycontact.vcf";
File.WriteAllText(filename, vcf.ToString());
Most properties are easy to figure out.
A small example:
BEGIN:VCARD
FN:Mr. John Smith
TITLE:Developer
ORG:Microsoft
BDAY:1979-12-10
VERSION:2.1
END:VCARD
If you want to include an image you have to base 64 encode it.
If you open this newly created file:
Process.Start(#"C:\mycontact.vcf");
Then it should be opened by the application that is configured by default to handle this file extension.
Wikipedia contains more information about the contents of a vCard:
http://en.wikipedia.org/wiki/VCard
It seems that outlook is using a Lync Feature.
Even if not exactly the Same you can use the Lync SDK in your wpf app if you use Lync.
http://msdn.microsoft.com/en-us/uc14trainingcourse_2l_topic2#_Toc273951814