Creating a new Outlook store - c#

I am creating a pst file in Outlook using C#, and Addin express.
The new pst file ("My Inbox") should have Inbox,Sent Items, Outbox.
Outlook.Folders olFolders = olNamespace.Folders;
foreach (Outlook.MAPIFolder olTmpFolder in (IEnumerable) olFolders)
{
if(olTmpFolder.Name == "My Inbox")
{
olTmpFolder.Folders.Add("Inbox", Outlook.OlDefaultFolders.olFolderInbox);
olTmpFolder.Folders.Add("Sent", Outlook.OlDefaultFolders.olFolderSentMail);
olTmpFolder.Folders.Add("Outbox", Outlook.OlDefaultFolders.olFolderOutbox);
}
}
But I can only see the Inbox folder .
If there are better ways please let me know.
Thanks
Sujay

Try this and let me know if it works:
Outlook.Folders olFolders = olNamespace.Folders;
foreach (Outlook.MAPIFolder olTmpFolder in (IEnumerable) olFolders)
{
if(olTmpFolder.Name == "My Inbox")
{
olTmpFolder.Folders.Add("Inbox", missing) as Outlook.Folder;
olTmpFolder.Folders.Add("Sent", missing) as Outlook.Folder
olTmpFolder.Folders.Add("Outbox", missing) as Outlook.Folder
}
}

Related

C# -- Read Emails from non default accounts in Outlook

I am trying to simply read and print the contents of emails from a specific folder and another inbox in Outlook using C# (not my default inbox). I'm finding it difficult to find examples of this on the web and have failed on my own. I know how to print the emails of the default account as well, just not additional ones.
My code here simply iterates over a list of all the inboxes and prints their names out. The one I want to read is the first element in the collection. I appreciate any help with this issue. Thanks.
using System;
using System.Collections;
using Microsoft.Office.Interop.Outlook;
public class StorageReplies {
public static void Main() {
Application app = new Microsoft.Office.Interop.Outlook.Application();
_NameSpace ns = app.GetNamespace("MAPI");
Folders folders = ns.Folders;
foreach(MAPIFolder f in folders) {
Console.WriteLine(f.Name);
}
}
}
You could obtain the Store for each folder, and then call GetDefaultFolder method to obtain the inbox folder for the corresponding store like this:
foreach (MAPIFolder f in folders)
{
MAPIFolder inbox_folder = f.Store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach (MailItem item in inbox_folder.Items)
{
//Access item here
}
}
However, instead of doing that, it makes sense to loop through the Stores property directly like this:
Stores stores = ns.Stores;
foreach (Store store in stores)
{
MAPIFolder inbox_folder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach (MailItem item in inbox_folder.Items)
{
//Access item here
}
}

The Outlook data file (.pst) failed to load for this session

i am using Microsoft.Office.Interop.Outlook Version 12.0.0.0 to read my outlook pst file but when compiler reaches this code outlookNs.AddStore(pstFilePath); it gives exception that
"The Outlook data file (.pst) failed to load for this session." i have tried outlookNs.AddStoreEx(pstFilePath); also but the error was same ....any sugession ??
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Outlook;
namespace PSTReader {
class Program {
static void Main () {
try {
IEnumerable<MailItem> mailItems = readPst(#"C:\temp\PST\Test.pst", "Test PST");
foreach (MailItem mailItem in mailItems) {
Console.WriteLine(mailItem.SenderName + " - " + mailItem.Subject);
}
} catch (System.Exception ex) {
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
private static IEnumerable<MailItem> readPst(string pstFilePath, string pstName) {
List<MailItem> mailItems = new List<MailItem>();
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath);
MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
// Traverse through all folders in the PST file
// TODO: This is not recursive, refactor
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders) {
Items items = folder.Items;
foreach (object item in items) {
if (item is MailItem) {
MailItem mailItem = item as MailItem;
mailItems.Add(mailItem);
}
}
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
return mailItems;
}
}
}
I got the same issue, and the below is what I did.
When you are saying outlookNs.AddStore, you have to give path and the file name.
and then in the outlookNs.Stores, the variable pstName should have any extension as .pst, you have to remove it.
Below is the sample on how I got it worked.
public class Mail
{
public MailItem mailItem { get; set; }
public String path { get; set; }
}
public static class Mails
{
public static List<Mail> readPst(string pstFilePath, string pstName)
{
List<Mail> mail = new List<Mail>();
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath + pstName);
string storeInfo = null;
foreach (Store store in outlookNs.Stores)
{
storeInfo = store.DisplayName;
storeInfo = store.FilePath;
storeInfo = store.StoreID;
}
MAPIFolder rootFolder = outlookNs.Stores[pstName.Substring(0,pstName.Length-4)].GetRootFolder();
// Traverse through all folders in the PST file
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders)
{
ExtractItems(mail, folder);
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
return mail;
}
private static void ExtractItems(List<Mail> mailItems, Folder folder)
{
Items items = folder.Items;
int itemcount = items.Count;
foreach (object item in items)
{
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
Mail mail = new Mail();
mail.mailItem = mailItem;
mail.path = folder.FolderPath + folder.Name;
mailItems.Add(mail);
}
}
foreach (Folder subfolder in folder.Folders)
{
ExtractItems(mailItems, subfolder);
}
}
}
Is it possible the PST is read only or already open in a another process? Outlook needs readwrite permissions to the PST file, regardless of what you intend to do with it.
I ran into this problem today! Searched high and low for an answer without success.
Eventually, we figured out that my user login credentials had read-permissions to the folder structure containing the PST file, while another user in my organization did not. She received the above error until we changed the permissions on the directory to allow read access, and the problem disappeared.
In my case, neither credentials were the issue nor that the PST file was opened in another process. I could not figure out what caused this but ...
... solution for me was to open Outlook->New Items->More Items->Outlook Data File...
In the popup, select Outlook data file (.pst) and hit OK
This will open Windows Expolorer, navigate to the location where your PST used to be created, name your PST file the way you used to name it and save it to that location. Make sure you save as type Outlook Data File (*.pst).
Restart Outlook
This should restart outlook and your PST should be loaded now. After this, I was also able to delete all from the location where my PST was located (including PST) and restarting outlook would not create it without any issues.

