Get email folder from MailItem via MAPI interface - c#

I'm creating a program to automatically generate reports from incoming email attachments and it is almost complete save for one area. The incoming emails are automatically filtered into folders which differentiate which client and server they originate from. I can't figure out how to get the path of the folder from the email Item.
I'm using the NewMailEx event to call the method below and this.AppNamespace and this.ReportFolder are confirmed to be instantiated properly.
void AppClass_NewMailEx(string EntryIDCollection)
{
Outlook.MailItem Item = (Outlook.MailItem)this.AppNamespace.GetItemFromID(EntryIDCollection, this.ReportFolder.StoreID);
string FolderName = ""; //How do I get this?
}
The MSDN on MailItem is here. Am I missing something or approaching this the incorrect way?

I think there's a Parent that you can check - it should return a MAPIFolder that you can check the Name of.

Related

Using Outlook interop, how can I save an attached .msg that's open in Inspector?

I'm working on an Outlook add-in button that saves the active mail item to the filesystem.
It works fine except on .msg attachments:
When the user double-clicks message.msg, a new Outlook inspector window opens, showing that email.
Next, the user clicks my add-in button to save that email to the filesystem. At this point, an exception is thrown:
System.UnauthorizedAccessException: 'You don't have appropriate permission to perform this operation.'
How can I save an attached email to the filesystem from an Inspector window?
TL;DR: Use mailItem.Copy().SaveAs().
The problem is that the attached email is read-only. For some reason, interop forbids calling SaveAs on read-only emails.
If you call Copy(), the new instance of the mail item is not read-only and can be saved to the disk without issue.
Note: Before copying the mail item, check whether it's truly read-only, because copying can be slow depending on the mail item's size. Use GetProperty to determine this; MailItem unfortunately doesn't have a ReadOnly property built in.
bool isMailItemReadOnly = mailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FF70003") == 0;
string path = #"C:\MyFolder\message.msg";
if (isMailItemReadOnly)
{
mailItem.Copy().SaveAs(path);
}
else
{
mailItem.SaveAs(path);
}

Outlook add-in error: The operation cannot be performed because the message has been changed

i am trying to create my own add-in for Outlook. My point is to extract some data from mails and then move these mails to Archive folder. When i open unread mails it works as i expect, but i got an error when i open unread mails in the moment when i am trying to move mail to Archive folder. I got an instance of mail from inspector. Here is some code.
Outlook.MailItem mail = inspector.CurrentItem as Outlook.MailItem;
var email = mail.UserProperties.Session.CurrentUser.Address;
Outlook.NameSpace ouNs = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.MAPIFolder baseFolder = ouNs.Folders[email];
var archiveFolder = findFolderRecursive(baseFolder, archiveFolderName);
mail.Move(archiveFolder);
Messages returned from the inspector disallow some methods. Try to track the Inspector.Close event, store the message entry id in a variable, and enable a timer (use Timer class from the Forms namespace - it runs on the same thread). When the timer fires, disable it, open the item by its entry id using Namespace.GetItemFromID, then move it.

C# get mail and its attachment from outlook

I've made program which one of the purposes is to open OutLook client where user can write his email, add attachments etc. After sending I want to get all attachments which were added to email, as well as all email in my program.
I tried to handle close event
((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)email.oMsg).Close += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CloseEventHandler(GetAttachmentsInfo);
and then
if (email.oMsg.Attachments.Count > 0)
{
foreach (Microsoft.Office.Interop.Outlook.Attachment at in email.oMsg.Attachments )
{ attachments.Add(at); }
}
email is OutlookEMail
oMsg is Email Item
attachments is List<Attachment>
but when I close client Attachment throws exception that cannot find object. as well as these in List.
I know that Microsoft.Office.Interop.Outlook.Attachment is not a file, but only something like path to this file containing it name and size.
So question : is it possible to save attachments after client is closed in my program?
(Without using Email.SaveAttachments os SaveEmail methods, because it uses time and computer space)?
Here are two other options you could try:
1. You could listen for MailItem additions to the SentItems Folder via Folder.ItemAdd.
Outlook.Folder sentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
sentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(GetAttachmentsInfo);
2. You could attach to the MailItem.Send event.
((Outlook.ItemEvents_10_Event)MailItem).Send += new Outlook.ItemEvents_10_SendEventHandler(GetAttachmentsInfo);

Outlook 2010 - C# - Get the account associated to a mail

I'm creating an Outlook add-in that can save selected emails to an external database.
Using the Office.IRibbonControl I can get the list of the selected email, but I need to know to which account those mails are associated.
I mean, if Outlook get messages from toto#exemple.com and from otot#exemple.com, when I want to save a message I need to know that information.
I can't use the informations like sender / receiver because it can be an outcome like an income email.
Currently, the only I have found is using the current folder path..
public void SayHello(Office.IRibbonControl control)
{
MessageBox.Show(
"Folder: " + (control.Context as Outlook.Explorer).CurrentFolder.FolderPath,
"Test",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
But the method isn't good enough. If I open a message (in a separated window) and then I change the current folder, it fails.
Also, Outlook.Explorer.CurrentAccount does not work as I expected.
So here is my question :
How can I access the related account having a Outlook.MailItem object ?
You can get the parent folder (MailItem.Parent) of an Outlook.MailItem to determine its Folder path (Folder.FolderPath).
Outlook.Folder parent = MailItem.Parent as Outlook.Folder;
string itemPath = parent.FolderPath;

Set a MailItem as sent before calling SaveAs in Outlook Addin with C#

I am building an Outlook 2010 addin to integrate it with some business software and have trapped the ItemSend Event. I check if it is a MailItem and if it is I call the SaveAs function to save it as a .msg to the file system (in the users temp folder).
void Application_ItemSend(object Item, ref bool Cancel)
{
if(Item is Outlook.MailItem)
{
Outlook.MailItem mailitem = (Outlook.MailItem)Item;
string filename = "somefilename.msg";
string path = System.IO.Path.GetTempPath();
string fullPathName = path+filename;
mailitem.SaveAs(fullPathName, Outlook.OlSaveAsType.olMSG);
}
}
I go on to read the file contents and send the file to the server using webservices. It all works fine.
The issue I have is if I go and open the file that it saves, then Outlook opens it as a message still being composed and the user could very easily click the Send button again.
Is there a way to flag that the item has been sent before it gets saved so when opened after the fact it opens as a readable email rather than an in-composition email?
Take a look at this post (and additionally at the last answer on that page), this might help you.
(Makes use of ItemAdd event)

Categories

Resources