Open Specific MailItem in Outlook from C# - 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).

Related

Move multiple mails to a folder in a different store

I'm using Outlook Redemption library (http://www.dimastr.com/redemption/home.htm) for my Outlook AddIn. I want to move multiple mails from an exchange account to a PST store.
onlineAccountFolder.Items.MoveMultiple(onlineEntryIds, targetFolderInPstStore);
The source folder mails were cut from the Exchange account, but not pasted in the target folder. They are gone.
I tried the same operation on an Exchange account folder in the same store and the move operation was successful. The items were moved to the target folder.
There's no overload of the 'MoveMultiple' method where I can define a StoreID.
I had no problem with the following script executed from OutlookSpy (I am its author - click “Script Editor” button on the OutlookSpy toolbar, paste the script, click Run.
The script moves the messages selected in Outlook to a folder returned by the PickFolder method. Works as expected with both PST and Exchange target folders.
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
dim messages()
set sel = Application.ActiveExplorer.Selection
redim messages(sel.Count-1)
for i = 1 to sel.Count
messages(i-1) = sel.Item(i).EntryID
next
set targetFolder = Session.PickFolder
set sourceFolder = Session.GetFolderFromID(Application.ActiveExplorer.CurrentFolder.EntryID)
sourceFolder.Items.MoveMultiple messages, targetFolder
Use the Move method of the RDOMail class to move items between stores in Outlook.

FileNotFoundException when trying to add an Attachment to Outlook Mailitem in C#

I am trying to send a mail on Outlook 2013 startup programmatically that has programmatically added attachments. Then the programm takes these attachments and creates a Password-locked Zip File and also adds this File as attachment. My Problem is, that I keep getting an
"System.IO.FileNotFoundException"
when executing
mail.Attachments.Add(pathToAttachment1);
I thought the path might not exist, but I can add this Path to a new zip-directory (using the same String), so it seems to be a Problem with the
Attachments.Add(Object)-Method.
MSDN says it is totally fine to give it a Pathname as String. In VBA it even worked, but in c# not (for whatever reason). Does somebody have an idea what i am doing wrong?
The Code i want to execute:
String pathToAttachment1 = #"C:\Testfile.txt";
//create a new ZipFile
ZipFile zipAttachment = new ZipFile("EncodedAttachments.zip");
zipAttachment.Password = "1234";
//Create a new MailItem
Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem);
//set Receiver, Body and Subject for the MailItem
mail.To = "foo";
mail.Body = "This is the body.";
mail.Subject = "This is the Subject.";
//This is working:
zipAttachment.AddFile(pathToAttachment1);
//This is the line where the Exception is thrown:
mail.Attachments.Add(pathToAttachment1);
This code is called in the Startup-Method handling the Outlook Startup Event. Does anyone have an idea what i might do wrong or could change to make it work?

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

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;

Using Outlook API to get to a specific folder

I'm trying to write some C# code to get to a specific folder in an Outlook mailbox. I have the following code:
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
Outlook.Recipient oRecip = oNS.CreateRecipient("AccountNameHere");
oRecip.Resolve();
if (oRecip.Resolved)
{
oInbox = oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderInbox);
oInboxMsgs = oInbox.Items;
ItemCount = oInboxMsgs.Count;
Console.Writeline("There are {0] items.", ItemCount.ToString())
}
This will get me to to the "Inbox" folder. I'm trying to get to a folder at the same level as the Inbox folder. I believe I need to use GetFolderFromID instead of GetSharedDefaultFolder, but I don't understand how to use it. Is there a way to iterate through all the top level folders? How might I determine the EntryID and StoreID of the folder?
Thanks!
You can use the Folders collection member of the Outlook.NameSpace object. That way you can iterate through the collection and find your folder by it's name. In case you still want to use GetFolderFromID, you can use OutlookSpy tool to get the EntryID and StoreID values.

Categories

Resources