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

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.

Related

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.

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.

"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...

Modifying a copy/paste item in Outlook prior to save (C#)

I recently wrote an Outlook plug-in (for use in Outlook 2010) for a client that syncs up their user's Outlook databases with a third-party application. This works fine. However, one thing that the client wants to do is to leverage standard copy/paste functionality to duplicate records in Outlook. The issue is that I define a UserProperty for any Outlook record that has been sent to the third-party system (it stores the internal ID from the third-party system). When the client performs a copy/paste, this UserProperty is also copied (which is bad as it creates multiple records with the same third-party ID). I was wondering if there was some way to detect via code that a copy/paste was occurring and to make a modification in the record (removal of this UserProperty) prior to save. As they might perform this on Tasks, Appointments, or Contacts, I would need guidance that would apply across the board.
Per the suggestions below, I attempted to leverage BeforeItemPaste, as seen below:
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
Globals.ThisAddIn.Application.ActiveExplorer().BeforeItemPaste += new Outlook.ExplorerEvents_10_BeforeItemPasteEventHandler(Item_BeforeItemPaste);
}
private void Item_BeforeItemPaste(ref System.Object ClipboardContent,
Microsoft.Office.Interop.Outlook.MAPIFolder Target, ref bool Cancel) {
System.Windows.Forms.MessageBox.Show("Trying to paste");
}
The message appears the very first time I do a copy/paste, and then never again. I tried leveraging Application.Explorers, which theoretically gives me access to all Explorers in Outlook, but that didn't give me access to the individual ones (Appointment, Task, Contact) that I was looking for (there was only one element in that 'array' when I debugged it). Also, I tried to access those specific Explorer items by invoking MAPIFolder.GetExplorer, but this always returned null.
Try to use Explorer.BeforeItemPaste event.

Intercepting the Move Item function between source and destination

I've successfully captured the ItemMove function the way I need to (mostly) using the Redemption libraries. My next task may be impossible, but I won't know unless I ask.
Part of what I'm writing involves moving messages from the Exchange inbox to a PST and potentially removing the attachment. This is being done because our network thrashing is taking a big hit in regards to PST replication over DFSR (yes, I 'm aware of the PST/network issues and MS recommendations, but you go try explaining that to the users when you don't have money for training or new archiving software). I'd like to be able to do the following via code:
1) User selects message(s) and drags them to a PST folder
2) Add-in intercepts this, copies the messages to a temporary PST on a local drive
3) Attachments are processed in the local PST and saved to their appropriate network destination
4) Messages are moved into the true destination PST on the network.
This multi-step process is necessary as we have quotas on both drive space as well as maximum PST file size. Since PST compression doesn't happen automatically and we can't programmatically force it, I've come up with this idea for a workaround.
Ideas and inspiration are welcome as usual.
-Larry
You cannot intercept any drag/drop events in Outlook, at least not using any of the officieally supported APIs.
You will get ItemAdd event on the target folder, but it will only fire after the item was created and saved.

Categories