Change the status of mail as Read in outlook

i have written a code that will read all unread mails from Outlook 2010 and write them in a file. After that i want to change the status of the mails as Read in outlook.
How do i do it?
I am using Interop for accessing the mails.
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.Items unreadItems = null;
app = new Microsoft.Office.Interop.Outlook.Application();//.CreateItem(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
ns = app.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
unreadItems = inboxFolder.Items.Restrict("[Unread]=true");
Here are some link that could help you:
http://msdn.microsoft.com/en-us/library/aa289167%28VS.71%29.aspx (it is for Outlook 2003 but it still contains very usefull info)
http://msdn.microsoft.com/en-us/library/ms268731%28VS.80%29.aspx
I've a code sample that could help you:
OutLook.Application oApp;
OutLook._NameSpace oNS;
OutLook.MAPIFolder oFolder;
OutLook._Explorer oExp;
oApp = new OutLook.Application();
oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
OutLook.Items items = oFolder.Items;
foreach (OutLook.MailItem mail in items)
{
if (mail.UnRead)
{
mail.UnRead = false;
mail.Save();
}
Marshal.ReleaseCOMObject(mail);
}
Marshal.ReleaseCOMObject(items);
// Dont forget to free all other object, using Marshal.ReleaseCOMObject then close oApp
Please note I've not tested if it works or even compile.
On of general rule with outlook dev is that you need to release ALL com object otherwise you can have strange behavior (save popup when closing outlook app, or even the outlook never close etc.)
EDIT:
I would advice to you indeed use the Restrict method to get only unred mail, because my snippet above will loop in all emails which could be unnecessary and not performant.
Set the OutLook.MailItem's UnRead property to false

How to get folder by path in multiple Accounts Outlook c#

I have 2 accounts added to outlook , two seperate pst files. You will get clear idea with image below :
First i prompt user select folders from the outlook which ones to read, image attached for better understanding:
At the end i have the folderpaths in a list, same as in the image.
Now i want to read emails only from these specific paths and send them replies or delete them, so how can i do that?How can i read the folder based on a path n such way i get the account associated with it so i can also send emails using that accounts.
Use the following code to loop through all the folders then match the path with the ones in the list.
CODE:
OutLook.Application oApp = new OutLook.Application();
OutLook.NameSpace oNS = (OutLook.NameSpace)oApp.GetNamespace("MAPI");
oNS.Logon(Missing.Value, Missing.Value, false, true);
foreach (OutLook.MAPIFolder folder in oNS.Folders)
{
string folderName = folder.Name;
GetFolders(folder);
}
public void GetFolders(MAPIFolder folder)
{
if (folder.Folders.Count == 0)
{
string path = folder.FullFolderPath;
if (foldersTocheck.Contains(path))
{
//GET EMAILS.....
foreach (OutLook.MailItem item in folder.Items)
{
Console.WriteLine(item.SenderEmailAddress + " " + item.Subject + "\n" + item.Body);
}
}
}
else
{
foreach (MAPIFolder subFolder in folder.Folders)
{
GetFolders(subFolder);
}
}
}

Get unread Mails from Outlook

Is there any way to get all mail from an specific Folder into my Application?
Check this link. Introduction to Outlook Programming will explain things more clearly.
You could loop through the mailitems. Sample code
using System.Runtime.InteropServices;
using OutLook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
OutLook.Application oApp;
OutLook._NameSpace oNS;
OutLook.MAPIFolder oFolder;
OutLook._Explorer oExp;
oApp = new OutLook.Application();
oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
OutLook.Items items = oFolder.Items;
foreach (OutLook.MailItem mail in items)
{
if (mail.UnRead == true)
{
}
}
Edit:
Reference other folders
oFolder.Folders["Foldername"]
OutLook Code
Common Outlook tasks
Looping through all items in a folder is a terrible idea, especially if you are working against an online Exchange store. Items.Find/FindNext or Items.Restrict is the way to go.
Find/FindNext:
OutLook.Items items = oFolder.Items;
OutLook.MailItem mail = items.Find("[Unread] = true");
while (mail != null)
{
MessageBox.Show(mail.Subject);
mail = items.FindNext();
}
Items.Restrict:
OutLook.Items items = oFolder.Items.Restict("[Unread] = true")
foreach (OutLook.MailItem mail in items)
{
MessageBox.Show(mail.Subject);
}
There's some examples of accessing Outlook folders here, one of which deals specifically with unread mail.
Edit: There's a KB article specifically about accessing folders from C#, Programming samples that can reference items and folders in Outlook by using Visual C# .NET
To open another user's folder, use GetSharedDefaultFolder
foreach (Object Unreadmail in folderItems)
{
if ((Unreadmail as Outlook.MailItem) != null && (Unreadmail as Outlook.MailItem).UnRead == true)
{
//DO Your action Here
}
}
I have experienced "COM_object" exception error with above solutions, More info please refer here

Categories