Accessing Outlook journal entries from C# - c#

For the last 7 years I've been using the Journal entries feature of Outlook to generate daily engineering log entries detailing what I've been working on day-to-day. It looks like MS are 'retiring' the Journal feature in the near future and I'd like to retain my 1000+ log entries.
Ideally I would like to export all the Journal entries into a SQLite database and use it as the basis of a new engineering log tool.
Reviewing the available MSDN documentation I was only able to find scant details on programmatically accessing Outlook data. I did identify a number of pay for/open source NuGet packages for accessing Outlook data, but none of them seemed to cover Journal entries.
Has anyone recommend a NuGet package/GitHub project which would handle this, or even a code snippet highlighting accessing Outlook Journal entries ?

JournalItem object can be accessed using Outlook Object Model just fine. Open the default Journal folder using Application.Session.GetDefaultFolder(olFolderJournal), loop through all items in that folder (cast them to JournalItem).

You can use the GetDefaultFolder method to get the Journal folder which contains corresponding items. Just pass the olFolderJournal value. For example, the following VBA macro which can be run on Outlook shows how to get the Journal folder:
Public Sub OpenJournalEntry()
Dim JournalFolder As Folder
Dim Item As Object
Set JournalFolder = Session.GetDefaultFolder(olFolderJournal)
Set Items = JournalFolder.Items
Set Item = Items.Item(1)
Item.Display
End Sub

Related

Auto Generated excel document producing server error on custom properties

My team has coded auto generating excel reports of business data in c# and openxml.
This is fine when opened from a local drive, but when opened from sharepoint it produces the following error
We believe its something to do with with these custom properties:
MSIP_Label_xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_SiteId
,MSIP_Label_xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_Method
,MSIP_Label_xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_ContentBits
,MSIP_Label_xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_Enabled
,MSIP_Label_xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_Name
These appear to be initialised and populate when saving normal files onto one drive automatically and do not appear in files that are not. We have getting c# to insert them directly. We copied the global identifier('xxxxxxx') from other files on our one drive as it seems to be uniform. Same for the SiteId field content, but the error still persist. Does anyone know how I can resolve this or direct me to a useful avenue of investigation
Adding manually above labels from within excel produced the same error.

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.

Getting a MacOS folder/file ID C#

Edit: I just read that Microsoft products often delete and create a new file when saving, so we may need a completely different solution than below...
I'm writing some code to track file and folder changes on both Windows and Mac while my app is running, but it also needs to detect changes if it's not running.
I'm able to do this on Windows by obtaining folder and file ID's using the following post:
Getting a Folder ID C#
Now I need a solution for Mac, but after searching I've come up a bit empty. I can't find anything regarding a system-wide folder/file ID. The closest I came to a solution was using some sort of bookmark that could track the file as described below, but I haven't started yet:
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW10
Seems I would have to store these bookmarks as Alias files to recall when the program starts back up.
Are there any other options for getting a global file/folder ID on Mac using C#?
You can use NSFileManager.GetAttributes to obtain an "inode" of the file (as Jason's comment calls out)
Note: Of course inodes change if moved across volumes, if a program that edits a file performs a delete and recreate vs. a replace, etc...
Example:
var ns = NSFileManager.DefaultManager;
var attribs = ns.GetAttributes("/Users/sushi/Desktop/foo.txt");
Console.WriteLine(attribs.SystemFileNumber);
File.Move("/Users/sushi/Desktop/foo.txt", "/Users/sushi/Desktop/foo2.txt");
var attribs2 = ns.GetAttributes("/Users/sushi/Desktop/foo2.txt");
Console.WriteLine(attribs2.SystemFileNumber);

How to create SQL Server data backup file in Outlook Compatible format by C# code

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.

Inventory Remote Computers for PST's connected to Outlook?

I've been searching for a solution for a few days now, I've looked through the MSDN for Interop.Outlook and I think I've found what I need, but can't seem to implement it properly.
Here's the code I've came up with based on something similar I saw in VBA.
class Program
{
Stores allstores = new Stores();
Store store;
static void Main(string[] args)
{
foreach (var store in allstores)
{
MessageBox.Show(store.FilePath);
}
}
}
`
This essentially needs to cycle through a list of computers, and run this code on their outlook(some 2003, some 2007) in order to inventory all connected PST's in each outlook profile. I'm sure there's more code to this, but I can't get this portion to work at all. There seems to be a lack of information on inventorying Outlook data files, most of it is reading e-mails from the mailboxes and not the data file itself.
If someone could shed some light on what I'm overlooking, It'd be greatly appreciated.
EDIT:
I've actually made a working piece of code now, however I have a problem with compatibility. The program works as designed in Office 2010/2007, however it crashes when accessing a 2003 version. I imagine I need to use the Microsoft Office Object 11.0, however I only have Microsoft Office Object 12.0 listed - is there a way to get the 11.0 reference?
This may be of use, pretty thorough object model comparison and development guide.
There is no reason to actually log to any Outlook profiles (which might require an authentication prompt). All the information is already in the profile section in the registry. The exact location is Outlook version specific, and the profile section guids are generated randomly, so the documented profile management API (IProfAdmin etc.) is the way to go, but unfortunately it is Extended MAPI and requires C++ or Delphi.
As of Outlook 2007, Outlook Object Model exposes Namespace.Stores collection and Store.FilePath property, so you can loop through all stores and read the FilePath property for each store (be sure to filter out OST files).
Note that there can be multiple Outlook profiles (as shown in Control Panel | Mail | Show Profiles), but Outlook can only work with one profile at a time, so to use a different profile, you'd need to close Outlook.
If using Redemption is an option (I am its author), it includes ProfMan library (accessible in any language) which will let you extract all PST file locations from all local profiles without actually logging in.:
'Print the path to all the PST files in all profiles
PR_PST_PATH = &H6700001E
set Profiles=CreateObject("ProfMan.Profiles")
for i = 1 to Profiles.Count
set Profile = Profiles.Item(i)
set Services = Profile.Services
Debug.Print "------ Profile: " & Profile.Name & " ------"
for j = 1 to Services.Count
set Service = Services.Item(j)
If (Service.ServiceName = "MSPST MS") or (Service.ServiceName = "MSUPST MS") Then
'there should be only one provider for this service
'but we should really loop through all the providers
Debug.Print Service.Providers.Item(1).ProfSect.Item(PR_PST_PATH)
End If
next
next

Categories