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

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;

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);
}

how to access my yahoo or gmail account in outlook (c#)?

I have 3 data files:
Outlook data file (is empty)
Gmail data file (from my gmail pop3)
Yahoo data file (from my yahoo imap)
I can access the Outlook data file inbox (which is always empty, I don't know how to automatically move from my google and yahoo account to my outlook data file) with
this code:
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
I have 2 questions:
How can I make my gmail and yahoo account automatically move to my outlook data file?
How to code to access my inbox in my gmail and yahoo account?
I have tried this function to make return to my MAPIFolder:
public Outlook.MAPIFolder GetInbox(string userName)
{
Outlook.Application oOutlook = new Outlook.Application();
Outlook.NameSpace oNs = oOutlook.GetNamespace("MAPI");
Outlook.Recipient oRep = oNs.CreateRecipient(userName);
Outlook.MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, Outlook.OlDefaultFolders.olFolderInbox);
return inbox;
}
But it didn't work. help me pleaseee..
I'm not sure I understand your first question, would you like to join all the data files into the main outlook data file?
About your second question, Outlook data files are represented by stores in the API, and if you call GetDefaultFolder on the Session object, you'll always end up with getting the default store's default folder (Inbox of your Outlook data file).
You can list all the store files and use GetDefaultFolder on them to retrieve the inbox folders for each store/data file:
Outlook.Stores stores = this.Appliction.Session.Stores;
foreach( Store store in stores )
{
Outlook.Folder inboxOfStore = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//Do stuff with your inbox folder.. Check store properties for infomation on which data file this store represents
}
See documentation for more information:
http://msdn.microsoft.com/en-us/library/office/bb176405%28v=office.12%29.aspx

Open Specific MailItem in Outlook from C#

I want to open a specific email in Outlook from my C# winforms application.
At the moment I have got the following code:
//...Get Folder & Entry ID for last Email in Sent Box
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder mySentBox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.MailItem myMail = ((Outlook.MailItem)mySentBox.Items[1]);
string guid = myMail.EntryID;
string folderEntryID = mySentBox.EntryID;
string folderStoreID = mySentBox.StoreID;
string mailAddressee = myMail.To;
MessageBox.Show(mailAddressee);
//...Attempt to Open that Email at a later date
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
getItem.Display();
The first an second parts of the code will be run at different times, although they are in the same Method for testing at the moment.
The first part obtains the relevant IDs for the mst recently sent email from Outlook. This part seems to work fine as evidence my the Messagebox I've built in.
The second part however is not working and I'm struggling to find the right code to access and open that specific mailItem having obtained its ID and folder location in the first part.
Anyone able to complete this little project for me please.
Done it at last by replacing the following lines of code . . .
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
with this . . .
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(guid, folderStoreID);
You can not use EntryId with the _Items.Find method. The MSDN reference is here.
There is also a remark which could be interesting for your project:
"The Entry ID changes when an item is moved into another store, for
example, from your Inbox to a Microsoft Exchange Server public folder,
or from one Personal Folders (.pst) file to another .pst file.
Solutions should not depend on the EntryID property to be unique
unless items will not be moved."
MailItem.EntryID Property (Outlook).

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);

Get email folder from MailItem via MAPI interface

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.

Categories

Resources